Skip to content

Commit

Permalink
Use pre-commit framework to execute mypy
Browse files Browse the repository at this point in the history
We fix some things the fromer bazlized mypy check missed.
  • Loading branch information
martis42 committed Jul 26, 2024
1 parent a83e326 commit 59743c3
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 13 deletions.
18 changes: 18 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,21 @@ repos:
# Linting
- id: ruff
args: [ --fix ]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.11.0
hooks:
# We overwrite the args as we configure mypy via our mypy.ini
# We require separate instances due to several files being named the same which mypy cannot handle
- id: mypy
name: "mypy"
args: [ ]
exclude: ^test/.*
- id: mypy
name: "mypy - test/aspect"
args: [ ]
files: ^test/aspect/.*\.py
- id: mypy
name: "mypy - test/apply_fixes"
args: [ ]
files: ^test/apply_fixes/.*\.py
6 changes: 3 additions & 3 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# https://mypy.readthedocs.io/en/stable/config_file.html
[mypy]
# Required to work with Bazel integration
# Without this mypy stumbles over us having several files with the same name in multiple directories, e.g. main
explicit_package_bases = true
# Preferred error reporting behavior
pretty = true
# Third party dependencies are not picked up by mypy
ignore_missing_imports = true
# Unreliable due to false positives
warn_return_any = false
# Additional checks beyond default settings
warn_redundant_casts = true
warn_unused_ignores = true
warn_no_return = true
warn_unreachable = true
# Enable relative imports in the test orchestration scripts
mypy_path = test/aspect/:test/apply_fixes/
5 changes: 4 additions & 1 deletion scripts/extract_std_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
python file containing the standard header list for DWYU to lookup.
"""

# No benefit for using logging here
# ruff: noqa: T201

from __future__ import annotations

import re
Expand All @@ -28,7 +31,7 @@ def extract_header(text: str) -> list[str]:
def main() -> None:
with INPUT.open(encoding="utf-8") as fin:
headers = extract_header(fin.read())
print("\n".join(f'"{h}",' for h in sorted(set(headers)))) # noqa: T201
print("\n".join(f'"{h}",' for h in sorted(set(headers))))


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion src/analyze_includes/parse_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from io import StringIO
from pathlib import Path

from pcpp.preprocessor import Action, OutputDirective, Preprocessor
from pcpp.preprocessor import Action, OutputDirective, Preprocessor # type: ignore[import-not-found]


class SimpleParsingPreprocessor(Preprocessor):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ def execute_test_logic(self) -> Result:
]
if all(msg in process.stderr for msg in expected_error):
return Success()
return self._make_unexpected_output_error(expected=expected_error, output=process.stderr)
return self._make_unexpected_output_error(expected="\n".join(expected_error), output=process.stderr)
4 changes: 3 additions & 1 deletion test/apply_fixes/test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ def _run_and_capture_cmd(self, cmd: list[str], **kwargs) -> subprocess.Completed
@staticmethod
def _make_unexpected_output_error(expected: str, output: str) -> Error:
border = 42 * "-"
return Error(f"Did not find expected output: {expected}\nUnexpected output:\n{border}\n{output}\n{border}\n")
return Error(
f"Did not find expected output: \n{border}\n{expected}\n{border}\nUnexpected output:\n{border}\n{output}\n{border}\n"
)

@staticmethod
def _make_unexpected_deps_error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ def execute_test_logic(self) -> Result:
self._run_automatic_fix(extra_args=["--fix-unused", f"--dwyu-log-file={log_file}"])

if len(deps := self._get_target_attribute(target="//:binary", attribute="deps")) > 0:
return self._make_unexpected_deps_error(expected_deps=[], actual_deps=deps)
return self._make_unexpected_deps_error(expected_deps=set(), actual_deps=deps)
if len(deps := self._get_target_attribute(target="//:another_binary", attribute="deps")) > 0:
return self._make_unexpected_deps_error(expected_deps=[], actual_deps=deps)
return self._make_unexpected_deps_error(expected_deps=set(), actual_deps=deps)
if len(deps := self._get_target_attribute(target="//sub/foo:foo", attribute="deps")) > 0:
return self._make_unexpected_deps_error(expected_deps=[], actual_deps=deps)
return self._make_unexpected_deps_error(expected_deps=set(), actual_deps=deps)
return Success()
9 changes: 6 additions & 3 deletions test/aspect/test_case_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import unittest
from pathlib import Path
from unittest.mock import MagicMock, Mock, patch
from unittest.mock import MagicMock, patch

from result import Error, Result, Success
from test_case import TestCaseBase
Expand All @@ -12,19 +12,22 @@
class TestCaseMock(TestCaseBase):
def __init__(self, name: str) -> None:
super().__init__(name)
self.result = Success()
self.result: Result = Success()
self.dwyu_extra_args: list[str] = []
self.target: list[str] | str = "//foo:bar"

def execute_test_logic(self) -> Result:
self._run_dwyu(target=self.target, aspect="//some:aspect", extra_args=self.dwyu_extra_args)
return self.result

@staticmethod
def _bazel_binary() -> str:
return "/bazel/binary"


class TestCaseTests(unittest.TestCase):
def setUp(self) -> None:
self.unit = TestCaseMock("foo")
self.unit._bazel_binary = Mock(return_value="/bazel/binary") # noqa: SLF001

@staticmethod
def get_cmd(mock: MagicMock) -> list[str]:
Expand Down

0 comments on commit 59743c3

Please sign in to comment.