8000 Add --exclude by hauntsaninja · Pull Request #9992 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Add --exclude #9992

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 22 commits into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
don't override defaults
  • Loading branch information
hauntsaninja committed Feb 3, 2021
commit 6254323191dffdb548e4b2ab9659f6b4549f20ea
21 changes: 9 additions & 12 deletions mypy/find_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

import functools
import os
import re
import sys

from typing import List, Sequence, Set, Tuple, Optional
from typing_extensions import Final

from mypy.modulefinder import BuildSource, PYTHON_EXTENSIONS, mypy_path
from mypy.modulefinder import BuildSource, PYTHON_EXTENSIONS, mypy_path, matches_exclude
from mypy.fscache import FileSystemCache
from mypy.options import Options

Expand Down Expand Up @@ -94,7 +92,7 @@ def __init__(self, fscache: FileSystemCache, options: Options) -> None:
self.explicit_package_bases = get_explicit_package_bases(options)
self.namespace_packages = options.namespace_packages
self.exclude = options.exclude
self.verbose = options.verbosity >= 2
self.verbosity = options.verbosity

def is_explicit_package_base(self, path: str) -> bool:
assert self.explicit_package_bases
Expand All @@ -106,16 +104,15 @@ def find_sources_in_dir(self, path: str) -> List[BuildSource]:
seen = set() # type: Set[str]
names = sorted(self.fscache.listdir(path), key=keyfunc)
for name in names:
# Skip certain names altogether
if name in ("__pycache__", "site-packages") or name.startswith("."):
continue
subpath = os.path.join(path, name)

if self.exclude:
s 8000 ubpath_str = os.path.abspath(subpath).replace(os.sep, "/")
if self.fscache.isdir(subpath):
subpath_str += "/"
if re.search(self.exclude, subpath_str):
if self.verbose:
print("TRACE: Excluding {}".format(subpath_str), file=sys.stderr)
continue
if matches_exclude(
subpath, self.exclude, self.fscache, self.verbosity >= 2
):
continue

if self.fscache.isdir(subpath):
sub_sources = self.find_sources_in_dir(subpath)
Expand Down
9 changes: 5 additions & 4 deletions mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,6 @@ def add_invertible_flag(flag: str,
# Options object. Options that require further processing should have
# their `dest` prefixed with `special-opts:`, which will cause them to be
# parsed into the separate special_opts namespace object.
options = Options()

# Note: we have a style guide for formatting the mypy --help text. See
# https://github.com/python/mypy/wiki/Documentation-Conventions
Expand Down Expand Up @@ -812,10 +811,10 @@ def add_invertible_flag(flag: str,
code_group.add_argument(
"--exclude",
metavar="PATH",
default=options.exclude,
default="",
help=(
"Regex to match file names, directory names or paths to avoid checking. "
"Defaults to '%(default)s'."
"Regular expression to match file names, directory names or paths which mypy should "
"ignore while recursively discovering files to check."
)
)
code_group.add_argument(
Expand Down Expand Up @@ -846,6 +845,8 @@ def add_invertible_flag(flag: str,
if config_file and not os.path.exists(config_file):
parser.error("Cannot find config file '%s'" % config_file)

options = Options()

def set_strict_flags() -> None:
for dest, value in strict_flag_assignments:
setattr(options, dest, value)
Expand Down
35 changes: 20 additions & 15 deletions mypy/modulefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,22 +444,14 @@ def find_modules_recursive(self, module: str) -> List[BuildSource]:
names = sorted(self.fscache.listdir(package_path))
for name in names:
# Skip certain names altogether
if (
name in ("__pycache__", "site-packages", "node_modules")
or name.startswith(".")
or name.endswith("~")
):
if name in ("__pycache__", "site-packages") or name.startswith("."):
continue
subpath = os.path.join(package_path, name)

if self.options and self.options.exclude:
subpath_str = os.path.abspath(subpath).replace(os.sep, "/")
if self.fscache.isdir(subpath):
subpath_str += "/"
if re.search(self.options.exclude, subpath_str):
if self.options.verbosity >= 2:
print("TRACE: Excluding {}".format(subpath_str), file=sys.stderr)
continue
if self.options and matches_exclude(
subpath, self.options.exclude, self.fscache, self.options.verbosity >= 2
):
continue

if self.fscache.isdir(subpath):
# Only recurse into packages
Expand All @@ -474,13 +466,26 @@ def find_modules_recursive(self, module: str) -> List[BuildSource]:
if stem == '__init__':
continue
if stem not in seen and '.' not in stem and suffix in PYTHON_EXTENSIONS:
# (If we sorted names) we could probably just make the BuildSource ourselves,
# but this ensures compatibility with find_module / the cache
# (If we sorted names by keyfunc) we could probably just make the BuildSource
# ourselves, but this ensures compatibility with find_module / the cache
seen.add(stem)
sources.extend(self.find_modules_recursive(module + '.' + stem))
return sources


def matches_exclude(subpath: str, exclude: str, fscache: FileSystemCache, verbose: bool) -> bool:
if not exclude:
return False
subpath_str = os.path.abspath(subpath).replace(os.sep, "/")
if fscache.isdir(subpath):
subpath_str += "/"
if re.search(exclude, subpath_str):
if verbose >= 2:
print("TRACE: Excluding {}".format(subpath_str), file=sys.stderr)
return True
return False


def verify_module(fscache: FileSystemCache, id: str, path: str, prefix: str) -> bool:
"""Check that all packages containing id have a __init__ file."""
if path.endswith(('__init__.py', '__init__.pyi')):
Expand Down
4 changes: 1 addition & 3 deletions mypy/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ def __init__(self) -> None:
# top-level __init__.py to your packages.
self.explicit_package_bases = False
# File names, directory names or subpaths to avoid checking
self.exclude = (
"/(__pycache__|site-packages|node_modules)/|/(\\.[^/]+|[^/]+~)/?$"
) # type: str
self.exclude = "" # type: str

# disallow_any options
self.disallow_any_generics = False
Expand Down
2 changes: 1 addition & 1 deletion mypy_self_check.ini
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ pretty = True
always_false = MYPYC
plugins = misc/proper_plugin.py
python_version = 3.5
exclude = /(mypy/typeshed|__pycache__|site-packages)/|/(\.[^/]+)/?$
exclude = /mypy/typeshed/
0