Skip to content

Commit

Permalink
Merge pull request #10321 from tk0miya/10266_autodoc_preserve_default…
Browse files Browse the repository at this point in the history
…s_crashes_on_kwonlyargs_without_defaults

Fix #10266: autodoc_preserve_defaults crashes on kwonlyargs w/o defaults
  • Loading branch information
tk0miya committed Apr 3, 2022
2 parents 020a1f9 + 0d31fc9 commit 9d81843
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Expand Up @@ -69,6 +69,8 @@ Bugs fixed
functions are rendered as a string literal
* #10280: autodoc: :confval:`autodoc_docstring_signature` unexpectedly generates
return value typehint for constructors if docstring has multiple signatures
* #10266: autodoc: :confval:`autodoc_preserve_defaults` does not work for
mixture of keyword only arguments with/without defaults
* #10214: html: invalid language tag was generated if :confval:`language`
contains a country code (ex. zh_CN)
* #10236: html search: objects are duplicated in search result
Expand Down
6 changes: 5 additions & 1 deletion sphinx/ext/autodoc/preserve_defaults.py
Expand Up @@ -79,7 +79,11 @@ def update_defvalue(app: Sphinx, obj: Any, bound_method: bool) -> None:
kw_defaults = list(function.args.kw_defaults)
parameters = list(sig.parameters.values())
for i, param in enumerate(parameters):
if param.default is not param.empty:
if param.default is param.empty:
if param.kind == param.KEYWORD_ONLY:
# Consume kw_defaults for kwonly args
kw_defaults.pop(0)
else:
if param.kind in (param.POSITIONAL_ONLY, param.POSITIONAL_OR_KEYWORD):
default = defaults.pop(0)
value = get_default_value(lines, default)
Expand Down
8 changes: 6 additions & 2 deletions tests/roots/test-ext-autodoc/target/preserve_defaults.py
Expand Up @@ -8,13 +8,17 @@
def foo(name: str = CONSTANT,
sentinel: Any = SENTINEL,
now: datetime = datetime.now(),
color: int = 0xFFFFFF) -> None:
color: int = 0xFFFFFF,
*,
kwarg1,
kwarg2 = 0xFFFFFF) -> None:
"""docstring"""


class Class:
"""docstring"""

def meth(self, name: str = CONSTANT, sentinel: Any = SENTINEL,
now: datetime = datetime.now(), color: int = 0xFFFFFF) -> None:
now: datetime = datetime.now(), color: int = 0xFFFFFF,
*, kwarg1, kwarg2 = 0xFFFFFF) -> None:
"""docstring"""
6 changes: 4 additions & 2 deletions tests/test_ext_autodoc_preserve_defaults.py
Expand Up @@ -29,14 +29,16 @@ def test_preserve_defaults(app):
'',
'',
' .. py:method:: Class.meth(name: str = CONSTANT, sentinel: ~typing.Any = '
'SENTINEL, now: ~datetime.datetime = datetime.now(), color: int = %s) -> None' % color,
'SENTINEL, now: ~datetime.datetime = datetime.now(), color: int = %s, *, '
'kwarg1, kwarg2=%s) -> None' % (color, color),
' :module: target.preserve_defaults',
'',
' docstring',
'',
'',
'.. py:function:: foo(name: str = CONSTANT, sentinel: ~typing.Any = SENTINEL, '
'now: ~datetime.datetime = datetime.now(), color: int = %s) -> None' % color,
'now: ~datetime.datetime = datetime.now(), color: int = %s, *, kwarg1, '
'kwarg2=%s) -> None' % (color, color),
' :module: target.preserve_defaults',
'',
' docstring',
Expand Down

0 comments on commit 9d81843

Please sign in to comment.