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

Fix duplicated *args and **kwargs with autodoc_typehints #9648

Closed
wants to merge 5 commits into from

Conversation

AntoineD
Copy link

@AntoineD AntoineD commented Sep 17, 2021

Fix duplicated *args and **kwargs with autodoc_typehints

Bugfix

  • Bugfix

Detail

Consider this

class _ClassWithDocumentedInitAndStarArgs:
    """Class docstring."""

    def __init__(self, x: int, *args: int, **kwargs: int) -> None:
        """Init docstring.

        :param x: Some integer
        :param *args: Some integer
        :param **kwargs: Some integer
        """

when using the autodoc extension and the setting autodoc_typehints = "description".

WIth sphinx 4.2.0, the current output is

Class docstring.

   Parameters:
      * **x** (*int*) --

      * **args** (*int*) --

      * **kwargs** (*int*) --

   Return type:
      None

   __init__(x, *args, **kwargs)

      Init docstring.

      Parameters:
         * **x** (*int*) -- Some integer

         * ***args** --

           Some integer

         * ****kwargs** --

           Some integer

         * **args** (*int*) --

         * **kwargs** (*int*) --

      Return type:
         None

where the *args and **kwargs are duplicated and incomplete.

The expected output is

  Class docstring.

   Parameters:
      * **x** (*int*) --

      * ***args** (*int*) --

      * ****kwargs** (*int*) --

   Return type:
      None

   __init__(x, *args, **kwargs)

      Init docstring.

      Parameters:
         * **x** (*int*) -- Some integer

         * ***args** (*int*) --

           Some integer

         * ****kwargs** (*int*) --

           Some integer

      Return type:
         None

@@ -30,7 +36,9 @@ def record_typehints(app: Sphinx, objtype: str, name: str, obj: Any,
sig = inspect.signature(obj, type_aliases=app.config.autodoc_type_aliases)
for param in sig.parameters.values():
if param.annotation is not param.empty:
annotation[param.name] = typing.stringify(param.annotation)
prefix = __ANNOTATION_KIND_TO_PARAM_PREFIX.get(param.kind, '')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change will cause the :param: definition without prefixes (ex. args, kwargs). So this change is not good. I think it would be better to change modify_field_list() and augment_descriptions_with_types() instead (without modifying this annotation data).

@tk0miya
Copy link
Member

tk0miya commented Oct 31, 2021

I noticed this docstring causes warnings because * and ** are considered as mark-up symbols:

    def __init__(self, x: int, *args: int, **kwargs: int) -> None:
        """Init docstring.

        :param x: Some integer
        :param *args: Some integer
        :param **kwargs: Some integer
        """

Here are warnings:

/Users/tkomiya/work/tmp/doc/example.py:docstring of example.ClassWithDocumentedInitAndStarArgs:6: WARNING: Inline emphasis start-string without end-string.
/Users/tkomiya/work/tmp/doc/example.py:docstring of example.ClassWithDocumentedInitAndStarArgs:7: WARNING: Inline strong start-string without end-string.

It will work fine if we escape * character like the following. But it's not officially recommended way, I believe.

    def __init__(self, x: int, *args: int, **kwargs: int) -> None:
        """Init docstring.

        :param x: Some integer
        :param \*args: Some integer
        :param \*\*kwargs: Some integer
        """

I'm not sure this feature is really needed?

@tk0miya tk0miya modified the milestones: 4.3.0, 4.4.0 Nov 9, 2021
@AntoineD
Copy link
Author

I noticed this docstring causes warnings because * and ** are considered as mark-up symbols:

    def __init__(self, x: int, *args: int, **kwargs: int) -> None:
        """Init docstring.

        :param x: Some integer
        :param *args: Some integer
        :param **kwargs: Some integer
        """

Here are warnings:

/Users/tkomiya/work/tmp/doc/example.py:docstring of example.ClassWithDocumentedInitAndStarArgs:6: WARNING: Inline emphasis start-string without end-string.
/Users/tkomiya/work/tmp/doc/example.py:docstring of example.ClassWithDocumentedInitAndStarArgs:7: WARNING: Inline strong start-string without end-string.

It will work fine if we escape * character like the following. But it's not officially recommended way, I believe.

    def __init__(self, x: int, *args: int, **kwargs: int) -> None:
        """Init docstring.

        :param x: Some integer
        :param \*args: Some integer
        :param \*\*kwargs: Some integer
        """

I'm not sure this feature is really needed?

This is needed for the Numpy and Google docstring formats, which napoleon converts to :param:s.

@tk0miya
Copy link
Member

tk0miya commented Nov 17, 2021

Oh, I missed numpydoc format. Indeed, it recommends prepending stars.
https://numpydoc.readthedocs.io/en/latest/format.html#parameters

@tk0miya tk0miya modified the milestones: 4.4.0, 4.5.0 Jan 16, 2022
@tk0miya tk0miya modified the milestones: 4.5.0, 5.0.0 Mar 27, 2022
tk0miya added a commit to tk0miya/sphinx that referenced this pull request May 15, 2022
In basic usage of autodoc (docstring), `args` and `kwargs` arguments
are marked up without stars.  But numpydoc style recommends to mark
them up with stars.

This adds support for starred arguments in docstrings to
`autodoc_typehints` feature.
tk0miya added a commit to tk0miya/sphinx that referenced this pull request May 15, 2022
In basic usage of autodoc (docstring), `args` and `kwargs` arguments
are marked up without stars.  But numpydoc style recommends to mark
them up with stars.

This adds support for starred arguments in docstrings to
`autodoc_typehints` feature.
tk0miya added a commit that referenced this pull request May 22, 2022
…tion_and_stared_args

Fix #9648: autodoc: *args and **kwargs entries are duplicated
@tk0miya
Copy link
Member

tk0miya commented May 22, 2022

I merged #10451 instead of this. Thank you for your proposal and contribution!

@tk0miya tk0miya closed this May 22, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants