Skip to content
Merged
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
3 changes: 3 additions & 0 deletions src/mutmut/mutation/file_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,9 @@ def filter_mutants_with_type_checker() -> dict[str, FailedTypeCheckMutant]:
wrapper.visit(visitor)
mutated_methods = visitor.found_mutants

if not mutated_methods:
continue

for error in errors_of_file:
assert error.file_path == visitor.file
mutant = next(
Expand Down
45 changes: 45 additions & 0 deletions tests/test_type_checking.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from pathlib import Path

import pytest
from inline_snapshot import snapshot

from mutmut.mutation.file_mutation import filter_mutants_with_type_checker
from mutmut.type_checking import TypeCheckingError
from mutmut.type_checking import parse_mypy_report
from mutmut.type_checking import parse_pyrefly_report
Expand Down Expand Up @@ -106,6 +108,49 @@ def test_pyrefly_parsing():
)


def test_filter_mutants_ignores_type_errors_in_files_without_mutants(tmp_path, monkeypatch):
project = tmp_path
copied_file = project / "mutants" / "src" / "consumer.py"
copied_file.parent.mkdir(parents=True)
copied_file.write_text("def use_kwargs(kwargs):\n return kwargs\n")
error = TypeCheckingError(
file_path=copied_file,
line_number=2,
error_description="Unpacked keyword argument object is not assignable",
)

monkeypatch.chdir(project)
monkeypatch.setattr("mutmut.mutation.file_mutation.run_type_checker", lambda command: [error])

assert filter_mutants_with_type_checker() == {}


def test_filter_mutants_still_raises_for_unowned_errors_in_mutated_files(tmp_path, monkeypatch):
project = tmp_path
mutated_file = project / "mutants" / "src" / "module.py"
mutated_file.parent.mkdir(parents=True)
mutated_file.write_text(
"from mutmut.mutation import _mutmut_mutated\n"
"\n"
"def helper(value):\n"
" return value\n"
"\n"
"def x_mutate_me__mutmut_1():\n"
" return 2\n"
)
error = TypeCheckingError(
file_path=mutated_file,
line_number=3,
error_description="helper type changed unexpectedly",
)

monkeypatch.chdir(project)
monkeypatch.setattr("mutmut.mutation.file_mutation.run_type_checker", lambda command: [error])

with pytest.raises(Exception, match="Could not find mutant for type error"):
filter_mutants_with_type_checker()


def _make_pahts_relative(errors: list[TypeCheckingError]):
cwd = Path(".").resolve()
for error in errors:
Expand Down
Loading