Skip to content

Generator docstring emitting both return type and yield type #419

Open
@masaccio

Description

@masaccio

Using Google-style docstrings with sphinx.ext.napoleon and sphinx_autodoc_typehints, I see both a Yields: row and a Return type: row in the Parameters table for generator methods.

Reproducer:

docs/index.rst:

.. currentmodule:: foo

.. autoclass:: Foo
   :members:

docs/conf.py:

import os
import sys

extensions = [
    "sphinx.ext.autodoc",
    "sphinx.ext.napoleon",
    "sphinx_autodoc_typehints",
]

master_doc = "index"

sys.path.insert(0, os.path.abspath("../"))

foo/__init.py:

from typing import Optional, Generator

class Foo:
    def func(
        self,
        arg1: Optional[int] = None,
        arg2: Optional[int] = None,
    ) -> Generator[tuple, None, None]:
        """Summary of the func

        A sligntly longer description

        Args:
            arg1: this is argument 1
            arg2: this is argument 2

        Yields:
            tuple: strings of things

        Raises:
            IndexError:
                If bad stuff happens
        """
        pass

Output from sphinx-build -a -b html docs docs/build:

Screenshot 2024-01-23 at 07 19 56

Removing sphinx_autodoc_typehints results in the following output, but obviously loses the type hints in the parameters table:

Screenshot 2024-01-23 at 07 20 42

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions