Skip to content

Commit 2051dbc

Browse files
committed
✅ work around tool/format_ignores.py breaking its own tests
1 parent c5f0f04 commit 2051dbc

File tree

2 files changed

+34
-27
lines changed

2 files changed

+34
-27
lines changed

lefthook.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ pre-commit:
1313
glob: "*.{py,pyi}"
1414
run: uv run --no-sync tool/format_ignores.py {staged_files}
1515
stage_fixed: true
16+
exclude:
17+
- "tool/test/test_format_ignores.py"
1618
docs-check:
1719
glob:
1820
- "docs/**"

tool/test/test_format_ignores.py

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@
1313
main,
1414
)
1515

16+
_TEST_SOURCE_TEMPLATE = """
17+
def func1() -> None:
18+
# {}
19+
pass
20+
21+
def func2() -> None:
22+
# {}
23+
pass
24+
""".lstrip()
25+
1626

1727
@pytest.mark.parametrize(
1828
("input_text", "expected_output"),
@@ -34,15 +44,16 @@ def test_sort_ignore_list(input_text: str, expected_output: str) -> None:
3444

3545
def test_process_file(tmpdir: pytest.TempdirFactory) -> None:
3646
"""Test _process_file function."""
37-
test_file = Path(str(tmpdir)) / "test.pyi"
38-
test_content = """def func1() -> None:
39-
# type: ignore[attr-defined, no-any-return]
40-
pass
47+
test_content = _TEST_SOURCE_TEMPLATE.format(
48+
"type: ignore[attr-defined, no-any-return]",
49+
"pyright: ignore[reportUnknownMemberType,reportUnknownVariableType]",
50+
)
51+
expected_content = _TEST_SOURCE_TEMPLATE.format(
52+
"type: ignore[attr-defined, no-any-return]",
53+
"pyright: ignore[reportUnknownMemberType, reportUnknownVariableType]",
54+
)
4155

42-
def func2() -> None:
43-
# pyright: ignore[reportUnknownMemberType, reportUnknownVariableType]
44-
pass
45-
"""
56+
test_file = Path(str(tmpdir)) / "test.pyi"
4657
test_file.write_text(test_content, encoding="utf-8")
4758

4859
# Check-only mode
@@ -53,15 +64,6 @@ def func2() -> None:
5364
# Processing mode
5465
modified = _process_file(test_file)
5566
assert modified is True
56-
57-
expected_content = """def func1() -> None:
58-
# type: ignore[attr-defined, no-any-return]
59-
pass
60-
61-
def func2() -> None:
62-
# pyright: ignore[reportUnknownMemberType, reportUnknownVariableType]
63-
pass
64-
"""
6567
assert test_file.read_text(encoding="utf-8") == expected_content
6668

6769
# Re-processing
@@ -94,15 +96,15 @@ def test_process_directory(tmpdir: pytest.TempdirFactory) -> None:
9496
encoding="utf-8",
9597
)
9698
test_files[2].write_text(
97-
"# pyright: ignore[reportUnknownMemberType, reportUnknownVariableType]",
99+
"# " + "pyright: ignore[reportUnknownVariableType,reportUnknownMemberType]",
98100
encoding="utf-8",
99101
)
100102
test_files[3].write_text(
101-
"# type: ignore[attr-defined, no-any-return]",
103+
"# type: ignore[no-any-return, attr-defined]",
102104
encoding="utf-8",
103105
)
104106
test_files[4].write_text(
105-
"# type: ignore[attr-defined, no-any-return]",
107+
"# type: ignore[no-any-return, attr-defined]",
106108
encoding="utf-8",
107109
)
108110

@@ -133,15 +135,15 @@ def test_process_directory(tmpdir: pytest.TempdirFactory) -> None:
133135
)
134136
assert (
135137
test_files[2].read_text(encoding="utf-8")
136-
== "# pyright: ignore[reportUnknownMemberType, reportUnknownVariableType]"
138+
== "# " + "pyright: ignore[reportUnknownMemberType, reportUnknownVariableType]"
137139
)
138140
assert (
139141
test_files[3].read_text(encoding="utf-8")
140-
== "# type: ignore[attr-defined, no-any-return]"
142+
== "# type: ignore[no-any-return, attr-defined]"
141143
)
142144
assert (
143145
test_files[4].read_text(encoding="utf-8")
144-
== "# type: ignore[attr-defined, no-any-return]"
146+
== "# type: ignore[no-any-return, attr-defined]"
145147
)
146148

147149

@@ -152,7 +154,7 @@ def test_main_success(
152154
"""Test main function with valid path."""
153155
test_file = Path(str(tmpdir)) / "test.pyi"
154156
test_file.write_text(
155-
"# type: ignore[attr-defined, no-any-return]",
157+
"# type: ignore[no-any-return, attr-defined]",
156158
encoding="utf-8",
157159
)
158160

@@ -174,7 +176,7 @@ def test_main_check_mode(
174176
"""Test main function in check-only mode."""
175177
test_file = Path(str(tmpdir)) / "test.pyi"
176178
test_file.write_text(
177-
"# type: ignore[attr-defined, no-any-return]",
179+
"# type: ignore[no-any-return, attr-defined]",
178180
encoding="utf-8",
179181
)
180182

@@ -185,7 +187,7 @@ def test_main_check_mode(
185187

186188
assert (
187189
test_file.read_text(encoding="utf-8")
188-
== "# type: ignore[attr-defined, no-any-return]"
190+
== "# type: ignore[no-any-return, attr-defined]"
189191
)
190192

191193

@@ -205,7 +207,10 @@ def test_main_custom_pattern(
205207
"""Test main function with custom pattern."""
206208
temp_dir = Path(str(tmpdir))
207209
test_py = temp_dir / "test.py"
208-
test_py.write_text("# type: ignore[attr-defined, no-any-return]", encoding="utf-8")
210+
test_py.write_text(
211+
"# type: ignore[attr-defined, no-any-return]",
212+
encoding="utf-8",
213+
)
209214

210215
monkeypatch.setattr(
211216
sys,

0 commit comments

Comments
 (0)