New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make autodoc_typehints="description" work with autoclass_content="class" #8539
Make autodoc_typehints="description" work with autoclass_content="class" #8539
Conversation
9d055bc
to
67737ba
Compare
I feel the requirements of |
Alright, I can see why you might feel that way. But, if you'll allow me to make the argument, can you see how it could instead be construed as a bug fix, in a feature that is quite new and that has never yet worked correctly?
class SomeClass:
"""My Class"""
def __init__(self, foo: str) -> None:
... then
def some_func(n: int, *, _private_state: int=42):
"""Some func""" and document it with: .. automodule:: module
.. autofunction:: some_func(n) then only the I think there's a compelling argument to be made that the current behavior is buggy, and that fixing the bugs is more valuable than maintaining backwards compatibility. Do we at least agree that neither of these behaviors is what users would want, even if you believe they should be maintained for the sake of backwards compatibility? |
If we do agree that neither of these behaviors is desirable, but you still feel that they must be maintained, perhaps we can add new options that fix these issues, and make those options the default in the next major version? |
I think it is enough worthy to generate |
OK, that seems fair enough, and I wouldn't mind creating an option to control whether or not But, then we're back at square one about the one that's definitely a bug. If we generate the If my proposal to only generate What do you think? |
As you said at beginning of this PR, it is a better way to add |
So if I understand you correctly, you would be happy with a solution where a new option is introduced that enables the behavior I propose here (a If that's the case I can try to find some time to work on this and make that change. Do you want the option to default to the backwards compatible behavior ( |
Yes, I prefer that the default behavior is to convert type annotations to info-field-list unconditionally. |
b209f4e
to
79b5f45
Compare
Sorry, this fell of my radar for a while. I've updated the PR to add a new option, |
doc/usage/extensions/autodoc.rst
Outdated
@@ -548,6 +548,19 @@ There are also config values that you can set: | |||
|
|||
New option ``'description'`` is added. | |||
|
|||
.. confval:: autodoc_typehint_undoc |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the late response. I missed your updates.
Now I'm thinking about the name of this option. I feel this option does not describe its behavior well. If my understanding is correct, it controls the target of the typehints generation to either all of parameters or documented parameters only when autodoc_typehints = "description"
.
So I propose you the alternatives:
autodoc_typehints_description
autodoc_typehints_description_target
autodoc_typehints_description_params
And it takes "all"
or "documented"
("documented_only"
?) to change the behavior. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I'm fine with that. I've updated the PR to call it autodoc_typehints_description_target
and have it take either "all"
or "documented"
.
79b5f45
to
60afdf7
Compare
Previously, if autodoc_typehints="description", a :type: field would be added for every parameter and return type appearing in the annotation, including **kwargs and underscore-prefixed parameters that are meant to be private, as well as None return types. This commit introduces a new option, "autodoc_typehint_undoc". By default this option is True, requesting the old behavior. By setting this option to False, :type: and :rtype: fields will only be added for annotated parameters or return types if there is already a corresponding :param: or :return: field, to put users in control over whether a given parameter is documented or not.
Previously, `__init__` type hints were not used when documenting a class using `autodoc_typehints="description"`. This was done to prevent documentation for parameters from showing up twice, both for the class and the `__init__` special method. As the new ``autodoc_typehint_undoc`` option provides a better way to prevent this bad behavior by placing the user in control of where the type hints are added, it is now safe to add type hints for documented `__init__` parameters. Closes sphinx-doc#8178
Add new tests to exercise the new autodoc_typehint_undoc option. Where an existing test would have a meaningful behavior change with the new option set to False, that test is copied, the new option is set to False in the copy, and the assertions reflect the new expected behavior. The new test test_autodoc_typehints_description_with_documented_init illustrates the problem reported in sphinx-doc#7329, and the new test test_autodoc_typehints_description_with_documented_init_no_undoc illustrates that this issue no longer occurs when the new autodoc_typehint_undoc option is set to False.
60afdf7
to
4c72848
Compare
And I've now rebased it on |
Merged. Thank you for your great work! |
Subject: Make autodoc_typehints="description" work with autoclass_content="class"
Feature or Bugfix
Purpose
After the fix for #7329,
autodoc_typehints="description"
does not automatically set the type for class constructor arguments whenautoclass_content="class"
. This fixed a bad behavior: the parameters were being documented twice, once for the class docstring and once for the__init__
docstring.This PR introduces a better fix for #7329.
Detail
This PR removes the restriction that
__init__
's type annotation will not be used to document parameters in the class docstring whenautoclass_content="class"
, and it instead implements a new rule. After this PR,autodoc_typehints="description"
will only insert a:type:
field if the object's description already contains a:param:
for the same parameter, and likewise will only insert an:rtype:
field if there is already a:return:
field.Not only does this fix #7329 while still allowing description-mode typehints to work for classes by default, it also fixes several other annoyances with description-mode typehints: undocumented, private parameters like
_cache: Dict[str, Any]={}
are no longer added automatically, nor are**kwargs
added as a single parameter (allowing the user to document the accepted keyword arguments individually, along with their individual types).I've based the PR on 3.x as I believe this behavior change is a bugfix rather than a breaking change, but I can rebase on master if you prefer.
Relates