Skip to content

Commit bf8b07f

Browse files
committed
pythongh-115937: remove extra processing for the __signature__ attribute
This is an alternative to python#100168.
1 parent 8e8ab75 commit bf8b07f

File tree

4 files changed

+19
-48
lines changed

4 files changed

+19
-48
lines changed

Lib/enum.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,21 @@ def _add_member_(cls, name, member):
10831083
#
10841084
cls._member_map_[name] = member
10851085

1086+
@property
1087+
def __signature__(cls):
1088+
from inspect import Parameter, Signature
1089+
if cls._member_names_:
1090+
return Signature([Parameter('values', Parameter.VAR_POSITIONAL)])
1091+
else:
1092+
return Signature([Parameter('new_class_name', Parameter.POSITIONAL_ONLY),
1093+
Parameter('names', Parameter.POSITIONAL_OR_KEYWORD),
1094+
Parameter('module', Parameter.KEYWORD_ONLY, default=None),
1095+
Parameter('qualname', Parameter.KEYWORD_ONLY, default=None),
1096+
Parameter('type', Parameter.KEYWORD_ONLY, default=None),
1097+
Parameter('start', Parameter.KEYWORD_ONLY, default=1),
1098+
Parameter('boundary', Parameter.KEYWORD_ONLY, default=None)])
1099+
1100+
10861101
EnumMeta = EnumType # keep EnumMeta name for backwards compatibility
10871102

10881103

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

1129-
@classmethod
1130-
def __signature__(cls):
1131-
if cls._member_names_:
1132-
return '(*values)'
1133-
else:
1134-
return '(new_class_name, /, names, *, module=None, qualname=None, type=None, start=1, boundary=None)'
1135-
11361144
def __new__(cls, value):
11371145
# all enum instances are actually created during class construction
11381146
# without calling this method; this method is called by the metaclass'

Lib/inspect.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2547,18 +2547,10 @@ def _signature_from_callable(obj, *,
25472547
pass
25482548
else:
25492549
if sig is not None:
2550-
# since __text_signature__ is not writable on classes, __signature__
2551-
# may contain text (or be a callable that returns text);
2552-
# if so, convert it
2553-
o_sig = sig
2554-
if not isinstance(sig, (Signature, str)) and callable(sig):
2555-
sig = sig()
2556-
if isinstance(sig, str):
2557-
sig = _signature_fromstr(sigcls, obj, sig)
25582550
if not isinstance(sig, Signature):
25592551
raise TypeError(
25602552
'unexpected object {!r} in __signature__ '
2561-
'attribute'.format(o_sig))
2553+
'attribute'.format(sig))
25622554
return sig
25632555

25642556
try:

Lib/test/test_inspect/test_inspect.py

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4202,38 +4202,6 @@ def foo(): pass
42024202
self.assertEqual(signature_func(foo), inspect.Signature())
42034203
self.assertEqual(inspect.get_annotations(foo), {})
42044204

4205-
def test_signature_as_str(self):
4206-
self.maxDiff = None
4207-
class S:
4208-
__signature__ = '(a, b=2)'
4209-
4210-
self.assertEqual(self.signature(S),
4211-
((('a', ..., ..., 'positional_or_keyword'),
4212-
('b', 2, ..., 'positional_or_keyword')),
4213-
...))
4214-
4215-
def test_signature_as_callable(self):
4216-
# __signature__ should be either a staticmethod or a bound classmethod
4217-
class S:
4218-
@classmethod
4219-
def __signature__(cls):
4220-
return '(a, b=2)'
4221-
4222-
self.assertEqual(self.signature(S),
4223-
((('a', ..., ..., 'positional_or_keyword'),
4224-
('b', 2, ..., 'positional_or_keyword')),
4225-
...))
4226-
4227-
class S:
4228-
@staticmethod
4229-
def __signature__():
4230-
return '(a, b=2)'
4231-
4232-
self.assertEqual(self.signature(S),
4233-
((('a', ..., ..., 'positional_or_keyword'),
4234-
('b', 2, ..., 'positional_or_keyword')),
4235-
...))
4236-
42374205
def test_signature_on_derived_classes(self):
42384206
# gh-105080: Make sure that signatures are consistent on derived classes
42394207

Lines changed: 3 additions & 0 deletions
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)