diff options
| -rw-r--r-- | src/mailman/testing/flake8.py | 62 |
1 files changed, 31 insertions, 31 deletions
diff --git a/src/mailman/testing/flake8.py b/src/mailman/testing/flake8.py index 18193d1e1..68bd37761 100644 --- a/src/mailman/testing/flake8.py +++ b/src/mailman/testing/flake8.py @@ -31,6 +31,24 @@ class ImportType(Enum): ImportRecord = namedtuple('ImportRecord', 'itype lineno colno, module, names') +NONFROM_FOLLOWS_FROM = 'B401 Non-from import follows from-import' +NONFROM_MULTIPLE_NAMES = 'B402 Multiple names on non-from import' +NONFROM_SHORTER_FOLLOWS = 'B403 Shorter non-from import follows longer' +NONFROM_ALPHA_UNSORTED = ( + 'B404 Same-length non-from imports not sorted alphabetically') +NONFROM_EXTRA_BLANK_LINE = ( + 'B405 Unexpected blank line since last non-from import') +NONFROM_DOTTED_UNSORTED = ( + 'B406 Dotted non-from import not sorted alphabetically') + +FROMIMPORT_MISSING_BLANK_LINE = ( + 'B411 Expected one blank line since last non-from import') +FROMIMPORT_ALPHA_UNSORTED = 'B412 from-import not sorted alphabetically' +FROMIMPORT_MULTIPLE = 'B413 Multiple from-imports of same module' +FROMIMPORT_NAMES_UNSORTED = ( + 'B414 from-imported names are not sorted alphabetically') + + class ImportVisitor(NodeVisitor): def __init__(self): self.imports = [] @@ -55,14 +73,15 @@ class ImportVisitor(NodeVisitor): class ImportOrder: - name = 'mm-import-order' + name = 'flufl-import-order' version = '0.1' def __init__(self, tree, filename): self.tree = tree self.filename = filename - def _error(self, record, code, text): + def _error(self, record, error): + code, space, text = error.partition(' ') return (record.lineno, record.colno, '{} {}'.format(code, text), ImportOrder) @@ -76,11 +95,9 @@ class ImportOrder: continue if record.itype is ImportType.non_from: if len(record.names) != 1: - yield self._error(record, 'B402', - 'Multiple names on non-from import') + yield self._error(record, NONFROM_MULTIPLE_NAMES) if last_import.itype is ImportType.from_import: - yield self._error(record, 'B401', - 'Non-from import follows from-import') + yield self._error(record, NONFROM_FOLLOWS_FROM) # Shorter imports should always precede longer import *except* # when they are dotted imports and everything but the last # path component are the same. In that case, they should be @@ -92,50 +109,33 @@ class ImportOrder: this_parts = this_name.split('.') if (last_parts[:-1] == this_parts[:-1] and last_parts[-1] > this_parts[-1]): - yield self._error( - record, 'B410', - 'Dotted non-from import not sorted ' - 'alphabetically') + yield self._error(record, NONFROM_DOTTED_UNSORTED) elif len(last_name) > len(this_name): - yield self._error( - record, 'B403', - 'Shorter non-from import follows longer') + yield self._error(record, NONFROM_SHORTER_FOLLOWS) # It's also possible that the imports are the same length, in # which case they must be sorted alphabetically. if (len(last_import.names[0]) == len(record.names[0]) and last_import.names[0] > record.names[0]): - yield self._error( - record, 'B404', - 'Non-from imports not alphabetically sorted') + yield self._error(record, NONFROM_ALPHA_UNSORTED) if last_import.lineno + 1 != record.lineno: - yield self._error( - record, 'B405', - 'Unexpected blank line since last non-from import') + yield self._error(record, NONFROM_DOTTED_UNSORTED) else: assert record.itype is ImportType.from_import if (last_import.itype is ImportType.non_from and record.lineno != last_import.lineno + 2): - yield self._error( - record, 'B406', - 'Expected one blank line since last non-from import') + yield self._error(record, FROMIMPORT_MISSING_BLANK_LINE) if last_import.itype is ImportType.non_from: last_import = record continue if last_import.module > record.module: - yield self._error( - record, 'B407', - 'From-imports not sorted alphabetically') + yield self._error(record, FROMIMPORT_ALPHA_UNSORTED) # All imports from the same module should show up in the same # multiline import. if last_import.module == record.module: - yield self._error( - record, 'B408', - 'Importing from same module on different lines') + yield self._error(record, FROMIMPORT_MULTIPLE) # Check the sort order of the imported names. if sorted(record.names) != record.names: - yield self._error( - record, 'B409', - 'Imported names are not sorted alphabetically') + yield self._error(record, FROMIMPORT_NAMES_UNSORTED) # How to check for no blank lines between from imports? # Update the last import. last_import = record |
