Skip to content

Commit fd74833

Browse files
Copilotcclauss
andauthored
Filter PEP 798 scan paths
Co-authored-by: cclauss <3709715+cclauss@users.noreply.github.com>
1 parent 9e3fbc1 commit fd74833

2 files changed

Lines changed: 35 additions & 6 deletions

File tree

‎scripts/find_and_fix_pep798.py‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
The rewritten source uses Python 3.15+ syntax, so fixed files need 3.15+.
2525
2626
Usage:
27-
scripts/find_and_fix_pep798.py [--fix] PATH [PATH ...]
27+
scripts/find_and_fix_pep798.py [--fix] [PATH ...]
28+
29+
If no path is provided, the current working directory is searched.
2830
2931
Without ``--fix`` the script only reports and exits with status 1 if any
3032
opportunities were found.
@@ -213,14 +215,26 @@ def fix_source(source: str) -> str:
213215
def iter_python_files(paths: Iterable[Path]) -> Iterator[Path]:
214216
for path in paths:
215217
if path.is_dir():
216-
yield from sorted(path.rglob("*.py"))
217-
else:
218+
yield from sorted(
219+
candidate
220+
for candidate in path.rglob("*.py")
221+
if not any(part.startswith(".") for part in candidate.parent.parts)
222+
)
223+
elif path.suffix == ".py" and not any(
224+
part.startswith(".") for part in path.parent.parts
225+
):
218226
yield path
219227

220228

221229
def main(argv: list[str] | None = None) -> int:
222230
parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0])
223-
parser.add_argument("paths", nargs="+", type=Path, help="files or directories")
231+
parser.add_argument(
232+
"paths",
233+
nargs="*",
234+
type=Path,
235+
default=[Path.cwd()],
236+
help="Python files or directories (defaults to the current directory)",
237+
)
224238
parser.add_argument("--fix", action="store_true", help="rewrite files in place")
225239
args = parser.parse_args(argv)
226240

‎tests/test_find_and_fix_pep798.py‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,16 @@ def test_multiline_comprehension() -> None:
9797

9898
def test_iter_python_files(tmp_path: Path) -> None:
9999
(tmp_path / "sub").mkdir()
100+
(tmp_path / ".hidden").mkdir()
100101
first = tmp_path / "a.py"
101102
second = tmp_path / "sub" / "b.py"
102103
other = tmp_path / "c.txt"
103-
for path in (first, second, other):
104+
hidden = tmp_path / ".hidden" / "hidden.py"
105+
for path in (first, second, other, hidden):
104106
path.write_text("")
105107
assert list(iter_python_files([tmp_path])) == [first, second]
106-
assert list(iter_python_files([other])) == [other]
108+
assert list(iter_python_files([other])) == []
109+
assert list(iter_python_files([hidden])) == []
107110

108111

109112
def test_main_reports_without_fixing(
@@ -137,6 +140,18 @@ def test_main_clean_file_returns_zero(
137140
assert "0 PEP 798 opportunities found" in capsys.readouterr().out
138141

139142

143+
def test_main_defaults_to_current_directory(
144+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
145+
) -> None:
146+
path = tmp_path / "mod.py"
147+
path.write_text("a = [y for x in xs for y in x]\n")
148+
monkeypatch.chdir(tmp_path)
149+
assert main([]) == 1
150+
output = capsys.readouterr().out
151+
assert f"{path}:1:" in output
152+
assert "1 PEP 798 opportunity found" in output
153+
154+
140155
def test_main_skips_syntax_errors(
141156
tmp_path: Path, capsys: pytest.CaptureFixture[str]
142157
) -> None:

0 commit comments

Comments
 (0)