Skip to content

Commit 9218ad4

Browse files
authored
Merge pull request #9491 from tk0miya/9489_NewType
Bugfix for HEAD of python-3.10
2 parents 9ebdc98 + 68fb548 commit 9218ad4

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

CHANGES

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ 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
24+
* #9490: autodoc: Some objects under ``typing`` module are not displayed well
25+
with the HEAD of 3.10
2226
* #9435: linkcheck: Failed to check anchors in github.com
2327

2428
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:

sphinx/util/typing.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -171,17 +171,17 @@ def _restify_py37(cls: Optional[Type]) -> str:
171171
text += r"\ [%s]" % ", ".join(restify(a) for a in cls.__args__)
172172

173173
return text
174-
elif hasattr(cls, '__qualname__'):
175-
if cls.__module__ == 'typing':
176-
return ':class:`~%s.%s`' % (cls.__module__, cls.__qualname__)
177-
else:
178-
return ':class:`%s.%s`' % (cls.__module__, cls.__qualname__)
179174
elif hasattr(cls, '_name'):
180175
# SpecialForm
181176
if cls.__module__ == 'typing':
182177
return ':obj:`~%s.%s`' % (cls.__module__, cls._name)
183178
else:
184179
return ':obj:`%s.%s`' % (cls.__module__, cls._name)
180+
elif hasattr(cls, '__qualname__'):
181+
if cls.__module__ == 'typing':
182+
return ':class:`~%s.%s`' % (cls.__module__, cls.__qualname__)
183+
else:
184+
return ':class:`%s.%s`' % (cls.__module__, cls.__qualname__)
185185
elif isinstance(cls, ForwardRef):
186186
return ':class:`%s`' % cls.__forward_arg__
187187
else:
@@ -309,7 +309,7 @@ def stringify(annotation: Any) -> str:
309309
elif annotation in INVALID_BUILTIN_CLASSES:
310310
return INVALID_BUILTIN_CLASSES[annotation]
311311
elif (getattr(annotation, '__module__', None) == 'builtins' and
312-
hasattr(annotation, '__qualname__')):
312+
getattr(annotation, '__qualname__', None)):
313313
return annotation.__qualname__
314314
elif annotation is Ellipsis:
315315
return '...'

0 commit comments

Comments
 (0)