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

Certain type aliases break type formatting in automodule #8200

Closed
dfremont opened this issue Sep 12, 2020 · 0 comments
Closed

Certain type aliases break type formatting in automodule #8200

dfremont opened this issue Sep 12, 2020 · 0 comments

Comments

@dfremont
Copy link
Contributor

Describe the bug
If automodule is applied to a module which defines a documented complex type alias, the type annotations on all attributes later in the module will be ignored.

To Reproduce
Apply automodule to a module like the following:

import typing

Alias = typing.Union[int, float]
"""a type alias"""

class Foo:
    """blah"""

    bar: object
    """an attribute"""

The generated documentation will not list any type for the attribute bar. Removing the definition of Alias causes the type of bar to be correctly rendered.

Expected behavior
The presence of a type alias definition should not affect the display of type information for unrelated members.

Your project
Here is a tiny example project: example3.zip

Environment info

  • OS: macOS 10.14.6
  • Python version: 3.8.0
  • Sphinx version: 3.2.1 (also reproduced on the current 3.x branch)
  • Sphinx extensions: sphinx.ext.autodoc

Additional context
The reason for the bug is that autodoc.GenericAliasDocumenter.add_directive_header and autodoc.TypeVarDocumenter.add_directive_header use the line self.options.annotation = SUPPRESS to suppress the printing of the usual type information. Unfortunately, the self.options object can be shared across multiple documenters created as part of the same automodule directive, so mutating the options for one documenter affects all the others. One way to fix this is to change autodoc.Documenter.__init__ to make a copy of the Options object, using the line self.options = Options(directive.genopt). Making this change causes the bug described above to disappear.

Thanks to @altendky for helping me debug this problem.

tk0miya added a commit to tk0miya/sphinx that referenced this issue Sep 12, 2020
The annotation option is shared between auto directives unexpectedly.
It causes supression of type annotations for objects after
GenericAlias definition.
@tk0miya tk0miya added this to the 3.3.0 milestone Sep 12, 2020
@tk0miya tk0miya closed this as completed in a555e3d Oct 4, 2020
tk0miya added a commit that referenced this issue Oct 4, 2020
…ation

Fix #8200: autodoc: type aliases break type formatting
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants