Skip to content

Commit 3d2f8a2

Browse files
author
Konstantin Ignatov
committed
Ensure builtin modules are from typeshed sooner
It should work now with custom-typeshed-dir. Fixes #1876
1 parent ca6357e commit 3d2f8a2

File tree

4 files changed

+49
-12
lines changed

4 files changed

+49
-12
lines changed

mypy/build.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,31 @@ def __init__(self, data_dir: str,
605605
self.fscache = fscache
606606
self.find_module_cache = FindModuleCache(self.search_paths, self.fscache, self.options,
607607
source_set=self.source_set)
608+
for module in CORE_BUILTIN_MODULES:
609+
if options.use_builtins_fixtures:
610+
continue
611+
if module == "_importlib_modulespec":
612+
continue
613+
path = self.find_module_cache.find_module(module)
614+
if not isinstance(path, str):
615+
raise CompileError([
616+
f"Failed to find builtin module {module}, perhaps typeshed is broken?",
617+
])
618+
if is_typeshed_file(path):
619+
continue
620+
if is_stub_package_file(path):
621+
continue
622+
if options.custom_typeshed_dir is not None:
623+
# Check if module lives under custom_typeshed_dir subtree
624+
custom_typeshed_dir = os.path.abspath(options.custom_typeshed_dir)
625+
if os.path.commonpath((path, custom_typeshed_dir)) == custom_typeshed_dir:
626+
continue
627+
628+
raise CompileError([
629+
f'mypy: "{os.path.relpath(path)}" shadows library module "{module}"',
630+
f'note: A user-defined top-level module with name "{module}" is not supported'
631+
])
632+
608633
self.metastore = create_metastore(options)
609634

610635
# a mapping from source files to their corresponding shadow files
@@ -2459,15 +2484,6 @@ def find_module_and_diagnose(manager: BuildManager,
24592484
if is_sub_path(result, dir):
24602485
# Silence errors in site-package dirs and typeshed
24612486
follow_imports = 'silent'
2462-
if (id in CORE_BUILTIN_MODULES
2463-
and not is_typeshed_file(result)
2464-
and not is_stub_package_file(result)
2465-
and not options.use_builtins_fixtures
2466-
and not options.custom_typeshed_dir):
2467-
raise CompileError([
2468-
f'mypy: "{os.path.relpath(result)}" shadows library module "{id}"',
2469-
f'note: A user-defined top-level module with name "{id}" is not supported'
2470-
])
24712487
return (result, follow_imports)
24722488
else:
24732489
# Could not find a module. Typically the reason is a

mypy/test/testgraph.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def test_scc(self) -> None:
3838
def _make_manager(self) -> BuildManager:
3939
errors = Errors()
4040
options = Options()
41+
options.use_builtins_fixtures = True
4142
fscache = FileSystemCache()
4243
search_paths = SearchPaths((), (), (), ())
4344
manager = BuildManager(

mypy/util.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
"__init__", "__new__", "__call__", "__init_subclass__", "__class_getitem__",
5252
))
5353

54+
TYPESHED_DIR: Final = os.path.join(os.path.dirname(__file__), 'typeshed')
55+
5456

5557
def is_dunder(name: str, exclude_special: bool = False) -> bool:
5658
"""Returns whether name is a dunder name.
@@ -745,16 +747,15 @@ def format_error(
745747

746748

747749
def is_typeshed_file(file: str) -> bool:
748-
# gross, but no other clear way to tell
749-
return 'typeshed' in os.path.abspath(file).split(os.sep)
750+
return os.path.commonpath((TYPESHED_DIR, os.path.abspath(file))) == TYPESHED_DIR
750751

751752

752753
def is_stub_package_file(file: str) -> bool:
753754
# Use hacky heuristics to check whether file is part of a PEP 561 stub package.
754755
if not file.endswith('.pyi'):
755756
return False
756757
return any(component.endswith('-stubs')
757-
for component in os.path.abspath(file).split(os.sep))
758+
for component in os.path.split(os.path.abspath(file)))
758759

759760

760761
def unnamed_function(name: Optional[str]) -> bool:

test-data/unit/cmdline.test

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,3 +1425,22 @@ b\.c \d+
14251425
# cmd: mypy --enable-incomplete-features a.py
14261426
[file a.py]
14271427
pass
1428+
1429+
[case testShadowTypingModuleEarlyLoad]
1430+
# cmd: mypy .
1431+
from typing import Union
1432+
1433+
def foo(a: Union[int, str]) -> str:
1434+
return str
1435+
[file typing.py]
1436+
import sys
1437+
import os
1438+
del sys.modules["typing"]
1439+
path = sys.path
1440+
sys.path.remove(os.getcwd())
1441+
from typing import *
1442+
sys.path = path
1443+
[out]
1444+
mypy: "tmp/typing.py" shadows library module "typing"
1445+
note: A user-defined top-level module with name "typing" is not supported
1446+
== Return code: 2

0 commit comments

Comments
 (0)