Skip to content

Commit 6c52c92

Browse files
committed
Support Enum signatures in Python 3.12 and 3.13
1 parent cef2bce commit 6c52c92

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

sphinx/ext/autodoc/__init__.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import functools
1111
import operator
1212
import re
13+
import sys
1314
from inspect import Parameter, Signature
1415
from typing import TYPE_CHECKING, Any, NewType, TypeVar
1516

@@ -1543,10 +1544,15 @@ def get_user_defined_function_or_method(obj: Any, attr: str) -> Any:
15431544
# This sequence is copied from inspect._signature_from_callable.
15441545
# ValueError means that no signature could be found, so we keep going.
15451546

1546-
# First, we check the obj has a __signature__ attribute
1547-
if (hasattr(self.object, '__signature__') and
1548-
isinstance(self.object.__signature__, Signature)):
1549-
return None, None, self.object.__signature__
1547+
# First, we check if obj has a __signature__ attribute
1548+
if hasattr(self.object, '__signature__'):
1549+
object_sig = self.object.__signature__
1550+
if isinstance(object_sig, Signature):
1551+
return None, None, object_sig
1552+
if sys.version_info[:2] in {(3, 12), (3, 13)} and callable(object_sig):
1553+
# Support for enum.Enum.__signature__ in Python 3.12
1554+
if isinstance(object_sig_str := object_sig(), str):
1555+
return None, None, inspect.signature_from_str(object_sig_str)
15501556

15511557
# Next, let's see if it has an overloaded __call__ defined
15521558
# in its metaclass

tests/test_extensions/test_ext_autodoc.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -1562,7 +1562,7 @@ def preamble_lookup(
15621562
doc
15631563
), f'enumeration class {self.target!r} should have an explicit docstring'
15641564

1565-
args = self._preamble_args()
1565+
args = self._preamble_args(functional_constructor=False)
15661566
return self._preamble(doc=doc, args=args, indent=indent, **options)
15671567

15681568
def preamble_constructor(
@@ -1572,7 +1572,7 @@ def preamble_constructor(
15721572
doc
15731573
), f'enumeration class {self.target!r} should have an explicit docstring'
15741574

1575-
args = self._preamble_args()
1575+
args = self._preamble_args(functional_constructor=True)
15761576
return self._preamble(doc=doc, args=args, indent=indent, **options)
15771577

15781578
def _preamble(
@@ -1582,12 +1582,25 @@ def _preamble(
15821582
return self._node('class', self.name, doc, args=args, indent=indent, **options)
15831583

15841584
@staticmethod
1585-
def _preamble_args():
1585+
def _preamble_args(functional_constructor: bool = False):
15861586
"""EnumType.__call__() is a dual-purpose method:
15871587
15881588
* Look an enum member (valid only if the enum has members)
15891589
* Create a new enum class (functional API)
15901590
"""
1591+
if sys.version_info[:2] >= (3, 13) or sys.version_info[:3] >= (3, 12, 3):
1592+
if functional_constructor:
1593+
return (
1594+
'(new_class_name, /, names, *, module=None, '
1595+
'qualname=None, type=None, start=1, boundary=None)'
1596+
)
1597+
else:
1598+
return '(*values)'
1599+
if sys.version_info[:2] >= (3, 12):
1600+
return (
1601+
'(value, names=None, *values, module=None, '
1602+
'qualname=None, type=None, start=1, boundary=None)'
1603+
)
15911604
return '(value)'
15921605

15931606
def method(

0 commit comments

Comments
 (0)