Skip to content
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

autodoc_typehints="description" doesn't use __init__ type hints #8178

Closed
godlygeek opened this issue Sep 6, 2020 · 2 comments · Fixed by #8539
Closed

autodoc_typehints="description" doesn't use __init__ type hints #8178

godlygeek opened this issue Sep 6, 2020 · 2 comments · Fixed by #8539
Labels

Comments

@godlygeek
Copy link
Contributor

Describe the bug
Type hints attached to the __init__ method are not used when autodoc_typehints="description", but are used when autodoc_typehints="signature".

To Reproduce
Create module.py with these contents:

class Person(object):
    """Represent a person.

    Args:
        name: The person's name
    """
    def __init__(self, name: str) -> None:
        self.name = name

    def greet(self, salutation: str) -> None:
        """Print a custom greeting to this person.

        Args:
            salutation: The words to begin the greeting.
        """
        print(salutation + ", " + self.name + "!")

Create index.rst with these contents:

.. automodule:: module
   :members:

Generate documentation into an html directory:

python3.8 -msphinx -aE -C -D 'extensions=sphinx.ext.autodoc,sphinx.ext.napoleon' -D autodoc_typehints=description . html

Expected behavior
The name parameter of the Person constructor should have a (str) type annotation, like the salutation parameter of greet does. When autodoc_typehints="signature", the signature does include the : str type annotation. Adding -D autoclass_content=both causes the type hint to be used, but:

  1. I believe the type hint should be used even for autoclass_content="class" like it is if autodoc_typehints="signature", and
  2. Using autoclass_content=both causes a Return type: None annotation to be added, which is not helpful for a constructor and which doesn't match the behavior of autodoc_typehints="signature" (there's no -> None in that case).

Environment info

  • OS: Linux
  • Python version: 3.8.5
  • Sphinx version: 3.2.1
  • Sphinx extensions: sphinx.ext.autodoc and sphinx.ext.napoleon

Additional context
This appears to be intentional behavior as it was the fix for #7329, but I believe it is incorrect because it is inconsistent with how signature type hints are handled.

@godlygeek
Copy link
Contributor Author

godlygeek commented Sep 9, 2020

The bug reported in #7329 was that, when autodoc_typehints="description", autoclass_content="class", and __init__ is documented separately from the class because special-members includes __init__, the constructor parameters would be duplicated in both the class documentation and the __init__ method documentation, and the documentation for the parameters attached to the class docstring would be missing the parameter description and have only the type. This happened because autodoc_typehints="description" will add parameters with annotations to the documentation even if they weren't already present in the docstring.

The fix that was applied makes it so that autodoc_typehints="description" will never modify a class docstring if autoclass_content="class".

This means that autodoc_typehints="description" is broken when autoclass_content="class" (the default) and arguments are documented in the class docstring (as both the Google and Numpy style docstrings do).

I suggest instead changing sphinx.ext.autodoc.typehints.modify_field_list to never add a :param:, only add a :type: for parameters with a pre-existing :param:, and to likewise only add an :rtype: if there was already a :return:. This would fix several issues:

  • The bug noted in autodoc_typehints='description' does not combine well with autoclass_content='class' #7329 will be fixed: only the __init__ docstring has a :param: in it, so the class description will be left unchanged by autodoc_typehints
  • It will now be possible to have parameters that have type hints, but that are not shown in the documentation. I've been surprised in the past when an entry got added to my documentation for kwargs with only a declared type and no description; this would fix that.
  • A function that returns None will no longer have Return type: None added unconditionally. While this is not incorrect, it is distracting, adds little value, and takes up valuable space.

The fix for #7329 fixed one special case of autodoc_typehints="description" documenting only a parameter's type without any description. This proposal would fix the general case by putting the user in control of which parameters are documented and reenabling automatic :type: documentation for constructor parameters.

godlygeek added a commit to godlygeek/sphinx that referenced this issue Dec 15, 2020
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, but that will no
longer occur now that parameter and return types are only inserted if
the parameter or return has a description.

Closes sphinx-doc#8178
godlygeek added a commit to godlygeek/sphinx that referenced this issue Dec 15, 2020
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, but that will no
longer occur now that parameter and return types are only inserted if
the parameter or return has a description.

Closes sphinx-doc#8178
godlygeek added a commit to godlygeek/sphinx that referenced this issue Feb 8, 2021
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
godlygeek added a commit to godlygeek/sphinx that referenced this issue Feb 8, 2021
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
godlygeek added a commit to godlygeek/sphinx that referenced this issue Mar 27, 2021
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
godlygeek added a commit to godlygeek/sphinx that referenced this issue Mar 27, 2021
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
@godlygeek
Copy link
Contributor Author

Addressed by #8539, which will ship as part of 4.0

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant