Skip to content

Commit 1e7b517

Browse files
emmatypingilevkivskyi
authored andcommitted
Silence modules in site-packages and typeshed (#5303)
Fixes #5260
1 parent 3d81dcb commit 1e7b517

File tree

6 files changed

+25
-7
lines changed

6 files changed

+25
-7
lines changed

mypy/build.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
from mypy.checker import TypeChecker
4141
from mypy.indirection import TypeIndirectionVisitor
4242
from mypy.errors import Errors, CompileError, report_internal_error
43-
from mypy.util import DecodeError, decode_python_encoding
43+
from mypy.util import DecodeError, decode_python_encoding, is_sub_path
4444
from mypy.report import Reports
4545
from mypy import moduleinfo
4646
from mypy.fixup import fixup_module
@@ -2387,7 +2387,11 @@ def find_module_and_diagnose(manager: BuildManager,
23872387
skipping_module(manager, caller_line, caller_state,
23882388
id, path)
23892389
raise ModuleNotFound
2390-
2390+
if not manager.options.no_silence_site_packages:
2391+
for dir in manager.search_paths.package_path + manager.search_paths.typeshed_path:
2392+
if is_sub_path(path, dir):
2393+
# Silence errors in site-package dirs and typeshed
2394+
follow_imports = 'silent'
23912395
return (path, follow_imports)
23922396
else:
23932397
# Could not find a module. Typically the reason is a

mypy/main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,9 @@ def add_invertible_flag(flag: str,
385385
'--no-site-packages', action='store_true',
386386
dest='special-opts:no_executable',
387387
help="do not search for installed PEP 561 compliant packages")
388+
imports_group.add_argument(
389+
'--no-silence-site-packages', action='store_true',
390+
help="Do not silence errors in PEP 561 compliant installed packages")
388391

389392
platform_group = parser.add_argument_group(
390393
title='platform configuration',

mypy/options.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ def __init__(self) -> None:
6565
self.custom_typeshed_dir = None # type: Optional[str]
6666
self.mypy_path = [] # type: List[str]
6767
self.report_dirs = {} # type: Dict[str, str]
68+
# Show errors in PEP 561 packages/site-packages modules
69+
self.no_silence_site_packages = False
6870
self.ignore_missing_imports = False
6971
self.follow_imports = 'normal' # normal|silent|skip|error
7072
# Whether to respect the follow_imports setting even for stub files.

mypy/util.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
"""Utility functions with no non-trivial dependencies."""
2-
2+
import genericpath # type: ignore # no stub files yet
3+
import os
4+
import pathlib
35
import re
46
import subprocess
7+
import sys
58
from xml.sax.saxutils import escape
6-
from typing import TypeVar, List, Tuple, Optional, Dict
9+
from typing import TypeVar, List, Tuple, Optional, Dict, Sequence
710

811

912
T = TypeVar('T')
@@ -204,3 +207,8 @@ def replace_object_state(new: object, old: object) -> None:
204207
setattr(new, attr, getattr(old, attr))
205208
elif hasattr(new, attr):
206209
delattr(new, attr)
210+
211+
212+
def is_sub_path(path1: str, path2: str) -> bool:
213+
"""Given two paths, return if path1 is a sub-path of path2."""
214+
return pathlib.Path(path2) in pathlib.Path(path1).parents

test-data/packages/typedpkg/typedpkg/sample.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
def ex(a):
55
# type: (Iterable[str]) -> Tuple[str, ...]
66
"""Example typed package."""
7-
return tuple(a)
7+
return list(a)

test-data/unit/fine-grained-modules.test

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -751,15 +751,16 @@ main:1: note: (Perhaps setting MYPYPATH or using the "--ignore-missing-imports"
751751
main:2: error: Incompatible types in assignment (expression has type "int", variable has type "str")
752752

753753
[case testAddFileWhichImportsLibModuleWithErrors]
754+
# flags: --no-silence-site-packages
754755
import a
755756
a.x = 0
756757
[file a.py.2]
757758
import broken
758759
x = broken.x
759760
z
760761
[out]
761-
main:1: error: Cannot find module named 'a'
762-
main:1: note: (Perhaps setting MYPYPATH or using the "--ignore-missing-imports" flag would help)
762+
main:2: error: Cannot find module named 'a'
763+
main:2: note: (Perhaps setting MYPYPATH or using the "--ignore-missing-imports" flag would help)
763764
==
764765
a.py:3: error: Name 'z' is not defined
765766
<ROOT>/test-data/unit/lib-stub/broken.pyi:2: error: Name 'y' is not defined

0 commit comments

Comments
 (0)