Commit 43eb352
authored
Fix the stubgen test flake. (#4811)
Fix the stubgen test flake.
The core problem is that `importlib.invalidate_caches()` is full of
lies and does not actually invalidate all of the relevant
caches. Python maintains sys.path_importer_cache, which maps from
directory names on the sys.path to "importer" objects for that path
entry. `importlib.invalidate_caches()` invalidates the caches inside
each of the per-directory importer objects, but there is an additional
negative cache that is *not* cleared: sys.path entries whose directory
doesn't exist have their importer set to None, which prevents that
path entry from ever being searched unless it is manually cleared.
This failure comes up rarely, because it only occurs if the following
events occur in sequence:
1. 'stubgen-test-path' is added to sys.path, but no new import is
done while there is a 'stubgen-test-path' subdirectory in the working
directory. This is done by all the stubgen tests that don't end with
`_import`.
2. Some non-stubgen test does an import of a previously unimported
module. This will cause
sys.path_importer_cache['stubgen-test-path'] to be set to None
when the directory doesn't exist during the module search.
This can happen the *first* time that a `_python2` test is run
since `parse.parse()` dynamically imports `mypy.fastparse2`
when asked to parse python 2.
3. A stubgen test tries to use importlib to import a module in
'stubgen-test-path'. All of the `_import` tests do this.
We fix this by clearing out the relevant entry from
`sys.path_importer_cache` ourselves.
Fixes #46351 parent 751b4ef commit 43eb352
2 files changed
+11
-39
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
153 | 153 | | |
154 | 154 | | |
155 | 155 | | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | 156 | | |
164 | 157 | | |
165 | 158 | | |
| |||
175 | 168 | | |
176 | 169 | | |
177 | 170 | | |
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 | 171 | | |
204 | 172 | | |
205 | 173 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | 2 | | |
4 | 3 | | |
5 | 4 | | |
| |||
125 | 124 | | |
126 | 125 | | |
127 | 126 | | |
128 | | - | |
| 127 | + | |
129 | 128 | | |
130 | 129 | | |
131 | 130 | | |
| |||
145 | 144 | | |
146 | 145 | | |
147 | 146 | | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
153 | 157 | | |
154 | 158 | | |
155 | 159 | | |
| |||
0 commit comments