|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | 5 | import ast |
| 6 | +import contextlib |
| 7 | +import sys |
6 | 8 | from inspect import Parameter, Signature, getsource |
| 9 | +from types import ModuleType |
7 | 10 | from typing import TYPE_CHECKING, cast |
| 11 | +from weakref import WeakSet |
8 | 12 |
|
| 13 | +from sphinx.errors import PycodeError |
9 | 14 | from sphinx.locale import __ |
| 15 | +from sphinx.pycode import ModuleAnalyzer |
10 | 16 | from sphinx.pycode.ast import unparse as ast_unparse |
11 | 17 | from sphinx.util import inspect, logging |
| 18 | +from sphinx.util.inspect import safe_getattr |
12 | 19 |
|
13 | 20 | if TYPE_CHECKING: |
14 | 21 | from collections.abc import Sequence |
|
18 | 25 | logger = logging.getLogger(__name__) |
19 | 26 |
|
20 | 27 |
|
21 | | -def not_suppressed(argtypes: Sequence[ast.expr] = ()) -> bool: |
22 | | - """Check given *argtypes* is suppressed type_comment or not.""" |
23 | | - if len(argtypes) == 0: # no argtypees |
24 | | - return False |
25 | | - if len(argtypes) == 1: |
26 | | - arg = argtypes[0] |
27 | | - if isinstance(arg, ast.Constant) and arg.value is ...: # suppressed |
28 | | - return False |
29 | | - # not suppressed |
30 | | - return True |
| 28 | +_objects_with_type_comment_annotations: WeakSet[Any] = WeakSet() |
| 29 | +"""Cache of objects with annotations updated from type comments.""" |
| 30 | + |
| 31 | + |
| 32 | +def _ensure_annotations_from_type_comments(obj: Any) -> None: |
| 33 | + """Ensures `obj.__annotations__` includes type comment information. |
| 34 | +
|
| 35 | + Failures to assign to `__annotations__` are silently ignored. |
| 36 | +
|
| 37 | + If `obj` is a class type, this also ensures that type comment |
| 38 | + information is incorporated into the `__annotations__` member of |
| 39 | + all parent classes, if possible. |
| 40 | +
|
| 41 | + This mutates the `__annotations__` of existing imported objects, |
| 42 | + in order to allow the existing `typing.get_type_hints` method to |
| 43 | + take the modified annotations into account. |
| 44 | +
|
| 45 | + Modifying existing imported objects is unfortunate but avoids the |
| 46 | + need to reimplement `typing.get_type_hints` in order to take into |
| 47 | + account type comment information. |
| 48 | +
|
| 49 | + Note that this does not directly include type comment information |
| 50 | + from parent classes, but `typing.get_type_hints` takes that into |
| 51 | + account. |
| 52 | + """ |
| 53 | + if obj in _objects_with_type_comment_annotations: |
| 54 | + return |
| 55 | + _objects_with_type_comment_annotations.add(obj) |
| 56 | + |
| 57 | + if isinstance(obj, type): |
| 58 | + for cls in inspect.getmro(obj): |
| 59 | + modname = safe_getattr(cls, '__module__') |
| 60 | + mod = sys.modules.get(modname) |
| 61 | + if mod is not None: |
| 62 | + _ensure_annotations_from_type_comments(mod) |
| 63 | + |
| 64 | + elif isinstance(obj, ModuleType): |
| 65 | + _update_module_annotations_from_type_comments(obj) |
| 66 | + |
| 67 | + |
| 68 | +def _update_module_annotations_from_type_comments(mod: ModuleType) -> None: |
| 69 | + """Adds type comment annotations for a single module. |
| 70 | +
|
| 71 | + Both module-level and class-level annotations are added. |
| 72 | + """ |
| 73 | + mod_annotations = dict(inspect.getannotations(mod)) |
| 74 | + mod.__annotations__ = mod_annotations |
| 75 | + |
| 76 | + class_annotations: dict[str, dict[str, Any]] = {} |
| 77 | + |
| 78 | + try: |
| 79 | + analyzer = ModuleAnalyzer.for_module(mod.__name__) |
| 80 | + analyzer.analyze() |
| 81 | + anns = analyzer.annotations |
| 82 | + for (classname, attrname), annotation in anns.items(): |
| 83 | + if not classname: |
| 84 | + annotations = mod_annotations |
| 85 | + else: |
| 86 | + cls_annotations = class_annotations.get(classname) |
| 87 | + if cls_annotations is None: |
| 88 | + try: |
| 89 | + cls = mod |
| 90 | + for part in classname.split('.'): |
| 91 | + cls = safe_getattr(cls, part) |
| 92 | + annotations = dict(inspect.getannotations(cls)) |
| 93 | + # Ignore errors setting __annotations__ |
| 94 | + with contextlib.suppress(TypeError, AttributeError): |
| 95 | + cls.__annotations__ = annotations |
| 96 | + except AttributeError: |
| 97 | + annotations = {} |
| 98 | + class_annotations[classname] = annotations |
| 99 | + else: |
| 100 | + annotations = cls_annotations |
| 101 | + annotations.setdefault(attrname, annotation) |
| 102 | + except PycodeError: |
| 103 | + pass |
| 104 | + |
| 105 | + |
| 106 | +def _update_annotations_using_type_comments(obj: Any, bound_method: bool) -> None: |
| 107 | + """Update annotations info of *obj* using type_comments.""" |
| 108 | + try: |
| 109 | + type_sig = get_type_comment(obj, bound_method) |
| 110 | + if type_sig: |
| 111 | + sig = inspect.signature(obj, bound_method) |
| 112 | + for param in sig.parameters.values(): |
| 113 | + if param.name not in obj.__annotations__: |
| 114 | + annotation = type_sig.parameters[param.name].annotation |
| 115 | + if annotation is not Parameter.empty: |
| 116 | + obj.__annotations__[param.name] = ast_unparse(annotation) |
| 117 | + |
| 118 | + if 'return' not in obj.__annotations__: |
| 119 | + obj.__annotations__['return'] = type_sig.return_annotation |
| 120 | + except KeyError as exc: |
| 121 | + logger.warning( |
| 122 | + __('Failed to update signature for %r: parameter not found: %s'), obj, exc |
| 123 | + ) |
| 124 | + except NotImplementedError as exc: # failed to ast.unparse() |
| 125 | + logger.warning(__('Failed to parse type_comment for %r: %s'), obj, exc) |
| 126 | + |
| 127 | + |
| 128 | +def get_type_comment(obj: Any, bound_method: bool = False) -> Signature | None: |
| 129 | + """Get type_comment'ed FunctionDef object from living object. |
| 130 | +
|
| 131 | + This tries to parse original code for living object and returns |
| 132 | + Signature for given *obj*. |
| 133 | + """ |
| 134 | + try: |
| 135 | + source = getsource(obj) |
| 136 | + if source.startswith((' ', r'\t')): |
| 137 | + # subject is placed inside class or block. To read its docstring, |
| 138 | + # this adds if-block before the declaration. |
| 139 | + module = ast.parse('if True:\n' + source, type_comments=True) |
| 140 | + subject = cast('ast.FunctionDef', module.body[0].body[0]) # type: ignore[attr-defined] |
| 141 | + else: |
| 142 | + module = ast.parse(source, type_comments=True) |
| 143 | + subject = cast('ast.FunctionDef', module.body[0]) |
| 144 | + |
| 145 | + type_comment = getattr(subject, 'type_comment', None) |
| 146 | + if type_comment: |
| 147 | + function = ast.parse(type_comment, mode='func_type', type_comments=True) |
| 148 | + return signature_from_ast(subject, bound_method, function) # type: ignore[arg-type] |
| 149 | + else: |
| 150 | + return None |
| 151 | + except (OSError, TypeError): # failed to load source code |
| 152 | + return None |
| 153 | + except SyntaxError: # failed to parse type_comments |
| 154 | + return None |
31 | 155 |
|
32 | 156 |
|
33 | 157 | def signature_from_ast( |
@@ -95,52 +219,13 @@ def signature_from_ast( |
95 | 219 | return Signature(params) |
96 | 220 |
|
97 | 221 |
|
98 | | -def get_type_comment(obj: Any, bound_method: bool = False) -> Signature | None: |
99 | | - """Get type_comment'ed FunctionDef object from living object. |
100 | | -
|
101 | | - This tries to parse original code for living object and returns |
102 | | - Signature for given *obj*. |
103 | | - """ |
104 | | - try: |
105 | | - source = getsource(obj) |
106 | | - if source.startswith((' ', r'\t')): |
107 | | - # subject is placed inside class or block. To read its docstring, |
108 | | - # this adds if-block before the declaration. |
109 | | - module = ast.parse('if True:\n' + source, type_comments=True) |
110 | | - subject = cast('ast.FunctionDef', module.body[0].body[0]) # type: ignore[attr-defined] |
111 | | - else: |
112 | | - module = ast.parse(source, type_comments=True) |
113 | | - subject = cast('ast.FunctionDef', module.body[0]) |
114 | | - |
115 | | - type_comment = getattr(subject, 'type_comment', None) |
116 | | - if type_comment: |
117 | | - function = ast.parse(type_comment, mode='func_type', type_comments=True) |
118 | | - return signature_from_ast(subject, bound_method, function) # type: ignore[arg-type] |
119 | | - else: |
120 | | - return None |
121 | | - except (OSError, TypeError): # failed to load source code |
122 | | - return None |
123 | | - except SyntaxError: # failed to parse type_comments |
124 | | - return None |
125 | | - |
126 | | - |
127 | | -def _update_annotations_using_type_comments(obj: Any, bound_method: bool) -> None: |
128 | | - """Update annotations info of *obj* using type_comments.""" |
129 | | - try: |
130 | | - type_sig = get_type_comment(obj, bound_method) |
131 | | - if type_sig: |
132 | | - sig = inspect.signature(obj, bound_method) |
133 | | - for param in sig.parameters.values(): |
134 | | - if param.name not in obj.__annotations__: |
135 | | - annotation = type_sig.parameters[param.name].annotation |
136 | | - if annotation is not Parameter.empty: |
137 | | - obj.__annotations__[param.name] = ast_unparse(annotation) |
138 | | - |
139 | | - if 'return' not in obj.__annotations__: |
140 | | - obj.__annotations__['return'] = type_sig.return_annotation |
141 | | - except KeyError as exc: |
142 | | - logger.warning( |
143 | | - __('Failed to update signature for %r: parameter not found: %s'), obj, exc |
144 | | - ) |
145 | | - except NotImplementedError as exc: # failed to ast.unparse() |
146 | | - logger.warning(__('Failed to parse type_comment for %r: %s'), obj, exc) |
| 222 | +def not_suppressed(argtypes: Sequence[ast.expr] = ()) -> bool: |
| 223 | + """Check given *argtypes* is suppressed type_comment or not.""" |
| 224 | + if len(argtypes) == 0: # no argtypees |
| 225 | + return False |
| 226 | + if len(argtypes) == 1: |
| 227 | + arg = argtypes[0] |
| 228 | + if isinstance(arg, ast.Constant) and arg.value is ...: # suppressed |
| 229 | + return False |
| 230 | + # not suppressed |
| 231 | + return True |
0 commit comments