Skip to content

Commit d40f6d4

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 d40f6d4

File tree

4 files changed

+41
-11
lines changed

4 files changed

+41
-11
lines changed

mypy/build.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import gc
1616
import json
1717
import os
18+
import pathlib
1819
import platform
1920
import re
2021
import stat
@@ -605,6 +606,29 @@ def __init__(self, data_dir: str,
605606
self.fscache = fscache
606607
self.find_module_cache = FindModuleCache(self.search_paths, self.fscache, self.options,
607608
source_set=self.source_set)
609+
for module in CORE_BUILTIN_MODULES:
610+
if options.use_builtins_fixtures:
611+
continue
612+
if module == "_importlib_modulespec":
613+
continue
614+
path = self.find_module_cache.find_module(module)
615+
if not isinstance(path, str):
616+
raise CompileError([
617+
f"Failed to find builtin module {module}, perhaps typeshed is broken?",
618+
])
619+
if is_typeshed_file(path):
620+
continue
621+
if is_stub_package_file(path):
622+
continue
623+
if options.custom_typeshed_dir is not None:
624+
if pathlib.Path(path) in pathlib.Path(options.custom_typeshed_dir).parents:
625+
continue
626+
627+
raise CompileError([
628+
f'mypy: "{os.path.relpath(path)}" shadows library module "{module}"',
629+
f'note: A user-defined top-level module with name "{module}" is not supported'
630+
])
631+
608632
self.metastore = create_metastore(options)
609633

610634
# a mapping from source files to their corresponding shadow files
@@ -2459,15 +2483,6 @@ def find_module_and_diagnose(manager: BuildManager,
24592483
if is_sub_path(result, dir):
24602484
# Silence errors in site-package dirs and typeshed
24612485
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-
])
24712486
return (result, follow_imports)
24722487
else:
24732488
# 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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -746,15 +746,15 @@ def format_error(
746746

747747
def is_typeshed_file(file: str) -> bool:
748748
# gross, but no other clear way to tell
749-
return 'typeshed' in os.path.abspath(file).split(os.sep)
749+
return 'typeshed' in pathlib.Path(file).absolute().parts
750750

751751

752752
def is_stub_package_file(file: str) -> bool:
753753
# Use hacky heuristics to check whether file is part of a PEP 561 stub package.
754754
if not file.endswith('.pyi'):
755755
return False
756756
return any(component.endswith('-stubs')
757-
for component in os.path.abspath(file).split(os.sep))
757+
for component in pathlib.Path(file).absolute().parts)
758758

759759

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

test-data/unit/cmdline.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,3 +1425,17 @@ 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+
class Bar:
1437+
pass
1438+
[out]
1439+
mypy: "tmp/typing.py" shadows library module "typing"
1440+
note: A user-defined top-level module with name "typing" is not supported
1441+
== Return code: 2

0 commit comments

Comments
 (0)