Skip to content

NewType handling #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions sphinx_autodoc_typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ 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':
# May be a NewType
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__
Expand Down
6 changes: 4 additions & 2 deletions tests/test_sphinx_autodoc_typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
import pytest
import sys
import textwrap
from typing import (
Any, AnyStr, Callable, Dict, Generic, Mapping, Optional, Pattern, Tuple, TypeVar, Union, Type)
from typing import Any, AnyStr, Callable, Dict, Generic, \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep the old import style.

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:
Expand Down Expand Up @@ -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`]'),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure this is the best way to render these?

(A, ':py:class:`~%s.A`' % __name__),
(B, ':py:class:`~%s.B`\\[\\~T]' % __name__),
(B[int], ':py:class:`~%s.B`\\[:py:class:`int`]' % __name__)
Expand Down