diff options
author | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2021-09-05 12:28:27 +0300 |
---|---|---|
committer | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2021-09-05 12:28:27 +0300 |
commit | c5b35efce353a3e1670185704bcd1453b9da3533 (patch) | |
tree | a71efcd702772f10232829471fa7a92cc243241c /sphinx/util | |
parent | 336605b8e4b14c5da9f4d872fb730dc6894edb77 (diff) |
Close #9560: autodoc: Allow to refer NewType with modname in py310+
Before 3.10, an instance of NewType has incorrect module name. But it
was fixed on 3.10. This starts to use the module info if the
interpreter is 3.10+.
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 cf4318cda..004fb4a71 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: |