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
AttributeError
when using X | Y type hinting syntax with sphinx.ext.autodoc
#8775
Comments
autodoc uses python interpreter itself to recognize type hints. So you need to run Sphinx under python 3.10 to recognize union types operator. |
@tk0miya Thank you for your response. This is a topic I'm not super familiar with but my hope was that as the type checking works under 3.9.1 (given the |
No, it is not related to your python interpreter. mypy-0.800 supports the new Union syntax, not python itself. So it will also work when you're using new mypy even with python-3.8. autodoc can support the syntax if somebody sends us a patch! |
Upgrade annotation parser for python domain to support type union operator introduced in PEP-604. It's available on all python interpreters.
Upgrade autodoc to support type union operator introduced in PEP-604. It's available only with python 3.10+.
Upgrade autodoc to support type union operator introduced in PEP-604. It's available only with python 3.10+.
Great, and thanks for the work I can see already on this from you @tk0miya . Your work on this project is a gift 🙏 |
…operator The type union operator (PEP-604) causes autodoc crashed in python 3.9 or below because of the syntax is not suppoerted yet in the interpreters. Internally, `typing.get_type_hints()` raises TypeError on evaluating the annotation by BitOr operator for types. To avoid the crash, this adds a fallback not to evaluate the annotations and keep as is. As a side effect, `autodoc_type_aliases` will not work for the modules and classes that uses type union operator for their annotations.
Now I posted #8805 to avoid the crash by union types operator. But it does not mean Sphinx supports it with Python 3.9 or below. I think it is a workaround because it disables |
Upgrade annotation parser for python domain to support type union operator introduced in PEP-604. It's available on all python interpreters.
autodoc: Support type union operator (PEP-604) (refs: #8775)
py domain: Support type union operator (PEP-604) (refs: #8775)
Fix #8775: Avoid the crash of autodoc caused by type union operator
Describe the bug
Using type syntax like
name: str | int
is now supported in Python (with an import from__future__
) butautodoc
-ing a class which uses this syntax produces anAttributeError
.To Reproduce
I have the following sample project:
Python 3.9.1
docs/source/conf.py
:docs/source/index.rst
:example_module.py
:Command:
Expected behavior
No error.
Sphinx builds documentation which makes it clear that
name
can be anint
or astr
.Environment info
sphinx.ext.autodoc
The text was updated successfully, but these errors were encountered: