Skip to content

Commit

Permalink
add baseline file
Browse files Browse the repository at this point in the history
  • Loading branch information
KotlinIsland committed Dec 13, 2021
1 parent 6d5bf84 commit 84cde47
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 17 deletions.
1 change: 1 addition & 0 deletions .mypy/baseline.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions .mypy/proper_plugin.json

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@
from mypy.config_parser import parse_mypy_comments
from mypy.freetree import free_tree
from mypy.stubinfo import legacy_bundled_packages, is_legacy_bundled_package
from mypy import errorcodes as codes

from mypy import errorcodes as codes, defaults

# Switch to True to produce debug output related to fine-grained incremental
# mode only that is useful during development. This produces only a subset of
Expand Down Expand Up @@ -3176,7 +3175,10 @@ def process_stale_scc(graph: Graph, scc: List[str], manager: BuildManager) -> No
graph[id].transitive_error = True
for id in stale:
if not manager.options.write_baseline:
manager.errors.load_baseline(Path(manager.options.baseline_file))
res = manager.errors.load_baseline(Path(manager.options.baseline_file))
if not res and manager.options.baseline_file != defaults.BASELINE_FILE:
print("Baseline file not found")
raise Exception("Baseline file not found")
if manager.errors.baseline:
manager.errors.filter_baseline()
manager.flush_errors(manager.errors.file_messages(graph[id].xpath), False)
Expand Down
38 changes: 26 additions & 12 deletions mypy/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,30 @@ def __init__(self,
Optional[ErrorCode]]


def filter_prefix(map: Dict[str, List[ErrorInfo]]) -> Dict[str, List[ErrorInfo]]:
result = {file.removeprefix(os.getcwd()): errors for file, errors in map.items()}
def filter_prefix(error_map: Dict[str, List[ErrorInfo]]) -> Dict[str, List[ErrorInfo]]:
"""Convert absolute paths to relative paths in an error_map"""
result = {
remove_path_prefix(file, os.getcwd()).replace(os.sep, "/"): errors
for file, errors in error_map.items()
}
for errors in result.values():
for error in errors:
error.origin = error.origin[0].removeprefix(os.getcwd()), *error.origin[1:]
error.file = error.file.removeprefix(os.getcwd())
error.origin = remove_path_prefix(
error.origin[0], os.getcwd()).replace(os.sep, "/"), *error.origin[1:]
error.file = remove_path_prefix(error.file, os.getcwd()).replace(os.sep, "/")
error.import_ctx = [
(
remove_path_prefix(import_ctx[0], os.getcwd()).replace(os.sep, "/"),
import_ctx[1],
) for import_ctx in error.import_ctx
]
return result


def baseline_json_hook(d: Dict[str, object]):
def baseline_json_hook(d: Dict[str, object]) -> object:
class_ = d.pop(".class", None)
if class_ is None: return d
if class_ is None:
return d
if class_ == "mypy.errors.ErrorInfo":
result = object.__new__(ErrorInfo)
elif class_ == "mypy.errorcodes.ErrorCode":
Expand Down Expand Up @@ -199,9 +211,9 @@ class Errors:
seen_import_error = False

# Error baseline
baseline: dict[str, list[ErrorInfo]] = {}
baseline: Dict[str, List[ErrorInfo]] = {}
# All detected errors before baseline filter
all_errors: dict[str, list[ErrorInfo]] = {}
all_errors: Dict[str, List[ErrorInfo]] = {}

def __init__(self,
show_error_context: bool = False,
Expand Down Expand Up @@ -804,21 +816,23 @@ def save_baseline(self, file: Path) -> None:
}
)

def load_baseline(self, file: Path) -> None:
def load_baseline(self, file: Path) -> bool:
"""Load baseline errors from baseline file"""

if not file.exists():
return
return False
self.baseline = json.load(file.open("r"), object_hook=baseline_json_hook)
return True

def filter_baseline(self) -> None:
"""Remove baseline errors from the error_info_map"""

self.all_errors = self.error_info_map.copy()
for file, errors in self.error_info_map.items():
baseline_errors = self.baseline.get(file.removeprefix(os.getcwd()))
baseline_errors = self.baseline.get(
remove_path_prefix(file, os.getcwd()).replace(os.sep, "/"))
if not baseline_errors:
return
continue
new_errors = []
for error in errors:
for baseline_error in baseline_errors:
Expand Down
2 changes: 1 addition & 1 deletion mypy/ipc.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,4 @@ def connection_name(self) -> str:
if sys.platform == 'win32':
return self.name
else:
return self.sock.getsockname()
return self.sock.getsockname() # type: ignore[no-any-return, unused-ignore]
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ description = type check ourselves
basepython = python3.7
commands =
python -m mypy --config-file mypy_self_check.ini -p mypy -p mypyc
python -m mypy --config-file mypy_self_check.ini misc/proper_plugin.py
python -m mypy --config-file mypy_self_check.ini --baseline-file .mypy/proper_plugin.json misc/proper_plugin.py

[testenv:docs]
description = invoke sphinx-build to build the HTML docs
Expand Down

0 comments on commit 84cde47

Please sign in to comment.