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

Property gets linked instead of external type in class with inline type annotations #8992

Closed
bluetech opened this issue Mar 13, 2021 · 5 comments

Comments

@bluetech
Copy link

Describe the bug

The project is using autodoc with inline Python type annotations. Given the following class (snipped to the relevant parts):

from types import TracebackType

class ExceptionInfo(Generic[E]):
    @classmethod
    def from_exc_info(
        cls,
        exc_info: Tuple[Type[E], E, TracebackType],
        exprinfo: Optional[str] = None,
    ) -> "ExceptionInfo[E]":
        pass

    @property
    def traceback(self) -> Traceback:
        pass

The class and from_exc_info method get rendered as follows:

Screenshot_2021-03-13 API Reference — pytest documentation

In the class, the TracebackType gets rendered as traceback and is not linked.

In the method, the TracebackType gets rendered as traceback and links to the traceback property.

Expected behavior

I expect it to show as TracebackType. A traceback is also OK I guess (if it's meant to refer to a "traceback object"), but in that case it should link to https://docs.python.org/3/library/types.html#types.TracebackType or https://docs.python.org/3/reference/datamodel.html#traceback-objects instead of the local property.

To Reproduce

I can try to create a minimal reproduction if the above is not sufficient or gets outdated.

Your project

https://github.com/pytest-dev/pytest

Environment info

  • OS: Linux
  • Python version: 3.9.2
  • Sphinx version: 3.5.2
  • Sphinx extensions:
extensions = [
    "pallets_sphinx_themes",
    "pygments_pytest",
    "sphinx.ext.autodoc",
    "sphinx.ext.autosummary",
    "sphinx.ext.intersphinx",
    "sphinx.ext.todo",
    "sphinx.ext.viewcode",
    "sphinx_removed_in",
    "sphinxcontrib_trio",
]
@bluetech bluetech changed the title <what happen when you do on which document project> Property gets linked instead of external type in class with inline type annotations Mar 13, 2021
@tk0miya
Copy link
Member

tk0miya commented Mar 15, 2021

Unfortunately, the real name of types.TracebackType is traceback.

$ python
Python 3.9.1 (default, Dec 18 2020, 00:18:40)
[Clang 11.0.3 (clang-1103.0.32.59)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from types import TracebackType
>>> TracebackType
<class 'traceback'>
>>> TracebackType.__module__
'builtins'

As a result, ExceptionInfo.from_exc_info was rendered as following (internally):

   .. py:method:: ExceptionInfo.from_exc_info(exc_info: Tuple[Type[example.E], example.E, traceback], exprinfo: Optional[str] = None) -> example.ExceptionInfo[example.E]
      :module: example
      :classmethod:

After that, the "traceback" name confuses Sphinx.

@bluetech
Copy link
Author

Ah the traceback makes sense now, thanks.

So I guess the problem is that the builtins gets elided + has lesser priority than the property. The second part makes sense, but for the first part, is it viable to keep the builtins, but just not display it?

@tk0miya
Copy link
Member

tk0miya commented Mar 16, 2021

Would you like to see builtins.int or builtins.str? Of course, it's possible technically. But nobody wants to show it, I think.

@bluetech
Copy link
Author

I mean that it would become builtins.traceback in the sphinx code but rendered in HTML as just traceback. But that's just a thought, I didn't try to check how viable this is. Another complicating factor is that traceback is not actually present in the builtins module.

BTW, if it's rendered as

   .. py:method:: ExceptionInfo.from_exc_info(exc_info: Tuple[Type[example.E], example.E, traceback], exprinfo: Optional[str] = None) -> example.ExceptionInfo[example.E]
      :module: example
      :classmethod:

maybe it makes sense to just skip properties when trying to resolve types.

tk0miya added a commit to tk0miya/sphinx that referenced this issue Mar 17, 2021
…ype annotation

The builtin module, ``types.TracebackType`` does not have correct module
name.  This allows to refer it automatically.
tk0miya added a commit to tk0miya/sphinx that referenced this issue Mar 17, 2021
…ype annotation

The builtin module, ``types.TracebackType`` does not have correct module
name.  This allows to refer it automatically.
@tk0miya
Copy link
Member

tk0miya commented Mar 17, 2021

I just post #9015 to resolve the correct name of types.TracebackType.

@tk0miya tk0miya added this to the 4.0.0 milestone Mar 17, 2021
tk0miya added a commit that referenced this issue Mar 18, 2021
Fix #8992: autodoc: Failed to resolve types.TracebackType type annotation
@tk0miya tk0miya closed this as completed Mar 18, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 12, 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