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-115937: remove extra processing for the __signature__ attribute #115984

Closed
wants to merge 1 commit into from
Closed
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
22 changes: 15 additions & 7 deletions Lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,21 @@ def _add_member_(cls, name, member):
#
cls._member_map_[name] = member

@property
def __signature__(cls):
from inspect import Parameter, Signature
if cls._member_names_:
return Signature([Parameter('values', Parameter.VAR_POSITIONAL)])
else:
return 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)])


EnumMeta = EnumType # keep EnumMeta name for backwards compatibility


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

@classmethod
def __signature__(cls):
if cls._member_names_:
return '(*values)'
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: 1 addition & 9 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2547,18 +2547,10 @@ 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(o_sig))
'attribute'.format(sig))
return sig

try:
Expand Down
32 changes: 0 additions & 32 deletions Lib/test/test_inspect/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -4202,38 +4202,6 @@ 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')),
...))

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should test that random objects should be passed as-is by the signature function (5, None, []...).
But maybe making this implementation perfect is not urgent as long as a decision has not been made as to what follow-up to bring to the base issue.

Copy link
Member Author

Choose a reason for hiding this comment

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

Since 42407ab there is also type check for returned sig object. So, my commit just returns things to pre-#100168 state (enum module tests kept).

Copy link
Contributor

@Gouvernathor Gouvernathor Feb 27, 2024

Choose a reason for hiding this comment

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

Ok, this is long-standing behavior, but then it needs to be documented. (again, it can wait)

def test_signature_on_derived_classes(self):
# gh-105080: Make sure that signatures are consistent on derived classes

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Removed extra preprocessing for the ``__signature__`` attribute: the code
just check if it's a :class:`inspect.Signature` instance. Patch by Sergey B
Kirpichev.
Loading