Skip to content
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

Silence modules in site-packages and typeshed #5303

Merged
merged 7 commits into from Jul 6, 2018
@@ -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, commonpath
from mypy.report import Reports
from mypy import moduleinfo
from mypy.fixup import fixup_module
@@ -2368,7 +2368,12 @@ 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:
if os.path.isabs(path):
for dir in manager.search_paths.package_path + manager.search_paths.typeshed_path:
if commonpath([dir, 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
@@ -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',
@@ -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.
@@ -1,9 +1,12 @@
"""Utility functions with no non-trivial dependencies."""

import genericpath # type: ignore # no stub files yet
import os
from os.path import splitdrive
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')
@@ -204,3 +207,91 @@ def replace_object_state(new: object, old: object) -> None:
setattr(new, attr, getattr(old, attr))
elif hasattr(new, attr):
delattr(new, attr)


# backport commonpath for 3.4
if sys.platform == 'win32':
def commonpath(paths: Sequence[str]) -> str:

This comment has been minimized.

Copy link
@ilevkivskyi

ilevkivskyi Jul 5, 2018

Collaborator

Do we need the general algorithm here? As I understand we essentially only need something like is_sub_path(p1, p2). Not that I think it is important, but maybe it will allow to simplify code?

This comment has been minimized.

Copy link
@ethanhs

ethanhs Jul 5, 2018

Author Collaborator

I was originally hoping to eventually use os.path.commonpath once we drop support for 3.4, but I think you are right it would be better to do this ourselves. I will change this when I have time later.

"""Given a sequence of path names, returns the longest common sub-path."""

if not paths:
raise ValueError('commonpath() arg is an empty sequence')

if isinstance(paths[0], bytes):
sep = b'\\'
altsep = b'/'
curdir = b'.'
else:
sep = '\\'
altsep = '/'
curdir = '.'

try:
drivesplits = [splitdrive(p.replace(altsep, sep).lower()) for p in paths]
split_paths = [p.split(sep) for d, p in drivesplits]

try:
isabs, = set(p[:1] == sep for d, p in drivesplits)
except ValueError:
raise ValueError("Can't mix absolute and relative paths") from None

# Check that all drive letters or UNC paths match. The check is made only
# now otherwise type errors for mixing strings and bytes would not be
# caught.
if len(set(d for d, p in drivesplits)) != 1:
raise ValueError("Paths don't have the same drive")

drive, path = splitdrive(paths[0].replace(altsep, sep))
common = path.split(sep)
common = [c for c in common if c and c != curdir]

split_paths = [[c for c in s if c and c != curdir] for s in split_paths]
s1 = min(split_paths)
s2 = max(split_paths)
for i, c in enumerate(s1):
if c != s2[i]:
common = common[:i]
break
else:
common = common[:len(s1)]

prefix = drive + sep if isabs else drive
return prefix + sep.join(common)
except (TypeError, AttributeError):
genericpath._check_arg_types('commonpath', *paths)
raise
else:
def commonpath(paths: Sequence[str]) -> str:
"""Given a sequence of path names, returns the longest common sub-path."""
if not paths:
raise ValueError('commonpath() arg is an empty sequence')

if isinstance(paths[0], bytes):
sep = b'/'
curdir = b'.'
else:
sep = '/'
curdir = '.'

try:
split_paths = [path.split(sep) for path in paths]

try:
isabs, = set(p[:1] == sep for p in paths)
except ValueError:
raise ValueError("Can't mix absolute and relative paths") from None

split_paths = [[c for c in s if c and c != curdir] for s in split_paths]
s1 = min(split_paths)
s2 = max(split_paths)
common = s1
for i, c in enumerate(s1):
if c != s2[i]:
common = s1[:i]
break

prefix = sep if isabs else sep[:0]
return prefix + sep.join(common)
except (TypeError, AttributeError):
genericpath._check_arg_types('commonpath', *paths)
raise
@@ -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

This comment has been minimized.

Copy link
@ilevkivskyi

ilevkivskyi Jul 5, 2018

Collaborator

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

This comment has been minimized.

Copy link
@ethanhs

ethanhs Jul 5, 2018

Author Collaborator

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
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.