You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When documenting this with autodoc and autodoc_preserve_defaults=True, sphinx.ext.autodoc.preserve_defaults.update_defvalue generates a DefaultValue with name=None for the kw_with_default arguments. This later raises an exception in sphinx.util.inspect.object_description since the DefaultValue.__repr__ dunder method now returns None instead of a string.
Basically what happens is that ast generates a None value in the kw_defaults of the arguments since the first keyword argument is required, but update_defvalue simply ignores that argument because the default is empty. This leaves the None in the kw_defaults to be picked up when the keyword argument with default value is processed -- instead of the actual default.
This can't be resolved by the unparse call which therefore simply returns None, which ends up as the name of the DefaultValue.
Imo this could simply be resolved by poping the corresponding None from the kw_defaults if a KW_ONLY parameter with empty default is encountered.
and auto-document while setting autodoc_preserve_defaults=True in your conf.py
Make sure sphinx tries to document all parameters, (since it's a __init__ method, they will be documented when the autodoc directive has :undoc-members:, if you try the same with a module level method you need to document the parameters)
The correct default value should be documented. The Warning Message also is pretty worthless (probably the value should not be
formatted with a simple %s but instead with a %r?)
Describe the bug
If I understand PEP 0570 correctly, the following is a valid signature of a class method:
When documenting this with autodoc and
autodoc_preserve_defaults=True
,sphinx.ext.autodoc.preserve_defaults.update_defvalue
generates aDefaultValue
withname=None
for thekw_with_default
arguments. This later raises an exception insphinx.util.inspect.object_description
since theDefaultValue.__repr__
dunder method now returnsNone
instead of a string.Basically what happens is that ast generates a
None
value in thekw_defaults
of thearguments
since the first keyword argument is required, butupdate_defvalue
simply ignores that argument because thedefault
is empty. This leaves theNone
in thekw_defaults
to be picked up when the keyword argument with default value is processed -- instead of the actual default.This can't be resolved by the
unparse
call which therefore simply returnsNone
, which ends up as thename
of theDefaultValue
.Imo this could simply be resolved by
pop
ing the correspondingNone
from thekw_defaults
if aKW_ONLY
parameter with emptydefault
is encountered.How to Reproduce
Create a module with contents
and auto-document while setting
autodoc_preserve_defaults=True
in yourconf.py
Make sure sphinx tries to document all parameters, (since it's a
__init__
method, they will be documented when the autodoc directive has:undoc-members:
, if you try the same with a module level method you need to document the parameters)test.zip
Expected behavior
The correct default value should be documented. The Warning Message also is pretty worthless (probably the value should not be
formatted with a simple
%s
but instead with a%r
?)Your project
https://github.com/sphinx-doc/sphinx/files/8253301/test.zip
OS
Any
Python version
Tested with versions > 3.8
Sphinx version
4.4.0
Sphinx extensions
sphinx.ext.autodoc
The text was updated successfully, but these errors were encountered: