From 8a387219bdfb6ee34928d6168ac42ca559f11c9a Mon Sep 17 00:00:00 2001 From: Yury Selivanov Date: Tue, 6 Mar 2018 12:59:45 -0500 Subject: [PATCH] bpo-33009: Fix inspect.signature() for single-parameter partialmethods. (GH-6004) --- Lib/inspect.py | 3 ++- Lib/test/test_inspect.py | 10 ++++++++++ .../Library/2018-03-06-11-54-59.bpo-33009.-Ekysb.rst | 1 + 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2018-03-06-11-54-59.bpo-33009.-Ekysb.rst diff --git a/Lib/inspect.py b/Lib/inspect.py index 57c04877c7..512785f923 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -2254,7 +2254,8 @@ def _signature_from_callable(obj, *, return sig else: sig_params = tuple(sig.parameters.values()) - assert first_wrapped_param is not sig_params[0] + assert (not sig_params or + first_wrapped_param is not sig_params[0]) new_params = (first_wrapped_param,) + sig_params return sig.replace(parameters=new_params) diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 1a856f6387..3481a57ec8 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -2580,6 +2580,16 @@ class TestSignatureObject(unittest.TestCase): ('c', 1, ..., 'keyword_only')), 'spam')) + class Spam: + def test(self: 'anno', x): + pass + + g = partialmethod(test, 1) + + self.assertEqual(self.signature(Spam.g), + ((('self', ..., 'anno', 'positional_or_keyword'),), + ...)) + def test_signature_on_fake_partialmethod(self): def foo(a): pass foo._partialmethod = 'spam' diff --git a/Misc/NEWS.d/next/Library/2018-03-06-11-54-59.bpo-33009.-Ekysb.rst b/Misc/NEWS.d/next/Library/2018-03-06-11-54-59.bpo-33009.-Ekysb.rst new file mode 100644 index 0000000000..96bc70a8c9 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-03-06-11-54-59.bpo-33009.-Ekysb.rst @@ -0,0 +1 @@ +Fix inspect.signature() for single-parameter partialmethods. -- 2.50.0