Skip to content

Commit

Permalink
Fix sphinx-doc#4490: autodoc: type annotation is broken with python 3…
Browse files Browse the repository at this point in the history
….7.0a4+
  • Loading branch information
tk0miya committed Jan 27, 2018
1 parent 376b6a5 commit e2389b4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Bugs fixed
* #4415: autodoc classifies inherited classmethods as regular methods
* #4415: autodoc classifies inherited staticmethods as regular methods
* #4472: DOCUMENTATION_OPTIONS is not defined
* #4490: autodoc: type annotation is broken with python 3.7.0a4+

Testing
--------
Expand Down
49 changes: 49 additions & 0 deletions sphinx/util/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,55 @@ def format_annotation(self, annotation):
Displaying complex types from ``typing`` relies on its private API.
"""
if sys.version_info >= (3, 7): # py37 and above
return self.format_annotation_new(annotation)
else:
return self.format_annotation_old(annotation)

def format_annotation_new(self, annotation):
# type: (Any) -> str
"""format_annotation() for py37+"""
module = getattr(annotation, '__module__', None)
if isinstance(annotation, string_types):
return annotation # type: ignore
elif isinstance(annotation, typing.TypeVar):
return annotation.__name__
elif not annotation:
return repr(annotation)
elif module == 'builtins':
return annotation.__qualname__
elif annotation is Ellipsis:
return '...'

if module == 'typing':
if getattr(annotation, '_name', None):
qualname = annotation._name
elif getattr(annotation, '__qualname__', None):
qualname = annotation.__qualname__
else:
qualname = self.format_annotation(annotation.__origin__) # ex. Union
elif hasattr(annotation, '__qualname__'):
qualname = '%s.%s' % (module, annotation.__qualname__)
else:
qualname = repr(annotation)

if getattr(annotation, '__args__', None):
if qualname == 'Union':
args = ', '.join(self.format_annotation(a) for a in annotation.__args__)
return '%s[%s]' % (qualname, args)
elif qualname == 'Callable':
args = ', '.join(self.format_annotation(a) for a in annotation.__args__[:-1])
returns = self.format_annotation(annotation.__args__[-1])
return '%s[[%s], %s]' % (qualname, args, returns)
else:
args = ', '.join(self.format_annotation(a) for a in annotation.__args__)
return '%s[%s]' % (qualname, args)

return qualname

def format_annotation_old(self, annotation):
# type: (Any) -> str
"""format_annotation() for py36 or below"""
if isinstance(annotation, string_types):
return annotation # type: ignore
if isinstance(annotation, typing.TypeVar): # type: ignore
Expand Down
7 changes: 1 addition & 6 deletions tests/test_util_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,7 @@ def test_Signature_annotations():

# TypeVars and generic types with TypeVars
sig = inspect.Signature(f2).format_args()
if sys.version_info < (3, 7):
sig == ('(x: typing.List[T], y: typing.List[T_co], z: T) -> '
'typing.List[T_contra]')
else:
sig == ('(x: typing.List[~T], y: typing.List[+T_co], z: T) -> '
'typing.List[-T_contra]')
assert sig == '(x: List[T], y: List[T_co], z: T) -> List[T_contra]'

# Union types
sig = inspect.Signature(f3).format_args()
Expand Down

0 comments on commit e2389b4

Please sign in to comment.