Skip to content

Commit c4b4949

Browse files
ilinumddfisher
authored andcommitted
Do not follow imports from stubs with --follow-imports=skip (#3738)
If such imports are followed, unanalyzed modules can become analyzed when not intended. Fixes #3727.
1 parent 417e925 commit c4b4949

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

mypy/build.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,12 +1377,9 @@ def __init__(self,
13771377
# - skip -> don't analyze, make the type Any
13781378
follow_imports = self.options.follow_imports
13791379
if (follow_imports != 'normal'
1380-
and not root_source # Honor top-level modules
1381-
and path.endswith('.py') # Stubs are always normal
1382-
and id != 'builtins' # Builtins is always normal
1383-
and not (caller_state and
1384-
caller_state.tree and
1385-
caller_state.tree.is_stub)):
1380+
and not root_source # Honor top-level modules
1381+
and path.endswith('.py') # Stubs are always normal
1382+
and id != 'builtins'): # Builtins is always normal
13861383
if follow_imports == 'silent':
13871384
# Still import it, but silence non-blocker errors.
13881385
manager.log("Silencing %s (%s)" % (path, id))

test-data/unit/check-modules.test

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -955,19 +955,24 @@ tmp/foo/bar.py: note: (Using --follow-imports=error, submodule passed on command
955955
# cmd: mypy -m main
956956
# flags: --follow-imports=skip
957957
[file main.py]
958-
from stub import x # Permitted
959-
from other import y # Disallowed
960-
x + '' # Error here
961-
y + '' # But not here
958+
from stub import x, z # Followed
959+
from other import y # Not followed
960+
x + '' # No error here
961+
y + '' # No error here
962+
z + '' # Error here
962963
[file stub.pyi]
963-
from non_stub import x
964+
from non_stub import x # this import is not followed
965+
966+
z = 42
964967
[file non_stub.py]
965968
x = 42
969+
970+
x + '' # no error because file is not analyzed
966971
[file other.py]
967972
y = 42
968973
[builtins fixtures/module.pyi]
969974
[out]
970-
tmp/main.py:3: error: Unsupported left operand type for + ("int")
975+
tmp/main.py:5: error: Unsupported left operand type for + ("int")
971976

972977
[case testSilentSubmoduleImport]
973978
# cmd: mypy -m foo

0 commit comments

Comments
 (0)