8000 gh-116110: remove extra processing for the __signature__ attribute · skirpichev/cpython@1f3c7f3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1f3c7f3

Browse files
committed
pythongh-116110: remove extra processing for the __signature__ attribute
This is an alternative to python#100168.
1 parent 5ae8583 commit 1f3c7f3

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
@@ -1086,6 +1086,21 @@ def _add_member_(cls, name, member):
10861086
#
10871087
cls._member_map_[name] = member
10881088

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

10911106

@@ -1129,13 +1144,6 @@ class Enum(metaclass=EnumType):
11291144
attributes -- see the documentation for details.
11301145
"""
11311146

1132-
@classmethod
1133-
def __signature__(cls):
1134-
if cls._member_names_:
1135-
return '(*values)'
1136-
else:
1137-
return '(new_class_name, /, names, *, module=None, qualname=None, type=None, start=1, boundary=None)'
1138-
11391147
def __new__(cls, value):
11401148
# all enum instances are actually created during class construction
11411149
# 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
@@ -2576,18 +2576,10 @@ def _signature_from_callable(obj, *,
25762576
pass
25772577
else:
25782578
if sig is not None:
2579-
# since __text_signature__ is not writable on classes, __signature__
2580-
# may contain text (or be a callable that returns text);
2581-
# if so, convert it
2582-
o_sig = sig
2583-
if not isinstance(sig, (Signature, str)) and callable(sig):
2584-
sig = sig()
2585-
if isinstance(sig, str):
2586-
sig = _signature_fromstr(sigcls, obj, sig)
25872579
if not isinstance(sig, Signature):
25882580
raise TypeError(
25892581
'unexpected object {!r} in __signature__ '
2590-
'attribute'.format(o_sig))
2582+
'attribute'.format(sig))
25912583
return sig
25922584

25932585
try:

Lib/test/test_inspect/test_inspect.py

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

4575-
def test_signature_as_str(self):
4576-
self.maxDiff = None
4577-
class S:
4578-
__signature__ = '(a, b=2)'
4579-
4580-
self.assertEqual(self.signature(S),
4581-
((('a', ..., ..., 'positional_or_keyword'),
4582-
('b', 2, ..., 'positional_or_keyword')),
4583-
...))
4584-
4585-
def test_signature_as_callable(self):
4586-
# __signature__ should be either a staticmethod or a bound classmethod
4587-
class S:
4588-
@classmethod
4589-
def __signature__(cls):
4590-
return '(a, b=2)'
4591-
4592-
self.assertEqual(self.signature(S),
4593-
((('a', ..., ..., 'positional_or_keyword'),
4594-
('b', 2, ..., 'positional_or_keyword')),
4595-
...))
4596-
4597-
class S:
4598-
@staticmethod
4599-
def __signature__():
4600-
return '(a, b=2)'
4601-
4602-
self.assertEqual(self.signature(S),
4603-
((('a', ..., ..., 'positional_or_keyword'),
4604-
('b', 2, ..., 'positional_or_keyword')),
4605-
...))
4606-
46074575
def test_signature_on_derived_classes(self):
46084576
# gh-105080: Make sure that signatures are consistent on derived classes
46094577

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)
0