You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sphinx raises an error when an imported type is joined with another type via the pipe operator in a type hint even though it is valid syntax for PEP 604.
How to Reproduce
importnumpyasnpdeffunc_1(inp: np.ndarray|None):
pass# should work but doesn'tdeffunc_2(inp: type(np.array([])) |None):
pass# works but is curseddeffunc_3(inp: np.ndarray):
pass# works finedeffunc_4(inp: list|None):
pass# works fine
I think I just ran into the same issue when using an imported type from a mocked module in a PEP604 Union. (Similar versions, sphinx 6.1.3, python 3.10.12)
[edit] retried it with sphinx 7.2.5, same behavior (ext.autodoc.mock.py is mostly unchanged, so that was expected [/edit]
The mock factory creates a new type() in the mock module's getattr, and returns an instance of that.
The instance in itself is not a type, so it does not gain the | operator for types.
(also, calling & or | on the mock objects with any other value/object seems to not work).
If the mock is only used in a type-checking context, it could be worked around by either returning the type itself, or implementing a __or__(__value) method in _MockObject, that can return a typing.Union[self, __value].
Both of these could potentially break imports where it should have been a value/instance instead.
Describe the bug
Sphinx raises an error when an imported type is joined with another type via the pipe operator in a type hint even though it is valid syntax for PEP 604.
How to Reproduce
conf.py contains the following:
Environment Information
Sphinx extensions
["sphinx.ext.autodoc"]
Additional context
No response
The text was updated successfully, but these errors were encountered: