Skip to content

Commit bfedd4a

Browse files
committed
Don't let -s silence .py imports from stubs. Fix #1364. (#1372)
Implement the idea in the issue by Ben Darnell. (However, this implementation still silences the case where main.py -> stub.pyi -> x.py -> y.py. Hopefully that doesn't matter.)
1 parent 41b7e02 commit bfedd4a

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

mypy/build.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -969,12 +969,18 @@ def __init__(self,
969969
file_id = '__builtin__'
970970
path = find_module(file_id, manager.lib_path)
971971
if path:
972-
# In silent mode, don't import .py files.
972+
# In silent mode, don't import .py files, except from stubs.
973973
if (SILENT_IMPORTS in manager.flags and
974974
path.endswith('.py') and (caller_state or is_ancestor)):
975-
path = None
976-
manager.missing_modules.add(id)
977-
raise ModuleNotFound
975+
# (Never silence builtins, even if it's a .py file;
976+
# this can happen in tests!)
977+
if (id != 'builtins' and
978+
not ((caller_state and
979+
caller_state.tree and
980+
caller_state.tree.is_stub))):
981+
path = None
982+
manager.missing_modules.add(id)
983+
raise ModuleNotFound
978984
else:
979985
# Could not find a module. Typically the reason is a
980986
# misspelled module name, missing stub, module not in

mypy/test/data/check-modules.test

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,3 +698,21 @@ class C(B):
698698
pass
699699
[out]
700700
tmp/bar.py:1: error: Module has no attribute 'B'
701+
702+
[case testStubImportNonStubWhileSilent]
703+
# cmd: mypy -m main
704+
[file main.py]
705+
# flags: silent-imports
706+
from stub import x # Permitted
707+
from other import y # Disallowed
708+
x + '' # Error here
709+
y + '' # But not here
710+
[file stub.pyi]
711+
from non_stub import x
712+
[file non_stub.py]
713+
x = 42
714+
[file other.py]
715+
y = 42
716+
[builtins fixtures/module.py]
717+
[out]
718+
tmp/main.py:4: error: Unsupported left operand type for + ("int")

0 commit comments

Comments
 (0)