Skip to content

Commit 21e908d

Browse files
authored
Merge pull request #6 from bagerard/fix_scanning_outside_flake8_filepaths
fix_scanning_outside_flake8_filepaths
2 parents 21b35ff + 33aa9b9 commit 21e908d

File tree

4 files changed

+32
-54
lines changed

4 files changed

+32
-54
lines changed

.github/workflows/github-actions.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
strategy:
3232
fail-fast: false
3333
matrix:
34-
python-version: [3.7, 3.8, 3.9, '3.10', 'pypy3.9']
34+
python-version: [3.8, 3.9, '3.10', 'pypy3.9']
3535
steps:
3636
- uses: actions/checkout@v3
3737
- name: Set up Python ${{ matrix.python-version }}

.gitlab-ci.yml

Lines changed: 0 additions & 36 deletions
This file was deleted.

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ This plugin uses `IFI` as error code (but it will never raise any error)
3131

3232
## Changes
3333

34-
[v0.2.3]
34+
[v0.3.0]
3535
* Fix issue with non-utf8 first line in the files (UnicodeDecodeError) #3
36+
* Limit the files it scans to the files processed by the flake8 invocation #5
3637

3738
[v0.2.x]
3839
* Fix and pypi doc improvement

flake8_in_file_ignores/flake8_plugin.py

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
import glob
21
from typing import List
32

43
# metadata
5-
__version__ = "0.2.2"
4+
__version__ = "0.3.0"
5+
6+
from flake8.discover_files import expand_paths
7+
68
CODE_PREFIX = "IFI" # stands for "In File Ignores"
79

810

911
IFI_TAG = "# flake8-in-file-ignores:"
1012
IFI_FULL_TAG = "# flake8-in-file-ignores: noqa:"
1113

1214

13-
def get_cwd_py_files() -> List[str]:
14-
return glob.glob("**/*.py", recursive=True)
15-
16-
1715
def read_first_line(filepath: str) -> str:
1816
with open(filepath, errors="ignore") as f:
1917
first_line = f.readline().rstrip()
@@ -25,15 +23,15 @@ def build_pfi_str(filepath: str, error_codes_csv: str):
2523
return f"{filepath}:{error_codes_csv}"
2624

2725

28-
def get_cwd_pfi_noqa() -> List[str]:
29-
pfi_noqa_item = []
30-
for file_path in get_cwd_py_files():
26+
def get_ifi_noqa(filepaths: List[str]) -> List[str]:
27+
ifi_noqa_item = []
28+
for file_path in filepaths:
3129
# print(f"Checking {file_path}")
3230
first_line = read_first_line(file_path)
3331
if IFI_TAG in first_line:
3432
error_codes = parse_ifi_error_codes(first_line)
35-
pfi_noqa_item.append(build_pfi_str(file_path, error_codes))
36-
return pfi_noqa_item
33+
ifi_noqa_item.append(build_pfi_str(file_path, error_codes))
34+
return ifi_noqa_item
3735

3836

3937
def parse_ifi_error_codes(line: str) -> str:
@@ -61,17 +59,32 @@ def __init__(self, tree, filename: str):
6159
@classmethod
6260
def parse_options(cls, option_manager, options, args):
6361
# print(option_manager, options, args)
64-
pfi_noqa_strs = get_cwd_pfi_noqa()
65-
if pfi_noqa_strs:
66-
prev_pfi = options.per_file_ignores
67-
concat_pfi = " ".join(pfi_noqa_strs)
62+
excludes = (*options.exclude, *options.extend_exclude)
63+
filepaths = cls.get_files(options, excludes)
64+
# print(f"{filepaths=}")
65+
ifi_noqa_strs = get_ifi_noqa(filepaths)
66+
if ifi_noqa_strs:
67+
# original_pfi = options.per_file_ignores
68+
concat_pfi = " ".join(ifi_noqa_strs)
6869
options.per_file_ignores += " " + concat_pfi
6970
# print(
70-
# f"flake8-in-file-ignores - Patching options.per_file_ignores from `{prev_pfi}` to `{options.per_file_ignores}`"
71+
# f"flake8-in-file-ignores - Patching options.per_file_ignores from `{original_pfi}` to `{options.per_file_ignores}`"
7172
# )
7273
else:
7374
# print(f"flake8-in-file-ignores - No match found")
7475
pass
7576

77+
@staticmethod
78+
def get_files(options, excludes):
79+
# inspired from flake8 codebase
80+
return tuple(
81+
expand_paths(
82+
paths=options.filenames,
83+
stdin_display_name=options.stdin_display_name,
84+
filename_patterns=options.filename,
85+
exclude=excludes,
86+
)
87+
)
88+
7689
def run(self):
7790
yield from []

0 commit comments

Comments
 (0)