Skip to content

Commit e770e25

Browse files
authored
Merge pull request #10427 from tk0miya/10421_autodoc_preserve_defaults_for_classmethod
Fix #10421: autodoc_preserve_defaults doesn't work on class methods
2 parents f58771c + 10dd5a0 commit e770e25

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

CHANGES

+2
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ Bugs fixed
9999
function
100100
* #10305: autodoc: Failed to extract optional forward-ref'ed typehints correctly
101101
via :confval:`autodoc_type_aliases`
102+
* #10421: autodoc: :confval:`autodoc_preserve_defaults` doesn't work on class
103+
methods
102104
* #10214: html: invalid language tag was generated if :confval:`language`
103105
contains a country code (ex. zh_CN)
104106
* #9974: html: Updated jQuery version from 3.5.1 to 3.6.0

sphinx/ext/autodoc/preserve_defaults.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import ast
88
import inspect
99
import sys
10+
from inspect import Parameter
1011
from typing import Any, Dict, List, Optional
1112

1213
from sphinx.application import Sphinx
@@ -96,8 +97,18 @@ def update_defvalue(app: Sphinx, obj: Any, bound_method: bool) -> None:
9697
if value is None:
9798
value = ast_unparse(default) # type: ignore
9899
parameters[i] = param.replace(default=DefaultValue(value))
100+
101+
if bound_method and inspect.ismethod(obj):
102+
# classmethods
103+
cls = inspect.Parameter('cls', Parameter.POSITIONAL_OR_KEYWORD)
104+
parameters.insert(0, cls)
105+
99106
sig = sig.replace(parameters=parameters)
100-
obj.__signature__ = sig
107+
if bound_method and inspect.ismethod(obj):
108+
# classmethods can't be assigned __signature__ attribute.
109+
obj.__dict__['__signature__'] = sig
110+
else:
111+
obj.__signature__ = sig
101112
except (AttributeError, TypeError):
102113
# failed to update signature (ex. built-in or extension types)
103114
pass

tests/roots/test-ext-autodoc/target/preserve_defaults.py

+6
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ def meth(self, name: str = CONSTANT, sentinel: Any = SENTINEL,
2222
now: datetime = datetime.now(), color: int = 0xFFFFFF,
2323
*, kwarg1, kwarg2 = 0xFFFFFF) -> None:
2424
"""docstring"""
25+
26+
@classmethod
27+
def clsmeth(cls, name: str = CONSTANT, sentinel: Any = SENTINEL,
28+
now: datetime = datetime.now(), color: int = 0xFFFFFF,
29+
*, kwarg1, kwarg2 = 0xFFFFFF) -> None:
30+
"""docstring"""

tests/test_ext_autodoc_preserve_defaults.py

+9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ def test_preserve_defaults(app):
2828
' docstring',
2929
'',
3030
'',
31+
' .. py:method:: Class.clsmeth(name: str = CONSTANT, sentinel: ~typing.Any = '
32+
'SENTINEL, now: ~datetime.datetime = datetime.now(), color: int = %s, *, '
33+
'kwarg1, kwarg2=%s) -> None' % (color, color),
34+
' :module: target.preserve_defaults',
35+
' :classmethod:',
36+
'',
37+
' docstring',
38+
'',
39+
'',
3140
' .. py:method:: Class.meth(name: str = CONSTANT, sentinel: ~typing.Any = '
3241
'SENTINEL, now: ~datetime.datetime = datetime.now(), color: int = %s, *, '
3342
'kwarg1, kwarg2=%s) -> None' % (color, color),

0 commit comments

Comments
 (0)