Skip to content

Commit

Permalink
Merge branch '1.7-release' into use_flake8-import-order
Browse files Browse the repository at this point in the history
  • Loading branch information
tk0miya authored Jan 28, 2018
2 parents 9f5c676 + 01caa52 commit 6926a3e
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Bugs fixed
* #4415: autodoc classifies inherited staticmethods as regular methods
* #4472: DOCUMENTATION_OPTIONS is not defined
* #4491: autodoc: prefer _MockImporter over other importers in sys.meta_path
* #4490: autodoc: type annotation is broken with python 3.7.0a4+

Testing
--------
Expand Down
4 changes: 4 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ exclude = .git,.tox,.venv,tests/*,build/*,doc/_build/*,sphinx/search/*,sphinx/py
application-import-names = sphinx
import-order-style = smarkets

[flake8:local-plugins]
extension =
X101 = utils.checks:sphinx_has_header

[mypy]
python_version = 2.7
show_column_numbers = True
Expand Down
13 changes: 3 additions & 10 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
print('ERROR: Sphinx requires at least Python 2.7 or 3.4 to run.')
sys.exit(1)

requires = [
install_requires = [
'six>=1.5',
'Jinja2>=2.3',
'Pygments>=2.0',
Expand Down Expand Up @@ -47,7 +47,7 @@
'pytest',
'pytest-cov',
'html5lib',
'flake8',
'flake8>=3.5.0',
'flake8-import-order',
],
'test:python_version<"3"': [
Expand Down Expand Up @@ -227,15 +227,8 @@ def sub_commands(self):
'distutils.commands': [
'build_sphinx = sphinx.setup_command:BuildDoc',
],
# consider moving this to 'flake8:local-plugins' once flake8 3.5.0 is
# in the wild:
# http://flake8.pycqa.org/en/latest/user/configuration.html\
# #using-local-plugins
'flake8.extension': [
'X101 = utils.checks:sphinx_has_header',
],
},
install_requires=requires,
install_requires=install_requires,
extras_require=extras_require,
cmdclass=cmdclass,
)
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+
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): # type: ignore
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 6926a3e

Please sign in to comment.