From e09a22a416b61f242feb90d8a536286713d0ab7e Mon Sep 17 00:00:00 2001 From: Ian Stapleton Cordasco Date: Fri, 26 Oct 2018 15:05:06 -0500 Subject: [PATCH] Refactor to support the per-file-ignores Most of the problems with our logic were due to not having the same logic as our exclude parameter. This refactors that out into a separate function so we can confidently achieve that. --- src/flake8/checker.py | 19 +++++-------------- src/flake8/style_guide.py | 12 ++++++------ src/flake8/utils.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 20 deletions(-) diff --git a/src/flake8/checker.py b/src/flake8/checker.py index e8a71d4..3b97414 100644 --- a/src/flake8/checker.py +++ b/src/flake8/checker.py @@ -2,7 +2,6 @@ import collections import errno import logging -import os import signal import sys import tokenize @@ -188,20 +187,12 @@ class Manager(object): return False path = self.options.stdin_display_name - exclude = self.options.exclude - if not exclude: - return False - basename = os.path.basename(path) - if utils.fnmatch(basename, exclude): - LOG.debug('"%s" has been excluded', basename) - return True - - absolute_path = os.path.abspath(path) - match = utils.fnmatch(absolute_path, exclude) - LOG.debug( - '"%s" has %sbeen excluded', absolute_path, "" if match else "not " + return utils.matches_filename( + path, + patterns=self.options.exclude, + log_message='"%(path)s" has %(whether)sbeen excluded', + logger=LOG, ) - return match def make_checkers(self, paths=None): # type: (List[str]) -> NoneType diff --git a/src/flake8/style_guide.py b/src/flake8/style_guide.py index 42576e9..46f72fc 100644 --- a/src/flake8/style_guide.py +++ b/src/flake8/style_guide.py @@ -459,7 +459,7 @@ class StyleGuide(object): def copy(self, filename=None, extend_ignore_with=None, **kwargs): """Create a copy of this style guide with different values.""" filename = filename or self.filename - options = copy.copy(self.options) + options = copy.deepcopy(self.options) options.ignore.extend(extend_ignore_with or []) return StyleGuide( options, self.listener, self.formatter, filename=filename @@ -485,11 +485,11 @@ class StyleGuide(object): """ if self.filename is None: return True - normalized_filename = utils.normalize_path(filename) - return ( - normalized_filename == self.filename - or utils.fnmatch(filename, self.filename) - or utils.fnmatch(normalized_filename, self.filename) + return utils.matches_filename( + filename, + patterns=[self.filename], + log_message='{!r} does %(whether)smatch "%(path)s"'.format(self), + logger=LOG, ) def should_report_error(self, code): diff --git a/src/flake8/utils.py b/src/flake8/utils.py index e77998d..68627af 100644 --- a/src/flake8/utils.py +++ b/src/flake8/utils.py @@ -336,6 +336,38 @@ def parameters_for(plugin): return parameters +def matches_filename(path, patterns, log_message, logger): + """Use fnmatch to discern if a path exists in patterns. + + :param str path: + The path to the file under question + :param patterns: + The patterns to match the path against. + :type patterns: + list[str] + :param str log_message: + The message used for logging purposes. + :returns: + True if path matches patterns, False otherwise + :rtype: + bool + """ + if not patterns: + return False + basename = os.path.basename(path) + if fnmatch(basename, patterns): + logger.debug(log_message, {"path": basename, "whether": ""}) + return True + + absolute_path = os.path.abspath(path) + match = fnmatch(absolute_path, patterns) + logger.debug( + log_message, + {"path": absolute_path, "whether": "" if match else "not "}, + ) + return match + + def get_python_version(): """Find and format the python implementation and version.