Skip to content

Commit 86f3e7a

Browse files
Fix dmypy re-adding modules excluded by per-module follow_imports=skip
Fixes #16190
1 parent 82dd3af commit 86f3e7a

3 files changed

Lines changed: 41 additions & 5 deletions

File tree

mypy/build.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3806,6 +3806,16 @@ def find_module_and_diagnose(
38063806
raise ModuleNotFound
38073807

38083808

3809+
def excluded_by_follow_imports(path: str, options: Options) -> bool:
3810+
"""Check if the module at path is excluded from build by follow-imports=skip/error.
3811+
3812+
Stubs are only excluded if follow_imports_for_stubs is set.
3813+
"""
3814+
return options.follow_imports in ("skip", "error") and (
3815+
not path.endswith(".pyi") or options.follow_imports_for_stubs
3816+
)
3817+
3818+
38093819
def exist_added_packages(suppressed: list[str], manager: BuildManager) -> bool:
38103820
"""Find if there are any newly added packages that were previously suppressed.
38113821
@@ -3829,9 +3839,7 @@ def exist_added_packages(suppressed: list[str], manager: BuildManager) -> bool:
38293839
# follow-imports = normal
38303840
# But such cases are extremely rare, and this allows us to avoid
38313841
# massive performance impact in much more common situations.
3832-
if options.follow_imports in ("skip", "error") and (
3833-
not path.endswith(".pyi") or options.follow_imports_for_stubs
3834-
):
3842+
if excluded_by_follow_imports(path, options):
38353843
continue
38363844
if os.path.basename(path) in ("__init__.py", "__init__.pyi"):
38373845
return True

mypy/dmypy_server.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -825,8 +825,9 @@ def find_added_suppressed(
825825
continue
826826
result = finder.find_module(module, fast_path=True)
827827
if isinstance(result, str) and module not in seen:
828-
# When not following imports, we only follow imports to .pyi files.
829-
if not self.following_imports() and not result.endswith(".pyi"):
828+
if mypy.build.excluded_by_follow_imports(
829+
result, self.options.clone_for_module(module)
830+
):
830831
continue
831832
found.append((module, result))
832833
seen.add(module)

test-data/unit/fine-grained-follow-imports.test

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,3 +846,30 @@ class A: ...
846846
[typing fixtures/typing-typeddict.pyi]
847847
[out]
848848
==
849+
850+
[case testFollowImportsPerModuleSkipNotAddedBack]
851+
# flags: --follow-imports=normal
852+
# cmd: mypy main.py
853+
854+
[file mypy.ini]
855+
\[mypy]
856+
follow_imports = normal
857+
\[mypy-pkg.sub]
858+
follow_imports = skip
859+
860+
[file main.py]
861+
import pkg.sub
862+
reveal_type(pkg.sub.x)
863+
864+
[file pkg/__init__.py]
865+
866+
[file pkg/sub.py]
867+
x = 1
868+
869+
[file unrelated.py.2]
870+
# Trigger a second increment with no relevant changes.
871+
872+
[out]
873+
main.py:2: note: Revealed type is "Any"
874+
==
875+
main.py:2: note: Revealed type is "Any"

0 commit comments

Comments
 (0)