mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Add some comments for Union types in `sphinx.util.typing
` (#12281)
This commit is contained in:
parent
48a345b7c5
commit
a5f534a77d
@ -186,6 +186,8 @@ def restify(cls: type | None, mode: str = 'fully-qualified-except-typing') -> st
|
|||||||
else:
|
else:
|
||||||
return f':py:class:`{cls.__name__}`'
|
return f':py:class:`{cls.__name__}`'
|
||||||
elif UnionType and isinstance(cls, UnionType):
|
elif UnionType and isinstance(cls, UnionType):
|
||||||
|
# Union types (PEP 585) retain their definition order when they
|
||||||
|
# are printed natively and ``None``-like types are kept as is.
|
||||||
return ' | '.join(restify(a, mode) for a in cls.__args__)
|
return ' | '.join(restify(a, mode) for a in cls.__args__)
|
||||||
elif cls.__module__ in ('__builtin__', 'builtins'):
|
elif cls.__module__ in ('__builtin__', 'builtins'):
|
||||||
if hasattr(cls, '__args__'):
|
if hasattr(cls, '__args__'):
|
||||||
@ -199,6 +201,7 @@ def restify(cls: type | None, mode: str = 'fully-qualified-except-typing') -> st
|
|||||||
elif (inspect.isgenericalias(cls)
|
elif (inspect.isgenericalias(cls)
|
||||||
and cls.__module__ == 'typing'
|
and cls.__module__ == 'typing'
|
||||||
and cls.__origin__ is Union): # type: ignore[attr-defined]
|
and cls.__origin__ is Union): # type: ignore[attr-defined]
|
||||||
|
# *cls* is defined in ``typing``, and thus ``__args__`` must exist
|
||||||
return ' | '.join(restify(a, mode) for a in cls.__args__) # type: ignore[attr-defined]
|
return ' | '.join(restify(a, mode) for a in cls.__args__) # type: ignore[attr-defined]
|
||||||
elif inspect.isgenericalias(cls):
|
elif inspect.isgenericalias(cls):
|
||||||
if isinstance(cls.__origin__, typing._SpecialForm): # type: ignore[attr-defined]
|
if isinstance(cls.__origin__, typing._SpecialForm): # type: ignore[attr-defined]
|
||||||
|
Loading…
Reference in New Issue
Block a user