Skip to content

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

@tilsche

Description

@tilsche

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions