sphinx icon indicating copy to clipboard operation
sphinx copied to clipboard

autodoc: `*args` documented with a leading star loses its type with `autodoc_typehints='description'`

Open PRINCE2-AI opened this issue 1 month ago • 0 comments

Describe the bug

With autodoc_typehints = 'description' (and the default autodoc_typehints_description_target = 'all'), a *args parameter that is documented with a leading star (:param \*args: ...) loses its type entirely, even though the annotation is present on the function. A typed parameter in the same docstring keeps its type, so the field list looks inconsistent.

To Reproduce

conf.py:

extensions = ['sphinx.ext.autodoc']
autodoc_typehints = 'description'

mod.py:

def g(*args: str, a: int) -> None:
    r"""G.

    :param \*args: Star values.
    :param int a: A value.
    """

index.rst:

.. autofunction:: mod.g

Text builder output (Sphinx 9.3.0.dev, commit 1ddf198):

mod.g(*args, a)

   G.

   Parameters:
      * ***args** -- Star values.

      * **a** (*int*) -- A value.

   Return type:
      None

Expected: *args is shown as (*str*), like a is shown as (*int*).

Root cause

sphinx/ext/autodoc/typehints.py, modify_field_list():

        if '*' + name in arguments:
            name = '*' + name
            arguments.get(name)
        elif '**' + name in arguments:
            name = '**' + name
            arguments.get(name)
        else:
            arg = arguments.get(name, {})

The first two branches call arguments.get(name) without assigning the result, so arg keeps whatever value the previous field or parameter left behind. In the example above the last parsed field is :param int a:, whose entry has type: True, so if not arg.get('type') is false and no type *args field is emitted. **kwargs documented the same way is affected too, and when the stale arg comes from a different parameter the type can also be dropped for a non-starred one.

Fix

Assign the looked-up mapping in both branches:

        if '*' + name in arguments:
            name = '*' + name
            arg = arguments.get(name, {})
        elif '**' + name in arguments:
            name = '**' + name
            arg = arguments.get(name, {})

I have a regression test (a *args parameter documented with a star next to a field that carries a type) that fails on master and passes with the change, and will open a pull request referencing this issue.

PRINCE2-AI avatar Oct 28 '26 02:10 PRINCE2-AI