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

AttributeError when using X | Y type hinting syntax with sphinx.ext.autodoc #8775

Closed
adamtheturtle opened this issue Jan 27, 2021 · 5 comments

Comments

@adamtheturtle
Copy link

adamtheturtle commented Jan 27, 2021

Describe the bug

Using type syntax like name: str | int is now supported in Python (with an import from __future__) but autodoc-ing a class which uses this syntax produces an AttributeError.

To Reproduce

I have the following sample project:

Python 3.9.1

.
├── docs
│   └── source
│       ├── conf.py
│       └── index.rst
└── example_module.py

docs/source/conf.py:

import os
import sys

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

docs/source/index.rst:

.. autoclass:: example_module.Foo
   :members:
   :undoc-members:

example_module.py:

from __future__ import annotations

class Foo:
    # See http://mypy-lang.blogspot.com/2021/01/mypy-0800-released.html
    # for a description of this syntax.
    name: str | int

Command:

sphinx-build -b html docs/source/ docs/build

Expected behavior

No error.
Sphinx builds documentation which makes it clear that name can be an int or a str.

Environment info

  • OS: macOS 11.1
  • Python version: 3.9.1
  • Sphinx version: 3.4.3
  • Sphinx extensions: sphinx.ext.autodoc
@tk0miya
Copy link
Member

tk0miya commented Jan 28, 2021

autodoc uses python interpreter itself to recognize type hints. So you need to run Sphinx under python 3.10 to recognize union types operator.

@adamtheturtle
Copy link
Author

@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 __future__ import), so would autodoc.

@tk0miya
Copy link
Member

tk0miya commented Jan 31, 2021

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!

tk0miya added a commit to tk0miya/sphinx that referenced this issue Jan 31, 2021
Upgrade annotation parser for python domain to support type union
operator introduced in PEP-604.  It's available on all python
interpreters.
tk0miya added a commit to tk0miya/sphinx that referenced this issue Jan 31, 2021
Upgrade autodoc to support type union operator introduced in
PEP-604. It's available only with python 3.10+.
tk0miya added a commit to tk0miya/sphinx that referenced this issue Jan 31, 2021
Upgrade autodoc to support type union operator introduced in
PEP-604. It's available only with python 3.10+.
@adamtheturtle
Copy link
Author

Great, and thanks for the work I can see already on this from you @tk0miya . Your work on this project is a gift 🙏

tk0miya added a commit to tk0miya/sphinx that referenced this issue Jan 31, 2021
…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.
@tk0miya
Copy link
Member

tk0miya commented Jan 31, 2021

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 autodoc_type_alises feature when the error is raised internally. I don't know it's really worthy. But it would work well for some people. Thanks,

@tk0miya tk0miya added this to the 3.5.0 milestone Jan 31, 2021
tk0miya added a commit to tk0miya/sphinx that referenced this issue Feb 1, 2021
Upgrade annotation parser for python domain to support type union
operator introduced in PEP-604.  It's available on all python
interpreters.
tk0miya added a commit that referenced this issue Feb 1, 2021
autodoc: Support type union operator (PEP-604) (refs: #8775)
tk0miya added a commit that referenced this issue Feb 1, 2021
py domain: Support type union operator (PEP-604) (refs: #8775)
@tk0miya tk0miya closed this as completed in 7ca5248 Feb 1, 2021
tk0miya added a commit that referenced this issue Feb 1, 2021
Fix #8775: Avoid the crash of autodoc caused by type union operator
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 14, 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