Skip to content

Commit

Permalink
new __args__
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Apr 23, 2024
1 parent 66c7c9e commit 7db1ef2
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions sphinx/util/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,23 +255,27 @@ def restify(cls: Any, mode: _RestifyMode = 'fully-qualified-except-typing') -> s
else:
text = restify(cls.__origin__, mode)

origin = getattr(cls, '__origin__', None)
if not hasattr(cls, '__args__'): # NoQA: SIM114
pass
elif all(is_system_TypeVar(a) for a in cls.__args__):
# Suppress arguments if all system defined TypeVars (ex. Dict[KT, VT])
pass
elif cls.__module__ == 'typing' and cls._name == 'Callable':
args = ', '.join(restify(a, mode) for a in cls.__args__[:-1])
text += fr'\ [[{args}], {restify(cls.__args__[-1], mode)}]'
elif cls.__module__ == 'typing' and getattr(origin, '_name', None) == 'Literal':
__args__ = getattr(cls, '__args__', ())
if not __args__:
return text
if all(map(is_system_TypeVar, __args__)):
# Don't print the arguments; they're all system defined type variables.
return text

# Callable has special formatting
if cls_module_is_typing and cls.__name__ == 'Callable':
args = ', '.join(restify(a, mode) for a in __args__[:-1])
returns = restify(__args__[-1], mode)
return fr'{text}\ [[{args}], {returns}]'

if cls_module_is_typing and cls.__origin__.__name__ == 'Literal':
args = ', '.join(_format_literal_arg_restify(a, mode=mode)
for a in cls.__args__)
text += fr"\ [{args}]"
elif cls.__args__:
text += fr"\ [{', '.join(restify(a, mode) for a in cls.__args__)}]"
return fr'{text}\ [{args}]'

return text
# generic representation of the parameters
args = ', '.join(restify(a, mode) for a in __args__)
return fr'{text}\ [{args}]'
elif isinstance(cls, typing._SpecialForm):
return f':py:obj:`~{cls.__module__}.{cls._name}`' # type: ignore[attr-defined]
elif sys.version_info[:2] >= (3, 11) and cls is typing.Any:
Expand Down

0 comments on commit 7db1ef2

Please sign in to comment.