Skip to content

Commit 1173cac

Browse files
committed
Revert "pythongh-116110: remove extra processing for the __signature__ attribute"
This reverts commit 1f3c7f3.
1 parent 38b7d53 commit 1173cac

File tree

4 files changed

+48
-19
lines changed

4 files changed

+48
-19
lines changed

Lib/enum.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,21 +1092,6 @@ 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-
11101095
EnumMeta = EnumType # keep EnumMeta name for backwards compatibility
11111096

11121097

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

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+
11531145
def __new__(cls, value):
11541146
# all enum instances are actually created during class construction
11551147
# without calling this method; this method is called by the metaclass'

Lib/inspect.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2528,10 +2528,18 @@ def _signature_from_callable(obj, *,
25282528
pass
25292529
else:
25302530
if sig is not None:
2531+
# since __text_signature__ is not writable on classes, __signature__
2532+
# may contain text (or be a callable that returns text);
2533+
# if so, convert it
2534+
o_sig = sig
2535+
if not isinstance(sig, (Signature, str)) and callable(sig):
2536+
sig = sig()
2537+
if isinstance(sig, str):
2538+
sig = _signature_fromstr(sigcls, obj, sig)
25312539
if not isinstance(sig, Signature):
25322540
raise TypeError(
25332541
'unexpected object {!r} in __signature__ '
2534-
'attribute'.format(sig))
2542+
'attribute'.format(o_sig))
25352543
return sig
25362544

25372545
try:

Lib/test/test_inspect/test_inspect.py

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

4897+
def test_signature_as_str(self):
4898+
self.maxDiff = None
4899+
class S:
4900+
__signature__ = '(a, b=2)'
4901+
4902+
self.assertEqual(self.signature(S),
4903+
((('a', ..., ..., 'positional_or_keyword'),
4904+
('b', 2, ..., 'positional_or_keyword')),
4905+
...))
4906+
4907+
def test_signature_as_callable(self):
4908+
# __signature__ should be either a staticmethod or a bound classmethod
4909+
class S:
4910+
@classmethod
4911+
def __signature__(cls):
4912+
return '(a, b=2)'
4913+
4914+
self.assertEqual(self.signature(S),
4915+
((('a', ..., ..., 'positional_or_keyword'),
4916+
('b', 2, ..., 'positional_or_keyword')),
4917+
...))
4918+
4919+
class S:
4920+
@staticmethod
4921+
def __signature__():
4922+
return '(a, b=2)'
4923+
4924+
self.assertEqual(self.signature(S),
4925+
((('a', ..., ..., 'positional_or_keyword'),
4926+
('b', 2, ..., 'positional_or_keyword')),
4927+
...))
4928+
48974929
def test_signature_on_derived_classes(self):
48984930
# gh-105080: Make sure that signatures are consistent on derived classes
48994931

Misc/NEWS.d/next/Library/2024-02-27-10-22-15.gh-issue-115937.0cVNur.rst

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)