mirror of
https://github.com/PyCQA/flake8.git
synced 2026-06-30 01:00:45 +00:00
Lint explicit files even when excluded
This commit is contained in:
parent
ee03327c82
commit
a6645fa8e6
4 changed files with 37 additions and 17 deletions
|
|
@ -10,7 +10,7 @@ import logging
|
||||||
import os.path
|
import os.path
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from flake8.discover_files import expand_paths
|
from flake8 import utils
|
||||||
from flake8.formatting import base as formatter
|
from flake8.formatting import base as formatter
|
||||||
from flake8.main import application as app
|
from flake8.main import application as app
|
||||||
from flake8.options.parse_args import parse_args
|
from flake8.options.parse_args import parse_args
|
||||||
|
|
@ -129,15 +129,12 @@ class StyleGuide:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def excluded(path: str) -> bool:
|
def excluded(path: str) -> bool:
|
||||||
paths = tuple(
|
return utils.matches_filename(
|
||||||
expand_paths(
|
path,
|
||||||
paths=[path],
|
patterns=self.options.exclude,
|
||||||
stdin_display_name=self.options.stdin_display_name,
|
log_message='"%(path)s" has %(whether)sbeen excluded',
|
||||||
filename_patterns=self.options.filename,
|
logger=LOG,
|
||||||
exclude=self.options.exclude,
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
return not paths
|
|
||||||
|
|
||||||
return excluded(filename) or (
|
return excluded(filename) or (
|
||||||
parent is not None and excluded(os.path.join(parent, filename))
|
parent is not None and excluded(os.path.join(parent, filename))
|
||||||
|
|
|
||||||
|
|
@ -22,17 +22,19 @@ def _filenames_from(
|
||||||
:param arg:
|
:param arg:
|
||||||
Parameter from the command-line.
|
Parameter from the command-line.
|
||||||
:param predicate:
|
:param predicate:
|
||||||
Predicate to use to filter out filenames. If the predicate
|
Predicate to use to filter out paths discovered from directories
|
||||||
returns ``True`` we will exclude the filename, otherwise we
|
or stdin aliases. Direct file arguments are yielded even if the
|
||||||
will yield it. By default, we include every filename
|
predicate matches them.
|
||||||
generated.
|
|
||||||
:returns:
|
:returns:
|
||||||
Generator of paths
|
Generator of paths
|
||||||
"""
|
"""
|
||||||
if predicate(arg):
|
if arg == "-" and predicate(arg):
|
||||||
return
|
return
|
||||||
|
|
||||||
if os.path.isdir(arg):
|
if os.path.isdir(arg):
|
||||||
|
if predicate(arg):
|
||||||
|
return
|
||||||
|
|
||||||
for root, sub_directories, files in os.walk(arg):
|
for root, sub_directories, files in os.walk(arg):
|
||||||
# NOTE(sigmavirus24): os.walk() will skip a directory if you
|
# NOTE(sigmavirus24): os.walk() will skip a directory if you
|
||||||
# remove it from the list of sub-directories.
|
# remove it from the list of sub-directories.
|
||||||
|
|
|
||||||
|
|
@ -135,6 +135,20 @@ def test_extend_exclude(tmpdir, capsys):
|
||||||
assert err == ""
|
assert err == ""
|
||||||
|
|
||||||
|
|
||||||
|
def test_explicit_file_ignores_exclude(tmpdir, capsys):
|
||||||
|
tmpdir.join("test/module.py").ensure()
|
||||||
|
tmpdir.join("test/module.py").write("import os\n")
|
||||||
|
tmpdir.join(".flake8").write("[flake8]\nexclude = module.py\n")
|
||||||
|
|
||||||
|
with tmpdir.as_cwd():
|
||||||
|
assert cli.main(["test/module.py"]) == 1
|
||||||
|
|
||||||
|
expected_out = "test/module.py:1:1: F401 'os' imported but unused\n"
|
||||||
|
out, err = capsys.readouterr()
|
||||||
|
assert out == expected_out.replace("/", os.sep)
|
||||||
|
assert err == ""
|
||||||
|
|
||||||
|
|
||||||
def test_malformed_per_file_ignores_error(tmpdir, capsys):
|
def test_malformed_per_file_ignores_error(tmpdir, capsys):
|
||||||
"""Test the error message for malformed `per-file-ignores`."""
|
"""Test the error message for malformed `per-file-ignores`."""
|
||||||
setup_cfg = """\
|
setup_cfg = """\
|
||||||
|
|
|
||||||
|
|
@ -95,11 +95,11 @@ def test_filenames_from_exclude_doesnt_exclude_directory_names(tmpdir):
|
||||||
assert filenames == [os.path.join(".", "2", "1", "return_me.py")]
|
assert filenames == [os.path.join(".", "2", "1", "return_me.py")]
|
||||||
|
|
||||||
|
|
||||||
def test_filenames_from_predicate_applies_to_initial_arg(tmp_path):
|
def test_filenames_from_predicate_does_not_apply_to_initial_file(tmp_path):
|
||||||
"""Test that the predicate is also applied to the passed argument."""
|
"""Test that the predicate is not applied to a directly passed file."""
|
||||||
fname = str(tmp_path.joinpath("f.py"))
|
fname = str(tmp_path.joinpath("f.py"))
|
||||||
ret = tuple(_filenames_from(fname, predicate=lambda _: True))
|
ret = tuple(_filenames_from(fname, predicate=lambda _: True))
|
||||||
assert ret == ()
|
assert ret == (fname,)
|
||||||
|
|
||||||
|
|
||||||
def test_filenames_from_predicate_applies_to_dirname(tmp_path):
|
def test_filenames_from_predicate_applies_to_dirname(tmp_path):
|
||||||
|
|
@ -164,3 +164,10 @@ def test_alternate_stdin_name_is_filtered():
|
||||||
def test_filename_included_even_if_not_matching_include(tmp_path):
|
def test_filename_included_even_if_not_matching_include(tmp_path):
|
||||||
some_file = str(tmp_path.joinpath("some/file"))
|
some_file = str(tmp_path.joinpath("some/file"))
|
||||||
assert _expand_paths(paths=(some_file,)) == {some_file}
|
assert _expand_paths(paths=(some_file,)) == {some_file}
|
||||||
|
|
||||||
|
|
||||||
|
def test_filename_included_even_if_matching_exclude(tmp_path):
|
||||||
|
some_file = str(tmp_path.joinpath("some/file.py"))
|
||||||
|
assert _expand_paths(paths=(some_file,), exclude=("file.py",)) == {
|
||||||
|
some_file,
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue