Commit d4143d2
committed
Keep _-prefixed module imports referenced by emitted stubs
`mypy_type_check` composes a stub from `collect_imports`,
`collect_runtime_type_stubs`, and `collect_variable_declarations`.
The variable-declaration step can emit annotations like
`request: _pytest.fixtures.TopRequest` whenever a context value's
runtime type lives in a `_`-prefixed module (pytest's `request`
fixture is the canonical example). `collect_imports` was
unconditionally dropping every `_`-prefixed entry from
`sys.modules`, so mypy then crashed with `Name "_pytest" is not
defined`.
Drop the `_`-prefix filter in `collect_imports`. The first-character
check now reads `k[0].isalpha() or k[0] == "_"`, which admits valid
Python identifier prefixes (including the `_pytest`-style internal
modules emitted stubs may reference) and still excludes mypyc-internal
sys.modules entries with UUID-prefixed names like
`4c842c94c09923bae9e4__mypyc` that would otherwise generate invalid
`import` statements. `autoflake.remove_all_unused_imports` already
strips anything the stubs/body don't reference, so over-emission is
free.
Closes #674.1 parent e43739b commit d4143d2
2 files changed
Lines changed: 18 additions & 145 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
248 | 248 | | |
249 | 249 | | |
250 | 250 | | |
251 | | - | |
252 | | - | |
253 | | - | |
254 | | - | |
255 | | - | |
256 | | - | |
257 | | - | |
258 | | - | |
259 | | - | |
260 | | - | |
261 | | - | |
262 | | - | |
263 | | - | |
264 | | - | |
265 | | - | |
266 | | - | |
267 | | - | |
268 | | - | |
269 | | - | |
270 | | - | |
271 | | - | |
272 | | - | |
273 | | - | |
274 | | - | |
275 | | - | |
276 | | - | |
277 | | - | |
278 | | - | |
279 | | - | |
280 | | - | |
281 | | - | |
282 | | - | |
283 | | - | |
284 | | - | |
285 | | - | |
| 251 | + | |
286 | 252 | | |
287 | 253 | | |
288 | 254 | | |
289 | 255 | | |
290 | 256 | | |
291 | 257 | | |
292 | | - | |
293 | | - | |
294 | | - | |
295 | | - | |
296 | | - | |
297 | | - | |
298 | 258 | | |
299 | 259 | | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
300 | 265 | | |
301 | 266 | | |
302 | 267 | | |
303 | | - | |
304 | | - | |
305 | | - | |
306 | | - | |
307 | | - | |
308 | | - | |
309 | | - | |
| 268 | + | |
310 | 269 | | |
311 | 270 | | |
312 | 271 | | |
| |||
618 | 577 | | |
619 | 578 | | |
620 | 579 | | |
| 580 | + | |
621 | 581 | | |
622 | 582 | | |
623 | 583 | | |
| |||
628 | 588 | | |
629 | 589 | | |
630 | 590 | | |
631 | | - | |
632 | | - | |
633 | | - | |
634 | | - | |
635 | | - | |
636 | | - | |
637 | | - | |
638 | | - | |
639 | | - | |
640 | | - | |
641 | | - | |
642 | 591 | | |
643 | 592 | | |
644 | 593 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
23 | 22 | | |
24 | 23 | | |
25 | 24 | | |
| |||
132 | 131 | | |
133 | 132 | | |
134 | 133 | | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
139 | 140 | | |
140 | 141 | | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
| 142 | + | |
170 | 143 | | |
171 | 144 | | |
172 | 145 | | |
173 | 146 | | |
174 | 147 | | |
175 | 148 | | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | | - | |
214 | | - | |
215 | | - | |
216 | | - | |
217 | | - | |
218 | | - | |
219 | | - | |
220 | | - | |
221 | | - | |
222 | | - | |
223 | | - | |
224 | | - | |
| 149 | + | |
| 150 | + | |
225 | 151 | | |
226 | 152 | | |
227 | 153 | | |
| |||
974 | 900 | | |
975 | 901 | | |
976 | 902 | | |
977 | | - | |
978 | | - | |
979 | | - | |
| 903 | + | |
980 | 904 | | |
981 | 905 | | |
982 | 906 | | |
| |||
0 commit comments