Skip to content

Don't let -s silence .py imports from stubs. Fix #1364. #1372

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

Merged
merged 1 commit into from
Apr 13, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -969,12 +969,18 @@ def __init__(self,
file_id = '__builtin__'
path = find_module(file_id, manager.lib_path)
if path:
# In silent mode, don't import .py files.
# In silent mode, don't import .py files, except from stubs.
if (SILENT_IMPORTS in manager.flags and
path.endswith('.py') and (caller_state or is_ancestor)):
path = None
manager.missing_modules.add(id)
raise ModuleNotFound
# (Never silence builtins, even if it's a .py file;
# this can happen in tests!)
if (id != 'builtins' and
not ((caller_state and
caller_state.tree and
caller_state.tree.is_stub))):
path = None
manager.missing_modules.add(id)
raise ModuleNotFound
else:
# Could not find a module. Typically the reason is a
# misspelled module name, missing stub, module not in
Expand Down
18 changes: 18 additions & 0 deletions mypy/test/data/check-modules.test
Original file line number Diff line number Diff line change
Expand Up @@ -698,3 +698,21 @@ class C(B):
pass
[out]
tmp/bar.py:1: error: Module has no attribute 'B'

[case testStubImportNonStubWhileSilent]
# cmd: mypy -m main
[file main.py]
# flags: silent-imports
from stub import x # Permitted
from other import y # Disallowed
x + '' # Error here
y + '' # But not here
[file stub.pyi]
from non_stub import x
[file non_stub.py]
x = 42
[file other.py]
y = 42
[builtins fixtures/module.py]
[out]
tmp/main.py:4: error: Unsupported left operand type for + ("int")