mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
refactor: sphinx.util.inspect:Signature.format_annotation_old()
This commit is contained in:
parent
5db23ea002
commit
f68dd9c986
@ -535,25 +535,18 @@ class Signature:
|
|||||||
else:
|
else:
|
||||||
qualname = repr(annotation)
|
qualname = repr(annotation)
|
||||||
|
|
||||||
if (hasattr(typing, 'TupleMeta') and
|
if (isinstance(annotation, typing.TupleMeta) and
|
||||||
isinstance(annotation, typing.TupleMeta) and
|
not hasattr(annotation, '__tuple_params__')): # for Python 3.6
|
||||||
not hasattr(annotation, '__tuple_params__')):
|
|
||||||
# This is for Python 3.6+, 3.5 case is handled below
|
|
||||||
params = annotation.__args__
|
params = annotation.__args__
|
||||||
if params:
|
if params:
|
||||||
param_str = ', '.join(self.format_annotation(p) for p in params)
|
param_str = ', '.join(self.format_annotation(p) for p in params)
|
||||||
return '%s[%s]' % (qualname, param_str)
|
return '%s[%s]' % (qualname, param_str)
|
||||||
else:
|
else:
|
||||||
return qualname
|
return qualname
|
||||||
elif (hasattr(typing, 'GenericMeta') and # for py36 or below
|
elif isinstance(annotation, typing.GenericMeta):
|
||||||
isinstance(annotation, typing.GenericMeta)):
|
|
||||||
# In Python 3.5.2+, all arguments are stored in __args__,
|
|
||||||
# whereas __parameters__ only contains generic parameters.
|
|
||||||
#
|
|
||||||
# Prior to Python 3.5.2, __args__ is not available, and all
|
|
||||||
# arguments are in __parameters__.
|
|
||||||
params = None
|
params = None
|
||||||
if hasattr(annotation, '__args__'):
|
if hasattr(annotation, '__args__'):
|
||||||
|
# for Python 3.5.2+
|
||||||
if annotation.__args__ is None or len(annotation.__args__) <= 2: # type: ignore # NOQA
|
if annotation.__args__ is None or len(annotation.__args__) <= 2: # type: ignore # NOQA
|
||||||
params = annotation.__args__ # type: ignore
|
params = annotation.__args__ # type: ignore
|
||||||
else: # typing.Callable
|
else: # typing.Callable
|
||||||
@ -562,13 +555,14 @@ class Signature:
|
|||||||
result = self.format_annotation(annotation.__args__[-1]) # type: ignore
|
result = self.format_annotation(annotation.__args__[-1]) # type: ignore
|
||||||
return '%s[[%s], %s]' % (qualname, args, result)
|
return '%s[[%s], %s]' % (qualname, args, result)
|
||||||
elif hasattr(annotation, '__parameters__'):
|
elif hasattr(annotation, '__parameters__'):
|
||||||
|
# for Python 3.5.0 and 3.5.1
|
||||||
params = annotation.__parameters__ # type: ignore
|
params = annotation.__parameters__ # type: ignore
|
||||||
if params is not None:
|
if params is not None:
|
||||||
param_str = ', '.join(self.format_annotation(p) for p in params)
|
param_str = ', '.join(self.format_annotation(p) for p in params)
|
||||||
return '%s[%s]' % (qualname, param_str)
|
return '%s[%s]' % (qualname, param_str)
|
||||||
elif (hasattr(typing, 'UnionMeta') and # for py35 or below
|
elif (hasattr(typing, 'UnionMeta') and
|
||||||
isinstance(annotation, typing.UnionMeta) and
|
isinstance(annotation, typing.UnionMeta) and
|
||||||
hasattr(annotation, '__union_params__')):
|
hasattr(annotation, '__union_params__')): # for Python 3.5
|
||||||
params = annotation.__union_params__
|
params = annotation.__union_params__
|
||||||
if params is not None:
|
if params is not None:
|
||||||
if len(params) == 2 and params[1] is NoneType: # type: ignore
|
if len(params) == 2 and params[1] is NoneType: # type: ignore
|
||||||
@ -576,9 +570,8 @@ class Signature:
|
|||||||
else:
|
else:
|
||||||
param_str = ', '.join(self.format_annotation(p) for p in params)
|
param_str = ', '.join(self.format_annotation(p) for p in params)
|
||||||
return '%s[%s]' % (qualname, param_str)
|
return '%s[%s]' % (qualname, param_str)
|
||||||
elif (hasattr(typing, 'Union') and # for py36
|
elif (hasattr(annotation, '__origin__') and
|
||||||
hasattr(annotation, '__origin__') and
|
annotation.__origin__ is typing.Union): # for Python 3.5.2+
|
||||||
annotation.__origin__ is typing.Union):
|
|
||||||
params = annotation.__args__
|
params = annotation.__args__
|
||||||
if params is not None:
|
if params is not None:
|
||||||
if len(params) == 2 and params[1] is NoneType: # type: ignore
|
if len(params) == 2 and params[1] is NoneType: # type: ignore
|
||||||
@ -586,10 +579,9 @@ class Signature:
|
|||||||
else:
|
else:
|
||||||
param_str = ', '.join(self.format_annotation(p) for p in params)
|
param_str = ', '.join(self.format_annotation(p) for p in params)
|
||||||
return 'Union[%s]' % param_str
|
return 'Union[%s]' % param_str
|
||||||
elif (hasattr(typing, 'CallableMeta') and # for py36 or below
|
elif (isinstance(annotation, typing.CallableMeta) and
|
||||||
isinstance(annotation, typing.CallableMeta) and
|
|
||||||
getattr(annotation, '__args__', None) is not None and
|
getattr(annotation, '__args__', None) is not None and
|
||||||
hasattr(annotation, '__result__')):
|
hasattr(annotation, '__result__')): # for Python 3.5
|
||||||
# Skipped in the case of plain typing.Callable
|
# Skipped in the case of plain typing.Callable
|
||||||
args = annotation.__args__
|
args = annotation.__args__
|
||||||
if args is None:
|
if args is None:
|
||||||
@ -602,10 +594,9 @@ class Signature:
|
|||||||
return '%s[%s, %s]' % (qualname,
|
return '%s[%s, %s]' % (qualname,
|
||||||
args_str,
|
args_str,
|
||||||
self.format_annotation(annotation.__result__))
|
self.format_annotation(annotation.__result__))
|
||||||
elif (hasattr(typing, 'TupleMeta') and # for py36 or below
|
elif (isinstance(annotation, typing.TupleMeta) and
|
||||||
isinstance(annotation, typing.TupleMeta) and
|
|
||||||
hasattr(annotation, '__tuple_params__') and
|
hasattr(annotation, '__tuple_params__') and
|
||||||
hasattr(annotation, '__tuple_use_ellipsis__')):
|
hasattr(annotation, '__tuple_use_ellipsis__')): # for Python 3.5
|
||||||
params = annotation.__tuple_params__
|
params = annotation.__tuple_params__
|
||||||
if params is not None:
|
if params is not None:
|
||||||
param_strings = [self.format_annotation(p) for p in params]
|
param_strings = [self.format_annotation(p) for p in params]
|
||||||
|
Loading…
Reference in New Issue
Block a user