bpo-40897:Give priority to using the current class constructor in `inspect.signature` (GH-27177) (#27189)
Co-authored-by: Ćukasz Langa <lukasz@langa.pl>
(cherry picked from commit 6aab5f9bf303a8e4cd8377fabcdcb499e0541f9a)
Co-authored-by: Weipeng Hong <hongweichen8888@sina.com>
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index 0ab6530..38d1618 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -3063,6 +3063,47 @@ def __init__(self, b):
('bar', 2, ..., "keyword_only")),
...))
+ def test_signature_on_subclass(self):
+ class A:
+ def __new__(cls, a=1, *args, **kwargs):
+ return object.__new__(cls)
+ class B(A):
+ def __init__(self, b):
+ pass
+ class C(A):
+ def __new__(cls, a=1, b=2, *args, **kwargs):
+ return object.__new__(cls)
+ class D(A):
+ pass
+
+ self.assertEqual(self.signature(B),
+ ((('b', ..., ..., "positional_or_keyword"),),
+ ...))
+ self.assertEqual(self.signature(C),
+ ((('a', 1, ..., 'positional_or_keyword'),
+ ('b', 2, ..., 'positional_or_keyword'),
+ ('args', ..., ..., 'var_positional'),
+ ('kwargs', ..., ..., 'var_keyword')),
+ ...))
+ self.assertEqual(self.signature(D),
+ ((('a', 1, ..., 'positional_or_keyword'),
+ ('args', ..., ..., 'var_positional'),
+ ('kwargs', ..., ..., 'var_keyword')),
+ ...))
+
+ def test_signature_on_generic_subclass(self):
+ from typing import Generic, TypeVar
+
+ T = TypeVar('T')
+
+ class A(Generic[T]):
+ def __init__(self, *, a: int) -> None:
+ pass
+
+ self.assertEqual(self.signature(A),
+ ((('a', ..., int, 'keyword_only'),),
+ None))
+
@unittest.skipIf(MISSING_C_DOCSTRINGS,
"Signature information for builtins requires docstrings")
def test_signature_on_class_without_init(self):