Skip to content

gh-112821: Fix rlcompleter failures on objects with descriptors#149577

Merged
mdboom merged 2 commits intopython:mainfrom
mdboom:issue112821
May 11, 2026
Merged

gh-112821: Fix rlcompleter failures on objects with descriptors#149577
mdboom merged 2 commits intopython:mainfrom
mdboom:issue112821

Conversation

@mdboom
Copy link
Copy Markdown
Contributor

@mdboom mdboom commented May 8, 2026

This issue came to light for me when it was discovered that autocompletion didn't work for a library I help maintain, written mainly in Cython. Cython implements @property-annotated methods on its cdef classes as getset descriptors. The rlcompleter logic doesn't exclude these (like it does regular Python properties) since isinstance(member, property) is False. It then goes on to call the getter, which in some cases (in our library at least) raises an exception. Any exception other than an AttributeError causes the rlcompleter to short circuit completely and not offer any options.

This is a more surgical (rlcompleter-only) solution than what was proposed in #112821. If adding ABCs is preferred, I could do that.

Restore the try/except removed in #27401
Create abstract classes for descriptors in _collections_abc (e.g. ImplementsGet, ImplementsSet, ImplementsDelete) and in rlcompleter, rather than checking for isinstance(obj, property), use isinstance(obj, ImplementsGet).

#112821 also mentioned:

This might cause false positives for method descriptors though.

I think this is handled here, but I also may not fully understand this specific issue.

@mdboom mdboom added type-bug An unexpected behavior, bug, or error 3.13 bugs and security fixes 3.14 bugs and security fixes topic-repl Related to the interactive shell 3.15 pre-release feature fixes, bugs and security fixes 3.16 new features, bugs and security fixes labels May 8, 2026
@python-cla-bot
Copy link
Copy Markdown

python-cla-bot Bot commented May 8, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

@picnixz picnixz added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes and removed 3.13 bugs and security fixes 3.14 bugs and security fixes 3.15 pre-release feature fixes, bugs and security fixes 3.16 new features, bugs and security fixes type-bug An unexpected behavior, bug, or error topic-repl Related to the interactive shell labels May 8, 2026
Comment thread Lib/rlcompleter.py
# Also, getattr(thisobject, word) will evaluate the
# property method, which is not desirable.

class_attr = getattr(type(thisobject), word, None)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could also run arbitrary code because the descriptor protocol also gets run on access through the class. To be fully safe we should probably use something like inspect.getattr_static. Still, your change is a strict improvement so I'm OK with landing it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I experimented with this but it breaks another test create_expected_for_none and changes behavior elsewhere. Upstream behavior and this PR:

>>> None.__class__<TAB>
None.__class__()

Changing this line to inspect.getattr_static(type(thisobject), word, None):

>>> None.__class__<TAB>
None.__class__

I could argue this either way, but I think I will merge this as-is as it doesn't change any existing tested behavior.

@mdboom mdboom merged commit f23a183 into python:main May 11, 2026
56 checks passed
@miss-islington-app
Copy link
Copy Markdown

Thanks @mdboom for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15.
🐍🍒⛏🤖

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 11, 2026

GH-149656 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label May 11, 2026
@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 11, 2026

GH-149657 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label May 11, 2026
@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 11, 2026

GH-149658 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 bugs and security fixes label May 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants