Skip to content

Silence modules in site-packages and typeshed #5303

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 7 commits into from
Jul 6, 2018
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
8 changes: 6 additions & 2 deletions mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
from mypy.checker import TypeChecker
from mypy.indirection import TypeIndirectionVisitor
from mypy.errors import Errors, CompileError, report_internal_error
from mypy.util import DecodeError, decode_python_encoding
from mypy.util import DecodeError, decode_python_encoding, is_sub_path
from mypy.report import Reports
from mypy import moduleinfo
from mypy.fixup import fixup_module
Expand Down Expand Up @@ -2368,7 +2368,11 @@ def find_module_and_diagnose(manager: BuildManager,
skipping_module(manager, caller_line, caller_state,
id, path)
raise ModuleNotFound

if not manager.options.no_silence_site_packages:
for dir in manager.search_paths.package_path + manager.search_paths.typeshed_path:
if is_sub_path(path, dir):
# Silence errors in site-package dirs and typeshed
follow_imports = 'silent'
return (path, follow_imports)
else:
# Could not find a module. Typically the reason is a
Expand Down
3 changes: 3 additions & 0 deletions mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,9 @@ def add_invertible_flag(flag: str,
'--no-site-packages', action='store_true',
dest='special-opts:no_executable',
help="do not search for installed PEP 561 compliant packages")
imports_group.add_argument(
'--no-silence-site-packages', action='store_true',
help="Do not silence errors in PEP 561 compliant installed packages")

platform_group = parser.add_argument_group(
title='platform configuration',
Expand Down
2 changes: 2 additions & 0 deletions mypy/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ def __init__(self) -> None:
self.custom_typeshed_dir = None # type: Optional[str]
self.mypy_path = [] # type: List[str]
self.report_dirs = {} # type: Dict[str, str]
# Show errors in PEP 561 packages/site-packages modules
self.no_silence_site_packages = False
self.ignore_missing_imports = False
self.follow_imports = 'normal' # normal|silent|skip|error
# Whether to respect the follow_imports setting even for stub files.
Expand Down
12 changes: 10 additions & 2 deletions mypy/util.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
"""Utility functions with no non-trivial dependencies."""

import genericpath # type: ignore # no stub files yet
import os
import pathlib
import re
import subprocess
import sys
from xml.sax.saxutils import escape
from typing import TypeVar, List, Tuple, Optional, Dict
from typing import TypeVar, List, Tuple, Optional, Dict, Sequence


T = TypeVar('T')
Expand Down Expand Up @@ -204,3 +207,8 @@ def replace_object_state(new: object, old: object) -> None:
setattr(new, attr, getattr(old, attr))
elif hasattr(new, attr):
delattr(new, attr)


def is_sub_path(path1: str, path2: str) -> bool:
"""Given two paths, return if path1 is a sub-path of path2."""
return pathlib.Path(path2) in pathlib.Path(path1).parents
2 changes: 1 addition & 1 deletion test-data/packages/typedpkg/typedpkg/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
def ex(a):
# type: (Iterable[str]) -> Tuple[str, ...]
"""Example typed package."""
return tuple(a)
return list(a)
5 changes: 3 additions & 2 deletions test-data/unit/fine-grained-modules.test
Original file line number Diff line number Diff line change
Expand Up @@ -751,15 +751,16 @@ main:1: note: (Perhaps setting MYPYPATH or using the "--ignore-missing-imports"
main:2: error: Incompatible types in assignment (expression has type "int", variable has type "str")

[case testAddFileWhichImportsLibModuleWithErrors]
# flags: --no-silence-site-packages
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary here? Is broken installed as a PEP 561 package?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since opening this, I also implemented silencing typeshed, which broken is simulated as being part of via alt_lib_path.

import a
a.x = 0
[file a.py.2]
import broken
x = broken.x
z
[out]
main:1: error: Cannot find module named 'a'
main:1: note: (Perhaps setting MYPYPATH or using the "--ignore-missing-imports" flag would help)
main:2: error: Cannot find module named 'a'
main:2: note: (Perhaps setting MYPYPATH or using the "--ignore-missing-imports" flag would help)
==
a.py:3: error: Name 'z' is not defined
<ROOT>/test-data/unit/lib-stub/broken.pyi:2: error: Name 'y' is not defined
Expand Down