Skip to content

Commit 0dfa86e

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 0dfa86e

File tree

5 files changed

+59
-12
lines changed

5 files changed

+59
-12
lines changed

build-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
-r mypy-requirements.txt
2+
types-setuptools
23
types-typed-ast>=1.5.0,<1.6.0

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: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222
except ImportError:
2323
CURSES_ENABLED = False
2424

25+
try:
26+
import pkg_resources
27+
except ImportError:
28+
TYPESHED_DIR = os.path.join(os.path.dirname(__file__), 'typeshed')
29+
else:
30+
TYPESHED_DIR = pkg_resources.resource_filename(__package__, 'typeshed')
31+
2532
T = TypeVar('T')
2633

2734
ENCODING_RE: Final = re.compile(br"([ \t\v]*#.*(\r\n?|\n))??[ \t\v]*#.*coding[:=][ \t]*([-\w.]+)")
@@ -745,16 +752,18 @@ def format_error(
745752

746753

747754
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)
755+
try:
756+
return os.path.commonpath((TYPESHED_DIR, os.path.abspath(file))) == TYPESHED_DIR
757+
except ValueError: # Different drives on Windows
758+
return False
750759

751760

752761
def is_stub_package_file(file: str) -> bool:
753762
# Use hacky heuristics to check whether file is part of a PEP 561 stub package.
754763
if not file.endswith('.pyi'):
755764
return False
756765
return any(component.endswith('-stubs')
757-
for component in os.path.abspath(file).split(os.sep))
766+
for component in os.path.split(os.path.abspath(file)))
758767

759768

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

test-data/unit/cmdline.test

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

0 commit comments

Comments
 (0)