Skip to content

Commit 771507e

Browse files
committed
Fix #9489: autodoc: Custom types using typing.NewType are broken
At the HEAD of 3.10, the implementation of ``typing.NewType`` has been changed to the class based. To follow the change, this uses ``isinstance`` on ``sphinx.util.inspect:isNewType()`.
1 parent 9ebdc98 commit 771507e

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

CHANGES

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Features added
1919
Bugs fixed
2020
----------
2121

22+
* #9489: autodoc: Custom types using ``typing.NewType`` are not displayed well
23+
with the HEAD of 3.10
2224
* #9435: linkcheck: Failed to check anchors in github.com
2325

2426
Testing

sphinx/util/inspect.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,15 @@ def getslots(obj: Any) -> Optional[Dict]:
211211

212212
def isNewType(obj: Any) -> bool:
213213
"""Check the if object is a kind of NewType."""
214-
__module__ = safe_getattr(obj, '__module__', None)
215-
__qualname__ = safe_getattr(obj, '__qualname__', None)
216-
if __module__ == 'typing' and __qualname__ == 'NewType.<locals>.new_type':
217-
return True
214+
if sys.version_info >= (3, 10):
215+
return isinstance(obj, typing.NewType)
218216
else:
219-
return False
217+
__module__ = safe_getattr(obj, '__module__', None)
218+
__qualname__ = safe_getattr(obj, '__qualname__', None)
219+
if __module__ == 'typing' and __qualname__ == 'NewType.<locals>.new_type':
220+
return True
221+
else:
222+
return False
220223

221224

222225
def isenumclass(x: Any) -> bool:

0 commit comments

Comments
 (0)