def _fix_flake_import_errors(self) -> None: """Fix python source code to correct missed or unused import statements.""" error_messages = autoflake.check(self._join_code()) for message in error_messages: if isinstance(message, (UndefinedName, UndefinedExport)): object_name = message.message_args[0] self._add_package(object_name) elif isinstance(message, UnusedImport): import_name = message.message_args[0] self._remove_unused_imports(import_name)
def pyflakes_count(filename): """Return pyflakes error count.""" with autoflake.open_with_encoding( filename, encoding=autoflake.detect_encoding(filename)) as f: return len(list(autoflake.check(f.read())))
def test_check_with_unicode(self): self.assertFalse(autoflake.check('print("∑")')) self.assertTrue(autoflake.check('import os # ∑'))
def test_check_with_bad_syntax(self): self.assertFalse(autoflake.check('foo('))
def test_check(self): self.assertTrue(autoflake.check('import os'))
def test_unused_import_line_numbers_with_dot(self): self.assertEqual([1], list( autoflake.unused_import_line_numbers( autoflake.check('import os.path\n'))))
def test_unused_import_line_numbers_with_from(self): self.assertEqual([1], list( autoflake.unused_import_line_numbers( autoflake.check('from os import path\n'))))
def test_unused_import_line_numbers_with_dot(self): self.assertEqual( [1], list(autoflake.unused_import_line_numbers( autoflake.check('import os.path\n'))))
def test_unused_import_line_numbers_with_from(self): self.assertEqual( [1], list(autoflake.unused_import_line_numbers( autoflake.check('from os import path\n'))))
def test_check_with_unicode(self): self.assertFalse(autoflake.check('print("∑")'))