Skip to content

Commit c44ee0e

Browse files
authored
Merge pull request #9611 from tk0miya/9560_NewType_module
Close #9560: autodoc: Allow to refer NewType with modname in py310+
2 parents 8416813 + c5b35ef commit c44ee0e

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Features added
1515

1616
* #9445: autodoc: Support class properties
1717
* #9479: autodoc: Emit a warning if target is a mocked object
18+
* #9560: autodoc: Allow to refer NewType instances with module name in Python
19+
3.10 or above
1820
* #9447: html theme: Expose the version of Sphinx in the form of tuple as a
1921
template variable ``sphinx_version_tuple``
2022
* #9445: py domain: ``:py:property:`` directive supports ``:classmethod:``

sphinx/util/typing.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,12 @@ def restify(cls: Optional[Type]) -> str:
116116
elif cls in INVALID_BUILTIN_CLASSES:
117117
return ':class:`%s`' % INVALID_BUILTIN_CLASSES[cls]
118118
elif inspect.isNewType(cls):
119-
return ':class:`%s`' % cls.__name__
119+
if sys.version_info > (3, 10):
120+
# newtypes have correct module info since Python 3.10+
121+
print(cls, type(cls), dir(cls))
122+
return ':class:`%s.%s`' % (cls.__module__, cls.__name__)
123+
else:
124+
return ':class:`%s`' % cls.__name__
120125
elif UnionType and isinstance(cls, UnionType):
121126
if len(cls.__args__) > 1 and None in cls.__args__:
122127
args = ' | '.join(restify(a) for a in cls.__args__ if a)
@@ -307,8 +312,11 @@ def stringify(annotation: Any) -> str:
307312
else:
308313
return '.'.join([annotation.__module__, annotation.__name__])
309314
elif inspect.isNewType(annotation):
310-
# Could not get the module where it defined
311-
return annotation.__name__
315+
if sys.version_info > (3, 10):
316+
# newtypes have correct module info since Python 3.10+
317+
return '%s.%s' % (annotation.__module__, annotation.__name__)
318+
else:
319+
return annotation.__name__
312320
elif not annotation:
313321
return repr(annotation)
314322
elif annotation is NoneType:

tests/test_util_typing.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,11 @@ def test_restify_type_hints_typevars():
113113
assert restify(T_co) == ":obj:`tests.test_util_typing.T_co`"
114114
assert restify(T_contra) == ":obj:`tests.test_util_typing.T_contra`"
115115
assert restify(List[T]) == ":class:`~typing.List`\\ [:obj:`tests.test_util_typing.T`]"
116-
assert restify(MyInt) == ":class:`MyInt`"
116+
117+
if sys.version_info >= (3, 10):
118+
assert restify(MyInt) == ":class:`tests.test_util_typing.MyInt`"
119+
else:
120+
assert restify(MyInt) == ":class:`MyInt`"
117121

118122

119123
def test_restify_type_hints_custom_class():
@@ -250,7 +254,10 @@ def test_stringify_type_hints_typevars():
250254
assert stringify(T_contra) == "tests.test_util_typing.T_contra"
251255
assert stringify(List[T]) == "List[tests.test_util_typing.T]"
252256

253-
assert stringify(MyInt) == "MyInt"
257+
if sys.version_info >= (3, 10):
258+
assert stringify(MyInt) == "tests.test_util_typing.MyInt"
259+
else:
260+
assert stringify(MyInt) == "MyInt"
254261

255262

256263
def test_stringify_type_hints_custom_class():

0 commit comments

Comments
 (0)