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

TypeVar mentioned in type annotation is not found, causing an unexpected warning on Sphinx 3.x #7722

Closed
pquentin opened this issue May 25, 2020 · 3 comments

Comments

@pquentin
Copy link

Describe the bug

TypeVar mentioned in type annotation is not found, causing an unexpected warning on Sphinx 3.x

To Reproduce

Write conf.py as follows:

import os
import sys

sys.path.insert(0, os.path.abspath('.'))
extensions = ['sphinx.ext.autodoc']

Write index.rst as follows:

Welcome to repro's documentation!
=================================

.. autoclass:: repro.SendChannel
   :members:

Write repro.py as follows:

from typing import TypeVar

SendType = TypeVar("SendType")


class SendChannel():
    async def send(self, value: SendType):
        """send"""
        pass

Run the following command:

sphinx-build -nW -b html . _build/html

Expected behavior

Like with Sphinx 2.x, the SendType type hint is ignored.

Actual behavior

The build fails, since we treat warnings as errors:

docstring of repro.SendChannel.send::py:class reference target not found: SendType

Environment info

  • OS: macOS 10.15 and Linux
  • Python version: 3.8.2
  • Sphinx version: 3.1.0+ (302c23f)
  • Sphinx extensions: sphinx.ext.autodoc

Additional context
Add any other context about the problem here.

@pquentin
Copy link
Author

I just ran git bisect, and realized that this issue is a duplicate of #7493. Closing

@tk0miya tk0miya reopened this May 26, 2020
@tk0miya
Copy link
Member

tk0miya commented May 26, 2020

I think this is similar to #7493, but not the same problem.
The warning was caused from that the type annotation SendType is not documented. So you need to make a document for it by .. autoclass:: repro.SendType. But autoclass for TypeVar will generate a long text document unexpectedly. It comes from a docstring of TypeVar class. In addition, autodoc generates a broken type annotation reference for TypeVars. In this case, the absolute pathname of the annotation should be repro.SendType. But it refers SendType instead. So it will be never resolved.

@tk0miya tk0miya added this to the 3.1.0 milestone May 26, 2020
@tk0miya tk0miya modified the milestones: 3.1.0, 3.2.0 Jun 4, 2020
tk0miya added a commit to tk0miya/sphinx that referenced this issue Jun 5, 2020
tk0miya added a commit to tk0miya/sphinx that referenced this issue Jun 6, 2020
tk0miya added a commit that referenced this issue Jun 6, 2020
@tk0miya
Copy link
Member

tk0miya commented Jul 12, 2020

I noticed #7930 also fixed this case. It helps to resolve the location of SendType.

I confirmed the fix with adding a comment to SendType and using the HEAD of 3.x branch:

from typing import TypeVar

#: SendType
SendType = TypeVar("SendType")


class SendChannel():
    async def send(self, value: SendType):
        """send"""
        pass

@tk0miya tk0miya closed this as completed Jul 12, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 22, 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