def _register_pyflakes_check(): """Register the pyFlakes checker into PEP8 set of checks.""" from flake8_import_order.flake8_linter import Linter from flake8_blind_except import check_blind_except # Resolving conflicts between pep8 and pyflakes. codes = { "UnusedImport": "F401", "ImportShadowedByLoopVar": "F402", "ImportStarUsed": "F403", "LateFutureImport": "F404", "Redefined": "F801", "RedefinedInListComp": "F812", "UndefinedName": "F821", "UndefinedExport": "F822", "UndefinedLocal": "F823", "DuplicateArgument": "F831", "UnusedVariable": "F841", } for name, obj in vars(pyflakes.messages).items(): if name[0].isupper() and obj.message: obj.tpl = "{0} {1}".format(codes.get(name, "F999"), obj.message) pep8.register_check(_PyFlakesChecker, codes=['F']) # FIXME parser hack parser = pep8.get_parser('', '') Linter.add_options(parser) options, args = parser.parse_args([]) Linter.parse_options(options) # end of hack pep8.register_check(Linter, codes=['I']) pep8.register_check(check_blind_except, codes=['B90'])
def test_I101_google_style(): base_path = os.path.dirname(__file__) test_case_path = os.path.join(base_path, "test_cases") fullpath = os.path.join(test_case_path, "bad_order_google.py") data = open(fullpath).read() tree = ast.parse(data, fullpath) argv = [ "--application-import-names=flake8_import_order,tests", "--import-order-style=google", ] parser = pep8.get_parser('', '') Linter.add_options(parser) options, args = parser.parse_args(argv) Linter.parse_options(options) checker = Linter(tree, fullpath) codes = [] messages = [] for lineno, col_offset, msg, instance in checker.run(): code, message = msg.split(" ", 1) codes.append(code) messages.append(message) assert codes == ["I101"] assert messages == [ "Imported names are in the wrong order. Should be A, c, D" ]
def test_expected_error(tree, filename, expected): parser = pep8.get_parser('', '') Linter.add_options(parser) options, args = parser.parse_args( ['--import-order-style=google'] if 'google' in filename else []) Linter.parse_options(options) checker = Linter(tree, filename) errors = [] for lineno, col_offset, msg, instance in checker.run(): errors.append(msg.split()[0]) assert errors == expected
def _get_linter(argv, code, filename): parser = pycodestyle.get_parser('', '') Linter.add_options(parser) options, args = parser.parse_args(argv) Linter.parse_options(options) return Linter( ast.parse( code, filename ), str(filename) )
def test_linter(): argv = ['--application-import-names=flake8_import_order'] parser = pycodestyle.get_parser('', '') Linter.add_options(parser) options, args = parser.parse_args(argv) Linter.parse_options(options) data = 'import ast\nimport flake8_import_order\n' pycodestyle.stdin_get_value = lambda: data tree = ast.parse(data) checker = Linter(tree, None) for lineno, col_offset, msg, instance in checker.run(): assert msg == 'I201 Missing newline before sections or imports.'
def test_linter(): argv = ['--application-import-names=flake8_import_order'] parser = pycodestyle.get_parser('', '') Linter.add_options(parser) options, args = parser.parse_args(argv) Linter.parse_options(options) data = 'import ast\nimport flake8_import_order\n' pycodestyle.stdin_get_value = lambda: data tree = ast.parse(data) checker = Linter(tree, None) for lineno, col_offset, msg, instance in checker.run(): assert msg.startswith( 'I201 Missing newline between import groups.', )
def test_parsing(): style = 'google' import_names = ['flake8_import_order', 'tests'] package_names = ['local_package'] argv = [ "--application-import-names={}".format(','.join(import_names)), "--import-order-style={}".format(style), "--application-package-names={}".format(','.join(package_names)), ] parser = pycodestyle.get_parser('', '') Linter.add_options(parser) options, args = parser.parse_args(argv) Linter.parse_options(options) assert Linter.options['import_order_style'] == style assert Linter.options['application_import_names'] == import_names assert Linter.options['application_package_names'] == package_names
def test_parsing(): style = 'google' import_names = ['flake8_import_order', 'tests'] package_names = ['local_package'] argv = [ "--application-import-names={}".format(','.join(import_names)), "--import-order-style={}".format(style), "--application-package-names={}".format(','.join(package_names)), ] parser = pycodestyle.get_parser('', '') Linter.add_options(parser) options, args = parser.parse_args(argv) Linter.parse_options(options) assert Linter.options['import_order_style'].name == style assert Linter.options['import_order_style'].load() is Google assert Linter.options['application_import_names'] == import_names assert Linter.options['application_package_names'] == package_names
def test_expected_error(tree, filename, expected_codes, expected_messages): argv = ["--application-import-names=flake8_import_order,tests"] if 'google' in filename: argv.append('--import-order-style=google') parser = pep8.get_parser('', '') Linter.add_options(parser) options, args = parser.parse_args(argv) Linter.parse_options(options) checker = Linter(tree, filename) codes = [] messages = [] for lineno, col_offset, msg, instance in checker.run(): code, message = msg.split(" ", 1) codes.append(code) messages.append(message) assert codes == expected_codes assert set(messages) >= set(expected_messages)
def test_expected_error(tree, filename, expected_codes, expected_messages): argv = [ "--application-import-names=flake8_import_order,tests" ] if 'google' in filename: argv.append('--import-order-style=google') parser = pep8.get_parser('', '') Linter.add_options(parser) options, args = parser.parse_args(argv) Linter.parse_options(options) checker = Linter(tree, filename) codes = [] messages = [] for lineno, col_offset, msg, instance in checker.run(): code, message = msg.split(" ", 1) codes.append(code) messages.append(message) assert codes == expected_codes assert set(messages) >= set(expected_messages)
def _get_linter(argv, code, filename): parser = pycodestyle.get_parser('', '') Linter.add_options(parser) options, args = parser.parse_args(argv) Linter.parse_options(options) return Linter(ast.parse(code, filename), str(filename))