Commit 3bb7231
authored
🐛 fix(resolver): resolve NamedTuple hints from class (#657)
Fixes #656
NamedTuples with non-builtin type annotations (e.g. `Path`) combined
with `from __future__ import annotations` produce spurious "Cannot
resolve forward reference" warnings since 3.9.6. 🐛 This happens because
the `__new__` method generated for NamedTuples has its `__module__` set
to a synthetic name like `namedtuple_Program` that doesn't exist in
`sys.modules`, so `typing.get_type_hints()` can't resolve the
stringified annotations.
The fix passes the original class to `get_all_type_hints()` instead of
`__new__` when dealing with NamedTuple-style classes (custom `__new__`,
inherited `__init__`). The class has the correct `__module__` pointing
to the real module where the imports live, while `__new__` continues to
be used for signature extraction as intended by the 3.9.6 C extension
support change.1 parent eed675c commit 3bb7231
File tree
3 files changed
+33
-2
lines changed- src/sphinx_autodoc_typehints
- tests
- roots/test-dummy
3 files changed
+33
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
152 | 152 | | |
153 | 153 | | |
154 | 154 | | |
| 155 | + | |
155 | 156 | | |
156 | 157 | | |
157 | 158 | | |
| 159 | + | |
158 | 160 | | |
159 | 161 | | |
160 | 162 | | |
| |||
176 | 178 | | |
177 | 179 | | |
178 | 180 | | |
179 | | - | |
| 181 | + | |
180 | 182 | | |
181 | 183 | | |
182 | 184 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
5 | 8 | | |
6 | 9 | | |
7 | 10 | | |
| |||
23 | 26 | | |
24 | 27 | | |
25 | 28 | | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
26 | 36 | | |
27 | 37 | | |
28 | 38 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
166 | 166 | | |
167 | 167 | | |
168 | 168 | | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
169 | 188 | | |
170 | 189 | | |
171 | 190 | | |
| |||
0 commit comments