Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-100039: enhance __signature__ to work with str and callables #100168

Merged
merged 4 commits into from
Dec 16, 2022
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
9 changes: 8 additions & 1 deletion Lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ def __new__(metacls, cls, bases, classdict, *, boundary=None, _simple=False, **k
'member order does not match _order_:\n %r\n %r'
% (enum_class._member_names_, _order_)
)

#
return enum_class

def __bool__(cls):
Expand Down Expand Up @@ -1083,6 +1083,13 @@ class Enum(metaclass=EnumType):
attributes -- see the documentation for details.
"""

@classmethod
def __signature__(cls):
if cls._member_names_:
return '(*values)'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why *values? If I understand correctly this corresponds to calls like Color(1), which only accept exactly one argument.

Copy link
Member Author

@ethanfurman ethanfurman Dec 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 3.12 multiple values are accepted to match multiple values being used to create the member:

class Point(Enum):
    ORIGIN = 0, 0

>>> Point(0, 0)
<Point.ORIGIN: (0, 0))>

else:
return '(new_class_name, /, names, *, module=None, qualname=None, type=None, start=1, boundary=None)'

def __new__(cls, value):
# all enum instances are actually created during class construction
# without calling this method; this method is called by the metaclass'
Expand Down
10 changes: 9 additions & 1 deletion Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2443,10 +2443,18 @@ def _signature_from_callable(obj, *,
pass
else:
if sig is not None:
# since __text_signature__ is not writable on classes, __signature__
# may contain text (or be a callable that returns text);
# if so, convert it
o_sig = sig
if not isinstance(sig, (Signature, str)) and callable(sig):
sig = sig()
if isinstance(sig, str):
sig = _signature_fromstr(sigcls, obj, sig)
if not isinstance(sig, Signature):
raise TypeError(
'unexpected object {!r} in __signature__ '
'attribute'.format(sig))
'attribute'.format(o_sig))
return sig

try:
Expand Down
25 changes: 23 additions & 2 deletions Lib/test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
32 changes: 32 additions & 0 deletions Lib/test/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve signatures for enums and flags.