|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | 5 | import sys |
| 6 | +import types |
6 | 7 | import typing |
7 | 8 | from collections.abc import Sequence |
| 9 | +from contextvars import Context, ContextVar, Token |
8 | 10 | from struct import Struct |
9 | | -from types import TracebackType |
10 | 11 | from typing import TYPE_CHECKING, Any, Callable, ForwardRef, TypedDict, TypeVar, Union |
11 | 12 |
|
12 | 13 | from docutils import nodes |
|
22 | 23 | else: |
23 | 24 | UnionType = None |
24 | 25 |
|
25 | | -# classes that have incorrect __module__ |
26 | | -INVALID_BUILTIN_CLASSES = { |
| 26 | +# classes that have an incorrect .__module__ attribute |
| 27 | +_INVALID_BUILTIN_CLASSES = { |
| 28 | + Context: 'contextvars.Context', # Context.__module__ == '_contextvars' |
| 29 | + ContextVar: 'contextvars.ContextVar', # ContextVar.__module__ == '_contextvars' |
| 30 | + Token: 'contextvars.Token', # Token.__module__ == '_contextvars' |
27 | 31 | Struct: 'struct.Struct', # Struct.__module__ == '_struct' |
28 | | - TracebackType: 'types.TracebackType', # TracebackType.__module__ == 'builtins' |
| 32 | + # types in 'types' with <type>.__module__ == 'builtins': |
| 33 | + types.AsyncGeneratorType: 'types.AsyncGeneratorType', |
| 34 | + types.BuiltinFunctionType: 'types.BuiltinFunctionType', |
| 35 | + types.BuiltinMethodType: 'types.BuiltinMethodType', |
| 36 | + types.CellType: 'types.CellType', |
| 37 | + types.ClassMethodDescriptorType: 'types.ClassMethodDescriptorType', |
| 38 | + types.CodeType: 'types.CodeType', |
| 39 | + types.CoroutineType: 'types.CoroutineType', |
| 40 | + types.FrameType: 'types.FrameType', |
| 41 | + types.FunctionType: 'types.FunctionType', |
| 42 | + types.GeneratorType: 'types.GeneratorType', |
| 43 | + types.GetSetDescriptorType: 'types.GetSetDescriptorType', |
| 44 | + types.LambdaType: 'types.LambdaType', |
| 45 | + types.MappingProxyType: 'types.MappingProxyType', |
| 46 | + types.MemberDescriptorType: 'types.MemberDescriptorType', |
| 47 | + types.MethodDescriptorType: 'types.MethodDescriptorType', |
| 48 | + types.MethodType: 'types.MethodType', |
| 49 | + types.MethodWrapperType: 'types.MethodWrapperType', |
| 50 | + types.ModuleType: 'types.ModuleType', |
| 51 | + types.TracebackType: 'types.TracebackType', |
| 52 | + types.WrapperDescriptorType: 'types.WrapperDescriptorType', |
29 | 53 | } |
30 | 54 |
|
31 | 55 |
|
32 | 56 | def is_invalid_builtin_class(obj: Any) -> bool: |
33 | 57 | """Check *obj* is an invalid built-in class.""" |
34 | 58 | try: |
35 | | - return obj in INVALID_BUILTIN_CLASSES |
| 59 | + return obj in _INVALID_BUILTIN_CLASSES |
36 | 60 | except TypeError: # unhashable type |
37 | 61 | return False |
38 | 62 |
|
@@ -143,7 +167,7 @@ def restify(cls: type | None, mode: str = 'fully-qualified-except-typing') -> st |
143 | 167 | elif ismock(cls): |
144 | 168 | return f':py:class:`{modprefix}{cls.__module__}.{cls.__name__}`' |
145 | 169 | elif is_invalid_builtin_class(cls): |
146 | | - return f':py:class:`{modprefix}{INVALID_BUILTIN_CLASSES[cls]}`' |
| 170 | + return f':py:class:`{modprefix}{_INVALID_BUILTIN_CLASSES[cls]}`' |
147 | 171 | elif inspect.isNewType(cls): |
148 | 172 | if sys.version_info[:2] >= (3, 10): |
149 | 173 | # newtypes have correct module info since Python 3.10+ |
@@ -300,7 +324,7 @@ def stringify_annotation( |
300 | 324 | elif ismock(annotation): |
301 | 325 | return module_prefix + f'{annotation_module}.{annotation_name}' |
302 | 326 | elif is_invalid_builtin_class(annotation): |
303 | | - return module_prefix + INVALID_BUILTIN_CLASSES[annotation] |
| 327 | + return module_prefix + _INVALID_BUILTIN_CLASSES[annotation] |
304 | 328 | elif str(annotation).startswith('typing.Annotated'): # for py310+ |
305 | 329 | pass |
306 | 330 | elif annotation_module == 'builtins' and annotation_qualname: |
|
0 commit comments