Skip to content

Commit c5b53dc

Browse files
committed
fix: classmethod __new__
1 parent d6eb10d commit c5b53dc

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

Lib/inspect.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,10 +1917,12 @@ def _signature_get_user_defined_method(cls, method_name, *, follow_wrapper_chain
19171917
if meth is None:
19181918
return None
19191919

1920+
unwrapped_meth = None
19201921
if follow_wrapper_chains:
1921-
meth = unwrap(meth, stop=(lambda m: hasattr(m, "__signature__")
1922+
unwrapped_meth = unwrap(meth, stop=(lambda m: hasattr(m, "__signature__")
19221923
or _signature_is_builtin(m)))
1923-
if isinstance(meth, _NonUserDefinedCallables):
1924+
if (isinstance(meth, _NonUserDefinedCallables)
1925+
or isinstance(unwrapped_meth, _NonUserDefinedCallables)):
19241926
# Once '__signature__' will be added to 'C'-level
19251927
# callables, this check won't be necessary
19261928
return None

Lib/test/test_inspect/test_inspect.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4154,17 +4154,17 @@ def __new__(cls, a):
41544154
...))
41554155

41564156
# TODO: classmethod is not correct
4157-
# with self.subTest('classmethod'):
4158-
# class C:
4159-
# @classmethod
4160-
# @identity
4161-
# def __new__(cls, cls2, a):
4162-
# return a
4163-
4164-
# self.assertEqual(C(1), 1)
4165-
# self.assertEqual(self.signature(C),
4166-
# ((('a', ..., ..., "positional_or_keyword"),),
4167-
# ...))
4157+
with self.subTest('classmethod'):
4158+
class C:
4159+
@classmethod
4160+
@identity
4161+
def __new__(cls, cls2, a):
4162+
return a
4163+
4164+
self.assertEqual(C(1), 1)
4165+
self.assertEqual(self.signature(C),
4166+
((('a', ..., ..., "positional_or_keyword"),),
4167+
...))
41684168

41694169
with self.subTest('staticmethod'):
41704170
class C:
@@ -4179,18 +4179,18 @@ def __new__(cls, a):
41794179
...))
41804180

41814181
# TODO: method type is not correct
4182-
# with self.subTest('MethodType'):
4183-
# class A:
4184-
# @identity
4185-
# def call(self, cls, a):
4186-
# return a
4187-
# class C:
4188-
# __new__ = A().call
4189-
4190-
# self.assertEqual(C(1), 1)
4191-
# self.assertEqual(self.signature(C),
4192-
# ((('a', ..., ..., "positional_or_keyword"),),
4193-
# ...))
4182+
with self.subTest('MethodType'):
4183+
class A:
4184+
@identity
4185+
def call(self, cls, a):
4186+
return a
4187+
class C:
4188+
__new__ = A().call
4189+
4190+
self.assertEqual(C(1), 1)
4191+
self.assertEqual(self.signature(C),
4192+
((('a', ..., ..., "positional_or_keyword"),),
4193+
...))
41944194

41954195
with self.subTest('partial'):
41964196
class C:

0 commit comments

Comments
 (0)