Skip to content

Commit e7b917e

Browse files
authored
Fix crash on follow_imports_for_stubs (python#15407)
1 parent 3cedd27 commit e7b917e

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

mypy/build.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@
116116
"types",
117117
"typing_extensions",
118118
"mypy_extensions",
119-
"_importlib_modulespec",
119+
"_typeshed",
120+
"_collections_abc",
121+
"collections",
122+
"collections.abc",
120123
"sys",
121124
"abc",
122125
}
@@ -659,8 +662,6 @@ def __init__(
659662
for module in CORE_BUILTIN_MODULES:
660663
if options.use_builtins_fixtures:
661664
continue
662-
if module == "_importlib_modulespec":
663-
continue
664665
path = self.find_module_cache.find_module(module)
665666
if not isinstance(path, str):
666667
raise CompileError(
@@ -2637,7 +2638,7 @@ def find_module_and_diagnose(
26372638
result.endswith(".pyi") # Stubs are always normal
26382639
and not options.follow_imports_for_stubs # except when they aren't
26392640
)
2640-
or id in mypy.semanal_main.core_modules # core is always normal
2641+
or id in CORE_BUILTIN_MODULES # core is always normal
26412642
):
26422643
follow_imports = "normal"
26432644
if skip_diagnose:

mypy/messages.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2437,7 +2437,7 @@ def format_literal_value(typ: LiteralType) -> str:
24372437
if isinstance(typ, Instance):
24382438
itype = typ
24392439
# Get the short name of the type.
2440-
if itype.type.fullname in ("types.ModuleType", "_importlib_modulespec.ModuleType"):
2440+
if itype.type.fullname == "types.ModuleType":
24412441
# Make some common error messages simpler and tidier.
24422442
base_str = "Module"
24432443
if itype.extra_attrs and itype.extra_attrs.mod_name and module_names:

test-data/unit/check-flags.test

+10
Original file line numberDiff line numberDiff line change
@@ -2174,3 +2174,13 @@ def f(x: bytes, y: bytearray, z: memoryview) -> None:
21742174
x in y
21752175
x in z
21762176
[builtins fixtures/primitives.pyi]
2177+
2178+
[case testNoCrashFollowImportsForStubs]
2179+
# flags: --config-file tmp/mypy.ini
2180+
{**{"x": "y"}}
2181+
2182+
[file mypy.ini]
2183+
\[mypy]
2184+
follow_imports = skip
2185+
follow_imports_for_stubs = true
2186+
[builtins fixtures/dict.pyi]

test-data/unit/cmdline.test

+8
Original file line numberDiff line numberDiff line change
@@ -1484,6 +1484,10 @@ note: A user-defined top-level module with name "typing" is not supported
14841484
[file dir/stdlib/types.pyi]
14851485
[file dir/stdlib/typing.pyi]
14861486
[file dir/stdlib/typing_extensions.pyi]
1487+
[file dir/stdlib/_typeshed.pyi]
1488+
[file dir/stdlib/_collections_abc.pyi]
1489+
[file dir/stdlib/collections/abc.pyi]
1490+
[file dir/stdlib/collections/__init__.pyi]
14871491
[file dir/stdlib/VERSIONS]
14881492
[out]
14891493
Failed to find builtin module mypy_extensions, perhaps typeshed is broken?
@@ -1523,6 +1527,10 @@ class dict: pass
15231527
[file dir/stdlib/typing.pyi]
15241528
[file dir/stdlib/mypy_extensions.pyi]
15251529
[file dir/stdlib/typing_extensions.pyi]
1530+
[file dir/stdlib/_typeshed.pyi]
1531+
[file dir/stdlib/_collections_abc.pyi]
1532+
[file dir/stdlib/collections/abc.pyi]
1533+
[file dir/stdlib/collections/__init__.pyi]
15261534
[file dir/stdlib/foo.pyi]
15271535
1() # Errors are reported if the file was explicitly passed on the command line
15281536
[file dir/stdlib/VERSIONS]

0 commit comments

Comments
 (0)