mirror of
https://github.com/python/cpython.git
synced 2026-01-06 15:32:22 +00:00
gh-100039: enhance __signature__ to work with str and callables (GH-100168)
Callables should be either class- or static-methods. Enum now uses the classmethod version to greatly improve the help given for enums and flags.
This commit is contained in:
parent
5234e1cbea
commit
a5a7cea202
5 changed files with 73 additions and 4 deletions
|
|
@ -4259,7 +4259,7 @@ class TestEnumTypeSubclassing(unittest.TestCase):
|
|||
Help on class Color in module %s:
|
||||
|
||||
class Color(enum.Enum)
|
||||
| Color(value, names=None, *values, module=None, qualname=None, type=None, start=1, boundary=None)
|
||||
| Color(*values)
|
||||
|
|
||||
| Method resolution order:
|
||||
| Color
|
||||
|
|
@ -4315,7 +4315,7 @@ class Color(enum.Enum)
|
|||
Help on class Color in module %s:
|
||||
|
||||
class Color(enum.Enum)
|
||||
| Color(value, names=None, *values, module=None, qualname=None, type=None, start=1)
|
||||
| Color(*values)
|
||||
|
|
||||
| Method resolution order:
|
||||
| Color
|
||||
|
|
@ -4462,6 +4462,27 @@ def test_inspect_classify_class_attrs(self):
|
|||
if failed:
|
||||
self.fail("result does not equal expected, see print above")
|
||||
|
||||
def test_inspect_signatures(self):
|
||||
from inspect import signature, Signature, Parameter
|
||||
self.assertEqual(
|
||||
signature(Enum),
|
||||
Signature([
|
||||
Parameter('new_class_name', Parameter.POSITIONAL_ONLY),
|
||||
Parameter('names', Parameter.POSITIONAL_OR_KEYWORD),
|
||||
Parameter('module', Parameter.KEYWORD_ONLY, default=None),
|
||||
Parameter('qualname', Parameter.KEYWORD_ONLY, default=None),
|
||||
Parameter('type', Parameter.KEYWORD_ONLY, default=None),
|
||||
Parameter('start', Parameter.KEYWORD_ONLY, default=1),
|
||||
Parameter('boundary', Parameter.KEYWORD_ONLY, default=None),
|
||||
]),
|
||||
)
|
||||
self.assertEqual(
|
||||
signature(enum.FlagBoundary),
|
||||
Signature([
|
||||
Parameter('values', Parameter.VAR_POSITIONAL),
|
||||
]),
|
||||
)
|
||||
|
||||
def test_test_simple_enum(self):
|
||||
@_simple_enum(Enum)
|
||||
class SimpleColor:
|
||||
|
|
|
|||
|
|
@ -3614,6 +3614,38 @@ def foo(): pass
|
|||
self.assertEqual(signature_func(foo), inspect.Signature())
|
||||
self.assertEqual(inspect.get_annotations(foo), {})
|
||||
|
||||
def test_signature_as_str(self):
|
||||
self.maxDiff = None
|
||||
class S:
|
||||
__signature__ = '(a, b=2)'
|
||||
|
||||
self.assertEqual(self.signature(S),
|
||||
((('a', ..., ..., 'positional_or_keyword'),
|
||||
('b', 2, ..., 'positional_or_keyword')),
|
||||
...))
|
||||
|
||||
def test_signature_as_callable(self):
|
||||
# __signature__ should be either a staticmethod or a bound classmethod
|
||||
class S:
|
||||
@classmethod
|
||||
def __signature__(cls):
|
||||
return '(a, b=2)'
|
||||
|
||||
self.assertEqual(self.signature(S),
|
||||
((('a', ..., ..., 'positional_or_keyword'),
|
||||
('b', 2, ..., 'positional_or_keyword')),
|
||||
...))
|
||||
|
||||
class S:
|
||||
@staticmethod
|
||||
def __signature__():
|
||||
return '(a, b=2)'
|
||||
|
||||
self.assertEqual(self.signature(S),
|
||||
((('a', ..., ..., 'positional_or_keyword'),
|
||||
('b', 2, ..., 'positional_or_keyword')),
|
||||
...))
|
||||
|
||||
|
||||
class TestParameterObject(unittest.TestCase):
|
||||
def test_signature_parameter_kinds(self):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue