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
Foo = NewType(...) is treated as data and not as a class #10480
Comments
Simplified single file reproducer (run from pathlib import Path
import shutil
from sphinx.cmd.make_mode import run_make_mode
Path("bug.py").write_text('''\
"""Module docstring."""
from typing import NewType
Foo = NewType("Foo", str)
def foo_to_str(foo: Foo) -> str:
"""Convert to string."""
return foo
''', encoding="utf-8")
Path("conf.py").write_text('''\
extensions = ['sphinx.ext.autodoc']
nitpicky = True
''', encoding="utf-8")
Path("index.rst").write_text('''\
Bug
---
.. automodule:: bug
:members:
''', encoding="utf-8")
shutil.rmtree("_build", ignore_errors=True)
run_make_mode(["html", ".", "_build"]) |
@tk0miya in #8465 you made It seems it would be easier to update A |
I agree that autodoc should generate |
After a few hours of digging through what's happening with my documentation generation I finally tracked it down to this problem and then found this. |
Ok, so adding this hack to my conf.py seems to make NewTypes resolve and gets rid of all of the warnings referencing NewType definitions:
I've having the same problem with TypeVar declarations - it's also being treated as data. |
@AA-Turner Since it's the same type of issue, can TypeVar also be changed to be |
Describe the bug
Saying
Foo = NewType(...)
storedFoo
asdata
and not asclass
. When usingFoo
in a type annotation,sphinx
searches for it with theobjtypes
class
andexception
but fails to find it since it is stored asdata
, so it generates a warning that the reference target is not found.How to Reproduce
The
docs/ERRORS
file contains sample result, including the warning line:Expected behavior
sphinx
should be able to figure out the type references and emit the right links without warnings when usingNewType
.Your project
https://github.com/orenbenkiki/sphinx-bug
Screenshots
No response
OS
Linux
Python version
3.10
Sphinx version
4.5.0 (latest PyPI version)
Sphinx extensions
sphinx.ext.autodoc sphinx.ext.autosectionlabel sphinx.ext.viewcodesphinx.ext.todo
Extra tools
No response
Additional context
One brutal way to fix this is to add
data
to theobjtypes
when searching for aclass
, that is, in line 1196 ofdomains/python.py
, change the line:To:
But that is not the right way to solve the problem - it was helpful in figuring out what the problem was, though.
The text was updated successfully, but these errors were encountered: