Skip to content

Commit eafd14f

Browse files
authored
gh-116110: remove extra processing for the __signature__ attribute (GH-116234)
This is an alternative to GH-100168.
1 parent b2a7272 commit eafd14f

File tree

4 files changed

+19
-48
lines changed

4 files changed

+19
-48
lines changed

Lib/enum.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,21 @@ def _add_member_(cls, name, member):
10921092
# now add to _member_map_ (even aliases)
10931093
cls._member_map_[name] = member
10941094

1095+
@property
1096+
def __signature__(cls):
1097+
from inspect import Parameter, Signature
1098+
if cls._member_names_:
1099+
return Signature([Parameter('values', Parameter.VAR_POSITIONAL)])
1100+
else:
1101+
return Signature([Parameter('new_class_name', Parameter.POSITIONAL_ONLY),
1102+
Parameter('names', Parameter.POSITIONAL_OR_KEYWORD),
1103+
Parameter('module', Parameter.KEYWORD_ONLY, default=None),
1104+
Parameter('qualname', Parameter.KEYWORD_ONLY, default=None),
1105+
Parameter('type', Parameter.KEYWORD_ONLY, default=None),
1106+
Parameter('start', Parameter.KEYWORD_ONLY, default=1),
1107+
Parameter('boundary', Parameter.KEYWORD_ONLY, default=None)])
1108+
1109+
10951110
EnumMeta = EnumType # keep EnumMeta name for backwards compatibility
10961111

10971112

@@ -1135,13 +1150,6 @@ class Enum(metaclass=EnumType):
11351150
attributes -- see the documentation for details.
11361151
"""
11371152

1138-
@classmethod
1139-
def __signature__(cls):
1140-
if cls._member_names_:
1141-
return '(*values)'
1142-
else:
1143-
return '(new_class_name, /, names, *, module=None, qualname=None, type=None, start=1, boundary=None)'
1144-
11451153
def __new__(cls, value):
11461154
# all enum instances are actually created during class construction
11471155
# without calling this method; this method is called by the metaclass'

Lib/inspect.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -2424,18 +2424,10 @@ def _signature_from_callable(obj, *,
24242424
pass
24252425
else:
24262426
if sig is not None:
2427-
# since __text_signature__ is not writable on classes, __signature__
2428-
# may contain text (or be a callable that returns text);
2429-
# if so, convert it
2430-
o_sig = sig
2431-
if not isinstance(sig, (Signature, str)) and callable(sig):
2432-
sig = sig()
2433-
if isinstance(sig, str):
2434-
sig = _signature_fromstr(sigcls, obj, sig)
24352427
if not isinstance(sig, Signature):
24362428
raise TypeError(
24372429
'unexpected object {!r} in __signature__ '
2438-
'attribute'.format(o_sig))
2430+
'attribute'.format(sig))
24392431
return sig
24402432

24412433
try:

Lib/test/test_inspect/test_inspect.py

-32
Original file line numberDiff line numberDiff line change
@@ -4879,38 +4879,6 @@ def foo(): pass
48794879
self.assertEqual(signature_func(foo), inspect.Signature())
48804880
self.assertEqual(inspect.get_annotations(foo), {})
48814881

4882-
def test_signature_as_str(self):
4883-
self.maxDiff = None
4884-
class S:
4885-
__signature__ = '(a, b=2)'
4886-
4887-
self.assertEqual(self.signature(S),
4888-
((('a', ..., ..., 'positional_or_keyword'),
4889-
('b', 2, ..., 'positional_or_keyword')),
4890-
...))
4891-
4892-
def test_signature_as_callable(self):
4893-
# __signature__ should be either a staticmethod or a bound classmethod
4894-
class S:
4895-
@classmethod
4896-
def __signature__(cls):
4897-
return '(a, b=2)'
4898-
4899-
self.assertEqual(self.signature(S),
4900-
((('a', ..., ..., 'positional_or_keyword'),
4901-
('b', 2, ..., 'positional_or_keyword')),
4902-
...))
4903-
4904-
class S:
4905-
@staticmethod
4906-
def __signature__():
4907-
return '(a, b=2)'
4908-
4909-
self.assertEqual(self.signature(S),
4910-
((('a', ..., ..., 'positional_or_keyword'),
4911-
('b', 2, ..., 'positional_or_keyword')),
4912-
...))
4913-
49144882
def test_signature_on_derived_classes(self):
49154883
# gh-105080: Make sure that signatures are consistent on derived classes
49164884

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

0 commit comments

Comments
 (0)