mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-06 13:06:53 +00:00
Only force files to be included when run from CLI
Previously we added support so users can do:
$ flake8 bin/executable
But this broke the fact that git hooks shouldn't check things like
reStructuredText doc files. This commit restores that functionality but
will cause bin/executable to be ignored in the git hook. This seems fair
since folks can amend their filename patterns to include it explicitly.
Closes #268
This commit is contained in:
parent
f8fbc11fad
commit
e4582ef4e7
5 changed files with 34 additions and 15 deletions
|
|
@ -3,7 +3,11 @@
|
||||||
|
|
||||||
You can view the `3.3.0 milestone`_ on GitLab for more details.
|
You can view the `3.3.0 milestone`_ on GitLab for more details.
|
||||||
|
|
||||||
|
- Fix problem where hooks should only check \*.py files. (See also
|
||||||
|
`GitLab#268`_)
|
||||||
|
|
||||||
.. links
|
.. links
|
||||||
.. _3.3.0 milestone:
|
.. _3.3.0 milestone:
|
||||||
https://gitlab.com/pycqa/flake8/milestones/16
|
https://gitlab.com/pycqa/flake8/milestones/16
|
||||||
|
.. _GitLab#268:
|
||||||
|
https://gitlab.com/pycqa/flake8/issues/268
|
||||||
|
|
|
||||||
|
|
@ -257,24 +257,17 @@ class Manager(object):
|
||||||
paths = ['.']
|
paths = ['.']
|
||||||
|
|
||||||
filename_patterns = self.options.filename
|
filename_patterns = self.options.filename
|
||||||
|
running_from_vcs = self.options._running_from_vcs
|
||||||
|
|
||||||
# NOTE(sigmavirus24): Yes this is a little unsightly, but it's our
|
# NOTE(sigmavirus24): Yes this is a little unsightly, but it's our
|
||||||
# best solution right now.
|
# best solution right now.
|
||||||
def should_create_file_checker(filename):
|
def should_create_file_checker(filename, argument):
|
||||||
"""Determine if we should create a file checker."""
|
"""Determine if we should create a file checker."""
|
||||||
matches_filename_patterns = utils.fnmatch(
|
matches_filename_patterns = utils.fnmatch(
|
||||||
filename, filename_patterns
|
filename, filename_patterns
|
||||||
)
|
)
|
||||||
is_stdin = filename == '-'
|
is_stdin = filename == '-'
|
||||||
file_exists = os.path.exists(filename)
|
file_exists = os.path.exists(filename)
|
||||||
return (file_exists and matches_filename_patterns) or is_stdin
|
|
||||||
|
|
||||||
checks = self.checks.to_dictionary()
|
|
||||||
self.checkers = [
|
|
||||||
FileChecker(filename, checks, self.options)
|
|
||||||
for argument in paths
|
|
||||||
for filename in utils.filenames_from(argument,
|
|
||||||
self.is_path_excluded)
|
|
||||||
# NOTE(sigmavirus24): If a user explicitly specifies something,
|
# NOTE(sigmavirus24): If a user explicitly specifies something,
|
||||||
# e.g, ``flake8 bin/script`` then we should run Flake8 against
|
# e.g, ``flake8 bin/script`` then we should run Flake8 against
|
||||||
# that. Since should_create_file_checker looks to see if the
|
# that. Since should_create_file_checker looks to see if the
|
||||||
|
|
@ -282,7 +275,19 @@ class Manager(object):
|
||||||
# the event that the argument and the filename are identical.
|
# the event that the argument and the filename are identical.
|
||||||
# If it was specified explicitly, the user intended for it to be
|
# If it was specified explicitly, the user intended for it to be
|
||||||
# checked.
|
# checked.
|
||||||
if argument == filename or should_create_file_checker(filename)
|
explicitly_provided = (not running_from_vcs and
|
||||||
|
(argument == filename))
|
||||||
|
return ((file_exists and
|
||||||
|
(explicitly_provided or matches_filename_patterns)) or
|
||||||
|
is_stdin)
|
||||||
|
|
||||||
|
checks = self.checks.to_dictionary()
|
||||||
|
self.checkers = [
|
||||||
|
FileChecker(filename, checks, self.options)
|
||||||
|
for argument in paths
|
||||||
|
for filename in utils.filenames_from(argument,
|
||||||
|
self.is_path_excluded)
|
||||||
|
if should_create_file_checker(filename, argument)
|
||||||
]
|
]
|
||||||
LOG.info('Checking %d files', len(self.checkers))
|
LOG.info('Checking %d files', len(self.checkers))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -174,6 +174,8 @@ class Application(object):
|
||||||
if not self.parsed_diff:
|
if not self.parsed_diff:
|
||||||
self.exit()
|
self.exit()
|
||||||
|
|
||||||
|
self.options._running_from_vcs = False
|
||||||
|
|
||||||
self.check_plugins.provide_options(self.option_manager, self.options,
|
self.check_plugins.provide_options(self.option_manager, self.options,
|
||||||
self.args)
|
self.args)
|
||||||
self.listening_plugins.provide_options(self.option_manager,
|
self.listening_plugins.provide_options(self.option_manager,
|
||||||
|
|
@ -300,16 +302,20 @@ class Application(object):
|
||||||
self.make_guide()
|
self.make_guide()
|
||||||
self.make_file_checker_manager()
|
self.make_file_checker_manager()
|
||||||
|
|
||||||
def _run(self, argv):
|
def report(self):
|
||||||
# type: (Union[NoneType, List[str]]) -> NoneType
|
"""Report errors, statistics, and benchmarks."""
|
||||||
self.initialize(argv)
|
|
||||||
self.run_checks()
|
|
||||||
self.formatter.start()
|
self.formatter.start()
|
||||||
self.report_errors()
|
self.report_errors()
|
||||||
self.report_statistics()
|
self.report_statistics()
|
||||||
self.report_benchmarks()
|
self.report_benchmarks()
|
||||||
self.formatter.stop()
|
self.formatter.stop()
|
||||||
|
|
||||||
|
def _run(self, argv):
|
||||||
|
# type: (Union[NoneType, List[str]]) -> NoneType
|
||||||
|
self.initialize(argv)
|
||||||
|
self.run_checks()
|
||||||
|
self.report()
|
||||||
|
|
||||||
def run(self, argv=None):
|
def run(self, argv=None):
|
||||||
# type: (Union[NoneType, List[str]]) -> NoneType
|
# type: (Union[NoneType, List[str]]) -> NoneType
|
||||||
"""Run our application.
|
"""Run our application.
|
||||||
|
|
|
||||||
|
|
@ -43,6 +43,7 @@ def hook(lazy=False, strict=False):
|
||||||
filepaths = list(copy_indexed_files_to(tempdir, lazy))
|
filepaths = list(copy_indexed_files_to(tempdir, lazy))
|
||||||
app.initialize(['.'])
|
app.initialize(['.'])
|
||||||
app.options.exclude = update_excludes(app.options.exclude, tempdir)
|
app.options.exclude = update_excludes(app.options.exclude, tempdir)
|
||||||
|
app.options._running_from_vcs = True
|
||||||
app.run_checks(filepaths)
|
app.run_checks(filepaths)
|
||||||
|
|
||||||
app.report_errors()
|
app.report_errors()
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,10 @@ def hook(ui, repo, **kwargs):
|
||||||
filenames = list(get_filenames_from(repo, kwargs))
|
filenames = list(get_filenames_from(repo, kwargs))
|
||||||
|
|
||||||
app = application.Application()
|
app = application.Application()
|
||||||
app.run(filenames)
|
app.initialize(filenames)
|
||||||
|
app.options._running_from_vcs = True
|
||||||
|
app.run_checks()
|
||||||
|
app.report()
|
||||||
|
|
||||||
if strict:
|
if strict:
|
||||||
return app.result_count
|
return app.result_count
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue