mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Fix #8775: Avoid the crash of autodoc caused by type union 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.
This commit is contained in:
parent
2956f19674
commit
7ca5248057
@ -77,7 +77,9 @@ def get_type_hints(obj: Any, globalns: Dict = None, localns: Dict = None) -> Dic
|
|||||||
# Failed to evaluate ForwardRef (maybe TYPE_CHECKING)
|
# Failed to evaluate ForwardRef (maybe TYPE_CHECKING)
|
||||||
return safe_getattr(obj, '__annotations__', {})
|
return safe_getattr(obj, '__annotations__', {})
|
||||||
except TypeError:
|
except TypeError:
|
||||||
return {}
|
# Invalid object is given. But try to get __annotations__ as a fallback for
|
||||||
|
# the code using type union operator (PEP 604) in python 3.9 or below.
|
||||||
|
return safe_getattr(obj, '__annotations__', {})
|
||||||
except KeyError:
|
except KeyError:
|
||||||
# a broken class found (refs: https://github.com/sphinx-doc/sphinx/issues/8084)
|
# a broken class found (refs: https://github.com/sphinx-doc/sphinx/issues/8084)
|
||||||
return {}
|
return {}
|
||||||
|
@ -2237,7 +2237,7 @@ def test_name_mangling(app):
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.skipif(sys.version_info < (3, 10), reason='python 3.10+ is required.')
|
@pytest.mark.skipif(sys.version_info < (3, 7), reason='python 3.7+ is required.')
|
||||||
@pytest.mark.sphinx('html', testroot='ext-autodoc')
|
@pytest.mark.sphinx('html', testroot='ext-autodoc')
|
||||||
def test_type_union_operator(app):
|
def test_type_union_operator(app):
|
||||||
options = {'members': None}
|
options = {'members': None}
|
||||||
|
Loading…
Reference in New Issue
Block a user