Skip to content

Feature/ignore errors by regex #8108

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

Closed
Closed
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
2 changes: 2 additions & 0 deletions docs/source/config_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,8 @@ no analog available via the command line options.
``ignore_errors`` (bool, default False)
Ignores all non-fatal errors.

``ignore_errors_by_regex`` (list of regex-pattern, with each pattern on a new line)
Ignores all errors, which match one of the given regex-pattern.

Miscellaneous strictness flags
******************************
Expand Down
3 changes: 2 additions & 1 deletion mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ def _build(sources: List[BuildSource],
options.show_error_codes,
options.pretty,
lambda path: read_py_file(path, cached_read, options.python_version),
options.show_absolute_path)
options.show_absolute_path,
options.ignore_errors_by_regex)
plugin, snapshot = load_plugins(options, errors, stdout, extra_plugins)

# Construct a build manager object to hold state during the build.
Expand Down
1 change: 1 addition & 0 deletions mypy/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def split_and_match_files(paths: str) -> List[str]:
'always_true': lambda s: [p.strip() for p in s.split(',')],
'always_false': lambda s: [p.strip() for p in s.split(',')],
'package_root': lambda s: [p.strip() for p in s.split(',')],
'ignore_errors_by_regex': lambda s: [p.strip() for p in s.split('\n') if p != ''],
'cache_dir': expand_path,
'python_executable': expand_path,
} # type: Final
Expand Down
31 changes: 28 additions & 3 deletions mypy/errors.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os.path
import sys
import traceback
import re
from collections import OrderedDict, defaultdict

from typing import Tuple, List, TypeVar, Set, Dict, Optional, TextIO, Callable
Expand Down Expand Up @@ -163,14 +164,20 @@ def __init__(self,
show_error_codes: bool = False,
pretty: bool = False,
read_source: Optional[Callable[[str], Optional[List[str]]]] = None,
show_absolute_path: bool = False) -> None:
show_absolute_path: bool = False,
ignore_errors_by_regex: Optional[List[str]] = None) -> None:
self.show_error_context = show_error_context
self.show_column_numbers = show_column_numbers
self.show_error_codes = show_error_codes
self.show_absolute_path = show_absolute_path
self.pretty = pretty
# We use fscache to read source code when showing snippets.
self.read_source = read_source
self.ignore_errors_by_regex = ignore_errors_by_regex
if ignore_errors_by_regex is not None:
self.ignore_message_regex_patterns = [re.compile(p) for p in ignore_errors_by_regex]
else:
self.ignore_message_regex_patterns = []
self.initialize()

def initialize(self) -> None:
Expand All @@ -194,7 +201,8 @@ def copy(self) -> 'Errors':
self.show_error_codes,
self.pretty,
self.read_source,
self.show_absolute_path)
self.show_absolute_path,
self.ignore_errors_by_regex)
new.file = self.file
new.import_ctx = self.import_ctx[:]
new.function_or_member = self.function_or_member[:]
Expand Down Expand Up @@ -349,8 +357,25 @@ def add_error_info(self, info: ErrorInfo) -> None:
self.only_once_messages.add(info.message)
self._add_error_info(file, info)

def is_ignore_message_by_regex(self, message: str) -> bool:
for p in self.ignore_message_regex_patterns:
if p.match(message):
return True

return False

def is_line_already_ignored(self, file: str, line: int) -> bool:
if file in self.used_ignored_lines:
if line in self.used_ignored_lines[file]:
return True
return False

