theislab / scanpydoc

Collection of Sphinx extensions similar to (but more flexible than) numpydoc
https://icb-scanpydoc.readthedocs-hosted.com/en/latest/
GNU General Public License v3.0
12 stars 6 forks source link

AttributeError 'types.UnionType' object has no attribute '__qualname__' for UnionType expressions #65

Closed tilsche closed 1 year ago

tilsche commented 1 year ago

I am running into an error with scanpydoc, sphinx, sphinx_autodoc_typehints:

When having any return type in the Python 3.10 Union Type expressions, I get

AttributeError 'types.UnionType' object has no attribute '__qualname__'

in https://github.com/theislab/scanpydoc/blob/master/scanpydoc/elegant_typehints/formatting.py#L37

Oddly, UnionType does in fact not have a __qualname__, while the old typing.Union does:

>>> UT = int | str
>>> type(UT)
<class 'types.UnionType'>
>>> UT.__qualname__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'types.UnionType' object has no attribute '__qualname__'
>>> from typing import Union
>>> UTold = Union[int, str]
>>> type(UTold)
<class 'typing._UnionGenericAlias'>
>>> UTold.__qualname__
'Union'

>>> inspect.isclass(UnionType)
True
flying-sheep commented 1 year ago

Thanks! Fix released