diff options
author | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2021-09-11 11:19:54 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-11 11:19:54 +0300 |
commit | c44ee0ebaa8039a4e1014fa02f485c4693414e17 (patch) | |
tree | f47ff35f40e68368d5d7c64199bea5de84b300a3 /sphinx/util | |
parent | 8416813168baa32449af1e9a2047282d57efc9fa (diff) | |
parent | c5b35efce353a3e1670185704bcd1453b9da3533 (diff) |
Merge pull request #9611 from tk0miya/9560_NewType_module
Close #9560: autodoc: Allow to refer NewType with modname in py310+
Diffstat (limited to 'sphinx/util')
-rw-r--r-- | sphinx/util/typing.py | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/sphinx/util/typing.py b/sphinx/util/typing.py index c0a038420..8912de8cd 100644 --- a/sphinx/util/typing.py +++ b/sphinx/util/typing.py @@ -116,7 +116,12 @@ def restify(cls: Optional[Type]) -> str: elif cls in INVALID_BUILTIN_CLASSES: return ':class:`%s`' % INVALID_BUILTIN_CLASSES[cls] elif inspect.isNewType(cls): - return ':class:`%s`' % cls.__name__ + if sys.version_info > (3, 10): + # newtypes have correct module info since Python 3.10+ + print(cls, type(cls), dir(cls)) + return ':class:`%s.%s`' % (cls.__module__, cls.__name__) + else: + return ':class:`%s`' % cls.__name__ elif UnionType and isinstance(cls, UnionType): if len(cls.__args__) > 1 and None in cls.__args__: args = ' | '.join(restify(a) for a in cls.__args__ if a) @@ -307,8 +312,11 @@ def stringify(annotation: Any) -> str: else: return '.'.join([annotation.__module__, annotation.__name__]) elif inspect.isNewType(annotation): - # Could not get the module where it defined - return annotation.__name__ + if sys.version_info > (3, 10): + # newtypes have correct module info since Python 3.10+ + return '%s.%s' % (annotation.__module__, annotation.__name__) + else: + return annotation.__name__ elif not annotation: return repr(annotation) elif annotation is NoneType: |