Skip to content

Commit dc1cae4

Browse files
authored
Bind method descriptor when __get__ owner is omitted (#8404)
`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
1 parent 16bb018 commit dc1cae4

2 files changed

Lines changed: 4 additions & 2 deletions

File tree

Lib/test/test_types.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,6 @@ def test_method_descriptor_types(self):
660660
self.assertIsInstance(int.from_bytes, types.BuiltinMethodType)
661661
self.assertIsInstance(int.__new__, types.BuiltinMethodType)
662662

663-
@unittest.expectedFailure # TODO: RUSTPYTHON; TypeError: descriptor 'read' needs a type, not 'StringIO', as arg 2
664663
def test_method_descriptor_crash(self):
665664
# gh-132747: The default __get__() implementation in C was unable
666665
# to handle a second argument of None when called from Python

crates/vm/src/builtins/descriptor.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ impl GetDescriptor for PyMethodDescriptor {
7979
let bound = match obj {
8080
Some(obj) => {
8181
if descr.method.flags.contains(PyMethodFlags::METHOD) {
82-
if cls.is_some_and(|c| c.fast_isinstance(vm.ctx.types.type_type)) {
82+
if cls
83+
.as_ref()
84+
.is_none_or(|c| c.fast_isinstance(vm.ctx.types.type_type))
85+
{
8386
obj
8487
} else {
8588
return Err(vm.new_type_error(format!(

0 commit comments

Comments
 (0)