Skip to content

Commit 9f745e5

Browse files
authored
Dev/fix command being over aggressive (#185)
* make filter actually work * log when we fail to read a file, what file it was * make a failing test * make it pass * print the exception in the test * switch to pathspec for globbing/matching in fix * move pathspec to core tooling section * format / linter issues * simplify
1 parent f2aeb64 commit 9f745e5

File tree

5 files changed

+29
-11
lines changed

5 files changed

+29
-11
lines changed

ni_python_styleguide/_acknowledge_existing_errors/__init__.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,11 @@ def _handle_emergent_violations(exclude, app_import_names, extend_ignore, file_o
134134

135135
def remove_auto_suppressions_from_file(file: pathlib.Path):
136136
"""Removes auto-suppressions from file."""
137-
lines = file.read_text(encoding=_utils.DEFAULT_ENCODING).splitlines()
137+
try:
138+
lines = file.read_text(encoding=_utils.DEFAULT_ENCODING).splitlines()
139+
except Exception:
140+
_module_logger.warning("Failed to read %s with encoding %s", file, _utils.DEFAULT_ENCODING)
141+
raise
138142
stripped_lines = [_filter_suppresion_from_line(line) for line in lines]
139143
file.write_text("\n".join(stripped_lines) + "\n", encoding=_utils.DEFAULT_ENCODING)
140144

ni_python_styleguide/_fix.py

+18-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import logging
22
import pathlib
33
from collections import defaultdict
4-
from fnmatch import fnmatch
54
from typing import Iterable
65

76
import isort
7+
import pathspec
88

9-
from ni_python_styleguide import _acknowledge_existing_errors
10-
from ni_python_styleguide import _config_constants
11-
from ni_python_styleguide import _format
12-
from ni_python_styleguide import _utils
9+
from ni_python_styleguide import (
10+
_acknowledge_existing_errors,
11+
_config_constants,
12+
_format,
13+
_utils,
14+
)
1315

1416
_module_logger = logging.getLogger(__name__)
1517

@@ -93,17 +95,24 @@ def fix(
9395
):
9496
"""Fix basic linter errors and format."""
9597
file_or_dir = file_or_dir or ["."]
98+
extend_ignore = extend_ignore or ""
99+
exclude = exclude or ""
96100
if aggressive:
101+
glob_spec = pathspec.PathSpec.from_lines(
102+
"gitwildmatch",
103+
["*.py"]
104+
+ [f"!{exclude_}" for exclude_ in exclude.split(",") if exclude_]
105+
+ [f"!{ignore_}" for ignore_ in extend_ignore.split(",") if ignore_],
106+
)
97107
all_files = []
98108
for file_or_dir_ in file_or_dir:
99109
file_path = pathlib.Path(file_or_dir_)
100110
if file_path.is_dir():
101-
all_files.extend(file_path.rglob("*.py"))
111+
all_files.extend(
112+
[pathlib.Path(o) for o in glob_spec.match_tree(str(file_path), negate=False)]
113+
)
102114
else:
103115
all_files.append(file_path)
104-
all_files = filter(
105-
lambda o: not any([fnmatch(o, exclude_) for exclude_ in exclude.split(",")]), all_files
106-
)
107116
for file in all_files:
108117
if not file.is_file(): # doesn't really exist...
109118
continue

poetry.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ include = ["ni_python_styleguide/config.toml"]
1414

1515
[tool.poetry.dependencies]
1616
python = "^3.7"
17+
pathspec = ">=0.11.1"
1718

1819
# Tools we aggregate
1920
flake8 = [

tests/test_cli/test_fix.py

+4
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,13 @@ def test_given_bad_input__fix__produces_expected_output_aggressive(
5151
in_file = test_dir / "input.py"
5252
test_file = tmp_path / "input.py"
5353
shutil.copyfile(in_file, test_file)
54+
test_loading_file = tmp_path / ".venv/file_with_non_unicode_chars.py"
55+
test_loading_file.parent.mkdir(parents=True, exist_ok=True)
56+
test_loading_file.write_text("Förward to victory!", encoding="ISO 8859-1")
5457

5558
output = styleguide_command(command="fix", command_args=["--aggressive"])
5659

60+
assert not output.exception, f"Error in running:\n{output.exception}"
5761
assert output.exit_code in (True, 0), f"Error in running:\n{output}"
5862
result = test_file.read_text(encoding="UTF-8")
5963
snapshot.snapshot_dir = test_dir

0 commit comments

Comments
 (0)