def is_ignored_error(self, line: int, info: ErrorInfo, ignores: Dict[int, List[str]]) -> bool:
if line not in ignores:
if info.severity == 'note' and self.is_line_already_ignored(info.origin[0], line):
return True
elif self.is_ignore_message_by_regex(info.message):
return True
elif line not in ignores:
return False
elif not ignores[line]:
# Empty list means that we ignore all errors
Expand Down
8 changes: 5 additions & 3 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,13 @@ def parse(source: Union[str, bytes],
on failure. Otherwise, use the errors object to report parse errors.
"""
raise_on_error = False
if errors is None:
errors = Errors()
raise_on_error = True
if options is None:
options = Options()
if errors is None:
errors = Errors(
ignore_errors_by_regex=options.ignore_errors_by_regex
)
raise_on_error = True
errors.set_file(fnam, module)
is_stub_file = fnam.endswith('.pyi')
try:
Expand Down
8 changes: 5 additions & 3 deletions mypy/fastparse2.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,13 @@ def parse(source: Union[str, bytes],
on failure. Otherwise, use the errors object to report parse errors.
"""
raise_on_error = False
if errors is None:
errors = Errors()
raise_on_error = True
if options is None:
options = Options()
if errors is None:
errors = Errors(
ignore_errors_by_regex=options.ignore_errors_by_regex
)
raise_on_error = True
errors.set_file(fnam, module)
is_stub_file = fnam.endswith('.pyi')
try:
Expand Down
4 changes: 4 additions & 0 deletions mypy/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class BuildType:
"follow_imports",
"follow_imports_for_stubs",
"ignore_errors",
"ignore_errors_by_regex",
"ignore_missing_imports",
"local_partial_types",
"mypyc",
Expand Down Expand Up @@ -132,6 +133,9 @@ def __init__(self) -> None:
# Files in which to ignore all non-fatal errors
self.ignore_errors = False

# RegEx patterns of errors, which should be ignored
self.ignore_errors_by_regex = [] # type: List[str]

# Apply strict None checking
self.strict_optional = True

Expand Down
4 changes: 3 additions & 1 deletion mypy/stubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1321,7 +1321,9 @@ def parse_source_file(mod: StubSource, mypy_options: MypyOptions) -> None:
with open(mod.path, 'rb') as f:
data = f.read()
source = mypy.util.decode_python_encoding(data, mypy_options.python_version)
errors = Errors()
errors = Errors(
ignore_errors_by_regex=mypy_options.ignore_errors_by_regex
)
mod.ast = mypy.parse.parse(source, fnam=mod.path, module=mod.module,
errors=errors, options=mypy_options)
mod.ast._fullname = mod.module
Expand Down
1 change: 1 addition & 0 deletions mypy/test/testcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
'check-typeddict.test',
'check-type-aliases.test',
'check-ignore.test',
'check-ignore-by-regex.test',
'check-type-promotion.test',
'check-semanal-error.test',
'check-flags.test',
Expand Down
4 changes: 3 additions & 1 deletion mypy/test/testgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ def test_scc(self) -> None:
frozenset({'D'})})

def _make_manager(self) -> BuildManager:
errors = Errors()
options = Options()
errors = Errors(
ignore_errors_by_regex=options.ignore_errors_by_regex
)
fscache = FileSystemCache()
search_paths = SearchPaths((), (), (), ())
manager = BuildManager(
Expand Down
4 changes: 3 additions & 1 deletion mypyc/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ def generate_c(sources: List[BuildSource],
print("Parsed and typechecked in {:.3f}s".format(t1 - t0))

if not messages and result:
errors = Errors()
errors = Errors(
ignore_errors_by_regex=options.ignore_errors_by_regex
)
modules, ctext = emitmodule.compile_modules_to_c(
result, compiler_options=compiler_options, errors=errors, groups=groups)

Expand Down
11 changes: 8 additions & 3 deletions mypyc/errors.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
from typing import List
from typing import List, Optional

import mypy.errors


class Errors:
def __init__(self) -> None:
def __init__(
self,
ignore_errors_by_regex: Optional[List[str]] = None
) -> None:
self.num_errors = 0
self.num_warnings = 0
self._errors = mypy.errors.Errors()
self._errors = mypy.errors.Errors(
ignore_errors_by_regex=ignore_errors_by_regex
)

def error(self, msg: str, path: str, line: int) -> None:
self._errors.report(line, None, msg, severity='error', file=path)
Expand Down
4 changes: 3 additions & 1 deletion mypyc/test/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@ def run_case_step(self, testcase: DataDrivenTestCase, incremental_step: int) ->
compiler_options=compiler_options,
groups=groups,
alt_lib_path='.')
errors = Errors()
errors = Errors(
ignore_errors_by_regex=options.ignore_errors_by_regex
)
ir, cfiles = emitmodule.compile_modules_to_c(
result,
compiler_options=compiler_options,
Expand Down
4 changes: 3 additions & 1 deletion mypyc/test/testutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ def build_ir_for_single_file(input_lines: List[str],
if result.errors:
raise CompileError(result.errors)

errors = Errors()
errors = Errors(
ignore_errors_by_regex=options.ignore_errors_by_regex
)
modules = genops.build_ir(
[result.files['__main__']], result.graph, result.types,
genops.Mapper({'__main__': None}),
Expand Down
Loading