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 suppress signature type hints for classes/methods #7435

godlygeek opened this issue Apr 7, 2020 · 1 comment


Copy link

Describe the bug

For functions, autodoc_typehints='description' prevents the type hints from being included in the signature in the generated documentation, but for methods and classes it doesn't. This is because FunctionDocumenter.format_args does:

        if self.env.config.autodoc_typehints in ('none', 'description'):
            kwargs.setdefault('show_annotation', False)

But ClassDocumenter.format_args and MethodDocumenter.format_args do only:

        if self.env.config.autodoc_typehints == 'none':
            kwargs.setdefault('show_annotation', False)

To Reproduce

mkdir -p sphinx_signature_type_hints
cd sphinx_signature_type_hints

cat <<'EOF' >
def times_5(x: int) -> int:
    """Multiplies *x* by 5.

    :param x: Value to multiply by 5
    :returns: *x* * 5

class Multiplier:
    """Functor for multiplying by a constant.

    :param factor: Factor to multiply by.
    def __init__(self, factor: int) -> None:
        self._factor = factor

    def multiply(self, x: int) -> int:
        """Multiplies *x* by this object's *factor*.

        :param x: Value to multiply
        :returns: *x* * *factor*
        return x * self._factor

mkdir -p docs

cat <<'EOF' >docs/
extensions = ["sphinx.ext.autodoc"]
autodoc_typehints = 'description'

cat <<'EOF' >docs/index.rst
.. automodule:: type_hint_test

mkdir -p html
python3.8 -m sphinx -vvv -W -b html --keep-going docs html

Expected behavior

autodoc_typehints='description' doesn't put type hints into the signature; only autodoc_typehints='signature' does.

Environment info

  • OS: Linux 4.4.0
  • Python version: 3.8.1
  • Sphinx version: 3.0.0
  • Sphinx extensions: sphinx.ext.autodoc, sphinx.ext.intersphinx

Additional context

The generated reST from the above reproducer is:

.. py:module:: type_hint_test

.. py:class:: Multiplier(factor: int)
   :module: type_hint_test

   Functor for multiplying by a constant.

   :param factor: Factor to multiply by.

   .. py:method:: Multiplier.multiply(x: int) -> int
      :module: type_hint_test

      Multiplies *x* by this object's *factor*.

      :param x: Value to multiply
      :returns: *x* * *factor*

.. py:function:: times_5(x)
   :module: type_hint_test

   Multiplies *x* by 5.

   :param x: Value to multiply by 5
   :returns: *x* * 5

Note that the py:class and py:method have type hints in the signature, but the py:function doesn't.

@tk0miya tk0miya added this to the 3.0.1 milestone Apr 8, 2020
Copy link

tk0miya commented Apr 8, 2020

OMG. I'll fix this in next release.

tk0miya added a commit to tk0miya/sphinx that referenced this issue Apr 8, 2020
tk0miya added a commit that referenced this issue Apr 9, 2020

Fix #7435: autodoc_typehints doesn't suppress typehints for classes/methods
@tk0miya tk0miya closed this as completed Apr 9, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 26, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet

No branches or pull requests

2 participants