Bind method descriptor when __get__ owner is omitted - #8404
Merged
Conversation
`method_descriptor.__get__(obj)` raised a TypeError when the owner (the optional second argument) was omitted, because the METHOD-flag branch required the owner to be a type. Match CPython: a missing owner binds to `obj`, while a non-type owner still raises "needs a type, not ...". Assisted-by: Claude Code:claude-opus-4-8
Contributor
📝 WalkthroughWalkthroughThe method descriptor binding check now accepts an omitted owner when an instance is provided, while still requiring supplied owners to be type objects. ChangesDescriptor binding
Estimated code review effort: 2 (Simple) | ~5 minutes Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Contributor
📦 Library DependenciesThe following Lib/ modules were modified. Here are their dependencies: [x] lib: cpython/Lib/types.py dependencies:
dependent tests: (57 tests)
Legend:
|
devyubin
marked this pull request as draft
July 27, 2026 23:08
devyubin
marked this pull request as ready for review
August 1, 2026 03:54
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
method_descriptor.__get__(obj)raisedTypeError: descriptor '...' needs a type, not '...', as arg 2when the owner (the second argument) was omitted:The owner is optional in the descriptor protocol; CPython binds to
objwhen it is not supplied, and only rejects a non-type owner.Cause
The
METHOD-flag branch ofPyMethodDescriptor::descr_getgated binding oncls.is_some_and(|c| c.fast_isinstance(type)), so an omitted owner (cls == None) fell through to the "needs a type" error.Fix
Use
cls.as_ref().is_none_or(...): a missing owner binds toobj, while a non-type owner still raises "needs a type, not ...".Test plan
Verified against CPython 3.14.6 — owner omitted binds, non-type owner raises, type owner binds:
test_types'stest_method_descriptor_crash(removes its@expectedFailure); it now passes.test_types/test_descr: pass, no regressions (the non-type-owner "needs a type" path intest_descrstill holds).cargo build/cargo clippy -p rustpython-vm/cargo fmt --check: clean.