diff options
| author | Ian Cordasco <graffatcolmingov@gmail.com> | 2016-07-26 19:41:41 -0500 |
|---|---|---|
| committer | Ian Cordasco <graffatcolmingov@gmail.com> | 2016-07-26 19:45:05 -0500 |
| commit | e51fc5458b81b6c56c359c79b13ce88b70e476ec (patch) | |
| tree | fc869436089258d822661fa22e32a6c86b8d41e7 /src | |
| parent | 6eca38f2f2a15765aecef010f878aa17fdbe1db7 (diff) | |
| download | flake8-e51fc5458b81b6c56c359c79b13ce88b70e476ec.tar.gz | |
Fix handling of logical lines with noqa
When attempting to centralize all inline NoQA handling in the StyleGuide
we inadvertently broke plugins relying on it in combination with checker
state. For example, the check for E402 relies both on NoQA and the state
to determine if it has seen a non-import line. Placing NoQA on the sole
line that is not an import is more elegant than placing it on each of
the following import lines.
Closes #186
Diffstat (limited to 'src')
| -rw-r--r-- | src/flake8/defaults.py | 17 | ||||
| -rw-r--r-- | src/flake8/processor.py | 13 | ||||
| -rw-r--r-- | src/flake8/style_guide.py | 18 |
3 files changed, 26 insertions, 22 deletions
diff --git a/src/flake8/defaults.py b/src/flake8/defaults.py index ede2946..73cadf1 100644 --- a/src/flake8/defaults.py +++ b/src/flake8/defaults.py @@ -1,4 +1,5 @@ """Constants that define defaults.""" +import re EXCLUDE = '.svn,CVS,.bzr,.hg,.git,__pycache__,.tox,.eggs,*.egg' IGNORE = 'E121,E123,E126,E226,E24,E704,W503,W504' @@ -15,3 +16,19 @@ STATISTIC_NAMES = ( 'physical lines', 'tokens', ) + +NOQA_INLINE_REGEXP = re.compile( + # We're looking for items that look like this: + # ``# noqa`` + # ``# noqa: E123`` + # ``# noqa: E123,W451,F921`` + # ``# NoQA: E123,W451,F921`` + # ``# NOQA: E123,W451,F921`` + # We do not care about the ``: `` that follows ``noqa`` + # We do not care about the casing of ``noqa`` + # We want a comma-separated list of errors + '# noqa(?:: (?P<codes>[A-Z0-9,]+))?', + re.IGNORECASE +) + +NOQA_FILE = re.compile(r'\s*# flake8[:=]\s*noqa', re.I) diff --git a/src/flake8/processor.py b/src/flake8/processor.py index dee0b15..1d0e3a4 100644 --- a/src/flake8/processor.py +++ b/src/flake8/processor.py @@ -2,7 +2,6 @@ import contextlib import io import logging -import re import sys import tokenize @@ -47,8 +46,6 @@ class FileProcessor(object): - :attr:`verbose` """ - NOQA_FILE = re.compile(r'\s*# flake8[:=]\s*noqa', re.I) - def __init__(self, filename, options, lines=None): """Initialice our file processor. @@ -147,6 +144,7 @@ class FileProcessor(object): self.previous_logical = self.logical_line self.blank_lines = 0 self.tokens = [] + self.noqa = False def build_logical_line_tokens(self): """Build the mapping, comments, and logical line lists.""" @@ -189,9 +187,12 @@ class FileProcessor(object): def build_logical_line(self): """Build a logical line from the current tokens list.""" comments, logical, mapping_list = self.build_logical_line_tokens() + joined_comments = ''.join(comments) self.logical_line = ''.join(logical) + if defaults.NOQA_INLINE_REGEXP.search(joined_comments): + self.noqa = True self.statistics['logical lines'] += 1 - return ''.join(comments), self.logical_line, mapping_list + return joined_comments, self.logical_line, mapping_list def split_line(self, token): """Split a physical line's line based on new-lines. @@ -316,12 +317,12 @@ class FileProcessor(object): """Check if ``# flake8: noqa`` is in the file to be ignored. :returns: - True if a line matches :attr:`FileProcessor.NOQA_FILE`, + True if a line matches :attr:`defaults.NOQA_FILE`, otherwise False :rtype: bool """ - ignore_file = self.NOQA_FILE.search + ignore_file = defaults.NOQA_FILE.search return any(ignore_file(line) for line in self.lines) def strip_utf_bom(self): diff --git a/src/flake8/style_guide.py b/src/flake8/style_guide.py index d9a6285..0cbab8f 100644 --- a/src/flake8/style_guide.py +++ b/src/flake8/style_guide.py @@ -3,8 +3,8 @@ import collections import enum import linecache import logging -import re +from flake8 import defaults from flake8 import statistics from flake8 import utils @@ -53,20 +53,6 @@ Error = collections.namedtuple( class StyleGuide(object): """Manage a Flake8 user's style guide.""" - NOQA_INLINE_REGEXP = re.compile( - # We're looking for items that look like this: - # ``# noqa`` - # ``# noqa: E123`` - # ``# noqa: E123,W451,F921`` - # ``# NoQA: E123,W451,F921`` - # ``# NOQA: E123,W451,F921`` - # We do not care about the ``: `` that follows ``noqa`` - # We do not care about the casing of ``noqa`` - # We want a comma-separated list of errors - '# noqa(?:: (?P<codes>[A-Z0-9,]+))?', - re.IGNORECASE - ) - def __init__(self, options, listener_trie, formatter): """Initialize our StyleGuide. @@ -177,7 +163,7 @@ class StyleGuide(object): if physical_line is None: physical_line = linecache.getline(error.filename, error.line_number) - noqa_match = self.NOQA_INLINE_REGEXP.search(physical_line) + noqa_match = defaults.NOQA_INLINE_REGEXP.search(physical_line) if noqa_match is None: LOG.debug('%r is not inline ignored', error) return False |
