From c851e4cabad8dbcfd8385034ef81b89a6326bc1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torstein=20I=2E=20B=C3=B8?= Date: Thu, 14 Jun 2018 10:47:32 +0200 Subject: [PATCH 1/5] NewType handling --- sphinx_autodoc_typehints.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sphinx_autodoc_typehints.py b/sphinx_autodoc_typehints.py index d2671bb5..eb349803 100644 --- a/sphinx_autodoc_typehints.py +++ b/sphinx_autodoc_typehints.py @@ -108,6 +108,9 @@ def format_annotation(annotation): return '{}`~{}.{}`{}'.format(prefix, module, class_name, extra) elif annotation is Ellipsis: return '...' + elif hasattr(annotation,'__name__'): + # May be a NewType + return annotation.__name__ elif inspect.isclass(annotation) or inspect.isclass(getattr(annotation, '__origin__', None)): if not inspect.isclass(annotation): annotation_cls = annotation.__origin__ From 6802695e11f76318a38883e637568c953a2dfbe0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torstein=20B=C3=B8?= Date: Thu, 14 Jun 2018 12:30:48 +0200 Subject: [PATCH 2/5] Made tests and passes successfully --- sphinx_autodoc_typehints.py | 5 +++-- tests/test_sphinx_autodoc_typehints.py | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/sphinx_autodoc_typehints.py b/sphinx_autodoc_typehints.py index eb349803..568ede8b 100644 --- a/sphinx_autodoc_typehints.py +++ b/sphinx_autodoc_typehints.py @@ -108,9 +108,10 @@ def format_annotation(annotation): return '{}`~{}.{}`{}'.format(prefix, module, class_name, extra) elif annotation is Ellipsis: return '...' - elif hasattr(annotation,'__name__'): + elif hasattr(annotation,'__name__') and hasattr(annotation, '__supertype__') and annotation.__module__ == 'typing': # May be a NewType - return annotation.__name__ + return ':py:class:`~typing.NewType.{}`\\[{}]'.format(annotation.__name__, + format_annotation(annotation.__supertype__)) elif inspect.isclass(annotation) or inspect.isclass(getattr(annotation, '__origin__', None)): if not inspect.isclass(annotation): annotation_cls = annotation.__origin__ diff --git a/tests/test_sphinx_autodoc_typehints.py b/tests/test_sphinx_autodoc_typehints.py index 5a944f05..b57e186e 100644 --- a/tests/test_sphinx_autodoc_typehints.py +++ b/tests/test_sphinx_autodoc_typehints.py @@ -3,13 +3,14 @@ import sys import textwrap from typing import ( - Any, AnyStr, Callable, Dict, Generic, Mapping, Optional, Pattern, Tuple, TypeVar, Union, Type) + Any, AnyStr, Callable, Dict, Generic, Mapping, Optional, Pattern, Tuple, TypeVar, Union, Type, NewType) from sphinx_autodoc_typehints import format_annotation, process_docstring T = TypeVar('T') U = TypeVar('U', covariant=True) V = TypeVar('V', contravariant=True) +Z = NewType('Z', float) class A: @@ -69,6 +70,7 @@ class Slotted: (Callable[[T], T], ':py:data:`~typing.Callable`\\[\\[\\~T], \\~T]'), (Pattern, ':py:class:`~typing.Pattern`\\[:py:data:`~typing.AnyStr`]'), (Pattern[str], ':py:class:`~typing.Pattern`\\[:py:class:`str`]'), + (Z, ':py:class:`~typing.NewType.Z`\\[:py:class:`float`]'), (A, ':py:class:`~%s.A`' % __name__), (B, ':py:class:`~%s.B`\\[\\~T]' % __name__), (B[int], ':py:class:`~%s.B`\\[:py:class:`int`]' % __name__) From 689d8aad060f6fb0b926e0576a135cdae9fcbd62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torstein=20B=C3=B8?= Date: Thu, 14 Jun 2018 12:34:26 +0200 Subject: [PATCH 3/5] Fixed syntax --- sphinx_autodoc_typehints.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sphinx_autodoc_typehints.py b/sphinx_autodoc_typehints.py index 568ede8b..82ced276 100644 --- a/sphinx_autodoc_typehints.py +++ b/sphinx_autodoc_typehints.py @@ -108,10 +108,11 @@ def format_annotation(annotation): return '{}`~{}.{}`{}'.format(prefix, module, class_name, extra) elif annotation is Ellipsis: return '...' - elif hasattr(annotation,'__name__') and hasattr(annotation, '__supertype__') and annotation.__module__ == 'typing': + elif hasattr(annotation, '__name__') and hasattr(annotation, '__supertype__') \ + and annotation.__module__ == 'typing': # May be a NewType return ':py:class:`~typing.NewType.{}`\\[{}]'.format(annotation.__name__, - format_annotation(annotation.__supertype__)) + format_annotation(annotation.__supertype__)) elif inspect.isclass(annotation) or inspect.isclass(getattr(annotation, '__origin__', None)): if not inspect.isclass(annotation): annotation_cls = annotation.__origin__ From 160b632943eb5c00271ce2d12bef80f30a2efbe6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torstein=20B=C3=B8?= Date: Thu, 14 Jun 2018 12:51:26 +0200 Subject: [PATCH 4/5] Fixed syntax, second try --- sphinx_autodoc_typehints.py | 3 ++- tests/test_sphinx_autodoc_typehints.py | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/sphinx_autodoc_typehints.py b/sphinx_autodoc_typehints.py index 82ced276..40746c2a 100644 --- a/sphinx_autodoc_typehints.py +++ b/sphinx_autodoc_typehints.py @@ -108,7 +108,8 @@ def format_annotation(annotation): return '{}`~{}.{}`{}'.format(prefix, module, class_name, extra) elif annotation is Ellipsis: return '...' - elif hasattr(annotation, '__name__') and hasattr(annotation, '__supertype__') \ + elif hasattr(annotation, '__name__') \ + and hasattr(annotation, '__supertype__') \ and annotation.__module__ == 'typing': # May be a NewType return ':py:class:`~typing.NewType.{}`\\[{}]'.format(annotation.__name__, diff --git a/tests/test_sphinx_autodoc_typehints.py b/tests/test_sphinx_autodoc_typehints.py index b57e186e..a552850e 100644 --- a/tests/test_sphinx_autodoc_typehints.py +++ b/tests/test_sphinx_autodoc_typehints.py @@ -2,8 +2,8 @@ import pytest import sys import textwrap -from typing import ( - Any, AnyStr, Callable, Dict, Generic, Mapping, Optional, Pattern, Tuple, TypeVar, Union, Type, NewType) +from typing import Any, AnyStr, Callable, Dict, Generic, \ + Mapping, Optional, Pattern, Tuple, TypeVar, Union, Type, NewType from sphinx_autodoc_typehints import format_annotation, process_docstring From b514fa2bfecfce8b06460f5299af4c2edda66533 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torstein=20B=C3=B8?= Date: Thu, 14 Jun 2018 13:06:36 +0200 Subject: [PATCH 5/5] Fixed syntax, third try --- sphinx_autodoc_typehints.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sphinx_autodoc_typehints.py b/sphinx_autodoc_typehints.py index 40746c2a..90196af2 100644 --- a/sphinx_autodoc_typehints.py +++ b/sphinx_autodoc_typehints.py @@ -112,8 +112,8 @@ def format_annotation(annotation): and hasattr(annotation, '__supertype__') \ and annotation.__module__ == 'typing': # May be a NewType - return ':py:class:`~typing.NewType.{}`\\[{}]'.format(annotation.__name__, - format_annotation(annotation.__supertype__)) + return ':py:class:`~typing.NewType.{}`\\[{}]'.format( + annotation.__name__, format_annotation(annotation.__supertype__)) elif inspect.isclass(annotation) or inspect.isclass(getattr(annotation, '__origin__', None)): if not inspect.isclass(annotation): annotation_cls = annotation.__origin__