flake8 lints all files with --diff, instead of only *.py files, or whatever matches --filename
I installed flake8 via pip in virtualenv:
virtualenv ~/flaketest
source ~/flaketest/bin/activate
pip install flake8
flake8 --bug-report
ouput:
{
"dependencies": [
{
"dependency": "setuptools",
"version": "36.0.1"
}
],
"platform": {
"python_implementation": "CPython",
"python_version": "2.7.13",
"system": "Linux"
},
"plugins": [
{
"plugin": "mccabe",
"version": "0.6.1"
},
{
"plugin": "pycodestyle",
"version": "2.3.1"
},
{
"plugin": "pyflakes",
"version": "1.5.0"
}
],
"version": "3.4.1"
}
When using the flake8 to check only changes in a diff using --diff
option, all the files in the diff are checked, instead of only selecting the relevant files (aka *.py
, or whatever matches the --filename
option).
This can be easily checked by running:
# prepare data
mkdir empydir datadir
echo "=" > datadir/documents.rst
echo "=" > datadir/somecode.py
# try different things
diff -uN emptydir datadir | flake8 --diff
diff -uN emptydir datadir | flake8 --diff --filename='*.py'
Both flake8 runs return:
datadir/documents.rst:1:1: E999 SyntaxError: invalid syntax
datadir/somecode.py:1:1: E999 SyntaxError: invalid syntax
instead of only checking the somecode.py
file.
From what I found out, this is caused by _checker.py:_def should_create_file_checker(filename, argument)
treating all the files passed to _checker.py:_def make_checkers(self, paths=None)
from _main/application.py:_def run_checks(self, files=None)
as explicitly provided:
explicitly_provided = (not running_from_vcs and
(argument == filename))
return ((file_exists and
(explicitly_provided or matches_filename_patterns)) or
is_stdin)
Since all the filenames from the diff are passed as list of files, the filename
and argument
argument passed to _checker.py:_def should_create_file_checker(filename, argument)
are the same, setting the explicitly_provided
variable to True
, and thus ignoring the matches_filename_patterns
value.
This could be easily solved by changing the explicitly_provided
check to:
explicitly_provided = (not running_from_vcs and
not self.options.diff and
(argument == filename))
With this change, the --filename
option (in tox.ini or on command line) is honoured, and the default flake8 run only checks the Python file.
I could provide a pull request if you want me to, but this seems rather straight-forward.
Thanks!