bpo-40897:Give priority to using the current class constructor in `inspect.signature` by hongweipeng · Pull Request #27177 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions Lib/inspect.py
41 changes: 41 additions & 0 deletions Lib/test/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down