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 doesn't handle TypeVars or document user-defined generic classes accurately #8219

Closed
anselor opened this issue Sep 17, 2020 · 7 comments
Labels
Milestone

Comments

@anselor
Copy link

anselor commented Sep 17, 2020

Describe the bug

  1. TypeVar aliases aren't resolved
    When using automodule, if a TypeVar is used, the TypeVar isn't being recognized. With nitpick on, this warning is generated:
    helloworld.py:docstring of helloworld.helloworld.MyGenericType.foo:: WARNING: py:class reference target not found: MyTypeVar
    I can add it to the nitpick ignore list, but then all of the parameters that reference it aren't recognized and linked.

  2. typing.Generic type parameters for user-defined generic classes aren't documented

  3. Constructor for user-defined generic classes are always (*args, **kwargs) instead of what's actually declared.

To Reproduce

Simple doc file index.rst:

Hello World
===========

code docs
=========

.. automodule:: helloworld.helloworld
    :members:
    :undoc-members:
    :show-inheritance:

Here's an example module helloworld.py:

from typing import Generic, TypeVar

MyTypeVar = TypeVar('MyTypeVar')

class MyGenericType(Generic[MyTypeVar]):
    def __init__(self, param: MyTypeVar) -> None:
        pass

    def foo(self, param: MyTypeVar) -> None:
        pass

Expected behavior
Ideally:

  1. I would expect TypeVar definitions to be recognized as types
  2. Generic classes should document the TypeVars passed to typing.Generic
  3. Constructor for generic classes should reflect the parameters declared in the constructor.

Work-around for Item 1

This is less than ideal, but I can get the TypeVar to resolve by manually documenting it as an attribute like this:

.. py:attribute:: MyTypeVar
   :type: TypeVar
   :value: TypeVar('MyTypeVar')

This is the type used in MyGenericClass

.. automodule:: helloworld.helloworld
    :members:
    :undoc-members:
    :show-inheritance:

Your project
I've included a simple example above

Screenshots
Here's the generated documentation:
image

Here's the generated output with the manual attribute work-around:
image

Environment info

  • OS: Ubuntu 18.04, Ubuntu 20.04
  • Python version: 3.6.9, 3.8.2
  • Sphinx version: 3.1.2, 3.2.1
  • Sphinx extensions: sphinx.ext.autodoc
  • Extra tools: Viewing output on Chrome

Additional context

@electric-coder
Copy link

electric-coder commented Sep 19, 2020

Functional solution was demonstrated at Stack Overflow.

EDIT:

It worked for me due to old caches. After invalidating caches I confirm the problem in the question of the typing.Generic not being correctly displayed in the class inheritance signature does occur.

Environment

Windows 10
Python 3.8.0 + Sphinx 3.2.1
Python 3.7.7 + Sphinx 3.1.1

@anselor
Copy link
Author

anselor commented Sep 19, 2020

I also added the additional options described in the Stack Overflow link and also continue to have this issue. I'm using the alabaster theme, but I don't think that matters.

I also added this to conf.py to force __init__() to be documented and the parameters are correctly resolved on __init__()

def skip(app, what, name, obj, would_skip, options):
    if name == '__init__':
        return False
    return would_skip


def setup(app):
    app.connect('autodoc-skip-member', skip)

tk0miya added a commit to tk0miya/sphinx that referenced this issue Sep 21, 2020
…lass is incorrect

Inogre the signature of typing.Generic.__new__() to get correct a
signature of the constructor from a subclass.
tk0miya added a commit to tk0miya/sphinx that referenced this issue Sep 21, 2020
…lass is incorrect

Ignore the signature of typing.Generic.__new__() to get correct a
signature of the constructor from a subclass.
@tk0miya
Copy link
Member

tk0miya commented Sep 21, 2020

  1. I would expect TypeVar definitions to be recognized as types

Please add a comment (starting with a colon) to the typevar. Then autodoc detects it automatically.

#: blah blah blah
MyTypeVar = TypeVar('MyTypeVar')
  1. Constructor for generic classes should reflect the parameters declared in the constructor.

Now I posted #8229 to fix this.

  1. Generic classes should document the TypeVars passed to typing.Generic

I need time to fix this.

Note: The arguments of Generic are lost in __mro__. So we need to refer __orig_bases__ instead.
see https://www.python.org/dev/peps/pep-0560/ in detail.

@anselor
Copy link
Author

anselor commented Sep 28, 2020

I've not seen the #: comment syntax before. That appears to have addressed that issue. Thank you.

@tk0miya
Copy link
Member

tk0miya commented Oct 2, 2020

It has been described at autoattribute's document.
https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#directive-autoattribute

tk0miya added a commit to tk0miya/sphinx that referenced this issue Oct 4, 2020
@anselor
Copy link
Author

anselor commented Oct 24, 2020

I'm still having issues with the first item.

It looks like if my module is in a package, the reference to the typevar doesn't preserve the package name of the typevar alias and is unable to locate it.

If I manually create a attribute in the global scope that matches the name of the typevar alias, then autodoc is able to locate that manually defined attribute.

tk0miya added a commit to tk0miya/sphinx that referenced this issue Nov 3, 2020
tk0miya added a commit to tk0miya/sphinx that referenced this issue Nov 3, 2020
tk0miya added a commit to tk0miya/sphinx that referenced this issue Nov 3, 2020
tk0miya added a commit to tk0miya/sphinx that referenced this issue Nov 3, 2020
tk0miya added a commit to tk0miya/sphinx that referenced this issue Nov 3, 2020
@tk0miya tk0miya added this to the 3.4.0 milestone Nov 3, 2020
@tk0miya
Copy link
Member

tk0miya commented Nov 3, 2020

@anselor Could you file another issue with detailed information? It would be very helpful if there is an example code!

tk0miya added a commit to tk0miya/sphinx that referenced this issue Nov 3, 2020
@tk0miya tk0miya closed this as completed in e2bf916 Nov 8, 2020
tk0miya added a commit that referenced this issue Nov 8, 2020
…ass_is_not_shown

Fix #8219: autodoc: Parameters for generic base class are not shown
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants