Skip to content

Commit 378d706

Browse files
corona101st1
authored andcommitted
bpo-30149: Fix partialmethod without explicit self parameter (#1308)
1 parent f8d05b3 commit 378d706

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

Lib/inspect.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2241,11 +2241,16 @@ def _signature_from_callable(obj, *,
22412241
sigcls=sigcls)
22422242

22432243
sig = _signature_get_partial(wrapped_sig, partialmethod, (None,))
2244-
22452244
first_wrapped_param = tuple(wrapped_sig.parameters.values())[0]
2246-
new_params = (first_wrapped_param,) + tuple(sig.parameters.values())
2247-
2248-
return sig.replace(parameters=new_params)
2245+
if first_wrapped_param.kind is Parameter.VAR_POSITIONAL:
2246+
# First argument of the wrapped callable is `*args`, as in
2247+
# `partialmethod(lambda *args)`.
2248+
return sig
2249+
else:
2250+
sig_params = tuple(sig.parameters.values())
2251+
assert first_wrapped_param is not sig_params[0]
2252+
new_params = (first_wrapped_param,) + sig_params
2253+
return sig.replace(parameters=new_params)
22492254

22502255
if isfunction(obj) or _signature_is_functionlike(obj):
22512256
# If it's a pure Python function, or an object that is duck type

Lib/test/test_inspect.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,6 +1986,41 @@ def test(a, b:'foo'=10, *args:'bar', spam:'baz', ham=123, **kwargs:int):
19861986
('kwargs', ..., int, "var_keyword")),
19871987
...))
19881988

1989+
def test_signature_without_self(self):
1990+
def test_args_only(*args): # NOQA
1991+
pass
1992+
1993+
def test_args_kwargs_only(*args, **kwargs): # NOQA
1994+
pass
1995+
1996+
class A:
1997+
@classmethod
1998+
def test_classmethod(*args): # NOQA
1999+
pass
2000+
2001+
@staticmethod
2002+
def test_staticmethod(*args): # NOQA
2003+
pass
2004+
2005+
f1 = functools.partialmethod((test_classmethod), 1)
2006+
f2 = functools.partialmethod((test_args_only), 1)
2007+
f3 = functools.partialmethod((test_staticmethod), 1)
2008+
f4 = functools.partialmethod((test_args_kwargs_only),1)
2009+
2010+
self.assertEqual(self.signature(test_args_only),
2011+
((('args', ..., ..., 'var_positional'),), ...))
2012+
self.assertEqual(self.signature(test_args_kwargs_only),
2013+
((('args', ..., ..., 'var_positional'),
2014+
('kwargs', ..., ..., 'var_keyword')), ...))
2015+
self.assertEqual(self.signature(A.f1),
2016+
((('args', ..., ..., 'var_positional'),), ...))
2017+
self.assertEqual(self.signature(A.f2),
2018+
((('args', ..., ..., 'var_positional'),), ...))
2019+
self.assertEqual(self.signature(A.f3),
2020+
((('args', ..., ..., 'var_positional'),), ...))
2021+
self.assertEqual(self.signature(A.f4),
2022+
((('args', ..., ..., 'var_positional'),
2023+
('kwargs', ..., ..., 'var_keyword')), ...))
19892024
@cpython_only
19902025
@unittest.skipIf(MISSING_C_DOCSTRINGS,
19912026
"Signature information for builtins requires docstrings")

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,10 @@ Extension Modules
323323
Library
324324
-------
325325

326+
- bpo-30149: inspect.signature() now supports callables with
327+
variable-argument parameters wrapped with partialmethod.
328+
Patch by Dong-hee Na.
329+
326330
- bpo-30301: Fix AttributeError when using SimpleQueue.empty() under
327331
*spawn* and *forkserver* start methods.
328332

0 commit comments

Comments
 (0)