Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash on follow_imports_for_stubs #15407

Merged
merged 2 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@
"types",
"typing_extensions",
"mypy_extensions",
"_importlib_modulespec",
"_typeshed",
"_collections_abc",
"collections",
"collections.abc",
"sys",
"abc",
}
Expand Down Expand Up @@ -659,8 +662,6 @@ def __init__(
for module in CORE_BUILTIN_MODULES:
if options.use_builtins_fixtures:
continue
if module == "_importlib_modulespec":
continue
path = self.find_module_cache.find_module(module)
if not isinstance(path, str):
raise CompileError(
Expand Down Expand Up @@ -2637,7 +2638,7 @@ def find_module_and_diagnose(
result.endswith(".pyi") # Stubs are always normal
and not options.follow_imports_for_stubs # except when they aren't
)
or id in mypy.semanal_main.core_modules # core is always normal
or id in CORE_BUILTIN_MODULES # core is always normal
):
follow_imports = "normal"
if skip_diagnose:
Expand Down
2 changes: 1 addition & 1 deletion mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -2437,7 +2437,7 @@ def format_literal_value(typ: LiteralType) -> str:
if isinstance(typ, Instance):
itype = typ
# Get the short name of the type.
if itype.type.fullname in ("types.ModuleType", "_importlib_modulespec.ModuleType"):
if itype.type.fullname == "types.ModuleType":
# Make some common error messages simpler and tidier.
base_str = "Module"
if itype.extra_attrs and itype.extra_attrs.mod_name and module_names:
Expand Down
10 changes: 10 additions & 0 deletions test-data/unit/check-flags.test
Original file line number Diff line number Diff line change
Expand Up @@ -2174,3 +2174,13 @@ def f(x: bytes, y: bytearray, z: memoryview) -> None:
x in y
x in z
[builtins fixtures/primitives.pyi]

[case testNoCrashFollowImportsForStubs]
# flags: --config-file tmp/mypy.ini
{**{"x": "y"}}

[file mypy.ini]
\[mypy]
follow_imports = skip
follow_imports_for_stubs = true
[builtins fixtures/dict.pyi]
8 changes: 8 additions & 0 deletions test-data/unit/cmdline.test
Original file line number Diff line number Diff line change
Expand Up @@ -1484,6 +1484,10 @@ note: A user-defined top-level module with name "typing" is not supported
[file dir/stdlib/types.pyi]
[file dir/stdlib/typing.pyi]
[file dir/stdlib/typing_extensions.pyi]
[file dir/stdlib/_typeshed.pyi]
[file dir/stdlib/_collections_abc.pyi]
[file dir/stdlib/collections/abc.pyi]
[file dir/stdlib/collections/__init__.pyi]
[file dir/stdlib/VERSIONS]
[out]
Failed to find builtin module mypy_extensions, perhaps typeshed is broken?
Expand Down Expand Up @@ -1523,6 +1527,10 @@ class dict: pass
[file dir/stdlib/typing.pyi]
[file dir/stdlib/mypy_extensions.pyi]
[file dir/stdlib/typing_extensions.pyi]
[file dir/stdlib/_typeshed.pyi]
[file dir/stdlib/_collections_abc.pyi]
[file dir/stdlib/collections/abc.pyi]
[file dir/stdlib/collections/__init__.pyi]
[file dir/stdlib/foo.pyi]
1() # Errors are reported if the file was explicitly passed on the command line
[file dir/stdlib/VERSIONS]
Expand Down