def test_filter_code_should_ignore_docstring(self): line = """ def foo(): ''' >>> import math ''' """ self.assertEqual(line, ''.join(autoflake.filter_code(line)))
def test_filter_code_should_avoid_inline_except(self): line = """\ try: from zap import foo except: from zap import bar """ self.assertEqual( line, ''.join(autoflake.filter_code(line, remove_all_unused_imports=True)))
def test_filter_code_with_from(self): self.assertEqual( """\ pass x = 1 """, ''.join(autoflake.filter_code("""\ from os import path x = 1 """)))
def test_filter_code_with_ambiguous_from(self): self.assertEqual( """\ pass """, ''.join(autoflake.filter_code("""\ from frommer import abc, frommer, xyz """, remove_all_unused_imports=True)))
def test_filter_code(self): self.assertEqual( """\ import os pass os.foo() """, ''.join(autoflake.filter_code("""\ import os import re os.foo() """)))
def test_filter_code_with_used_from(self): self.assertEqual( """\ import frommer print(frommer) """, ''.join(autoflake.filter_code("""\ import frommer print(frommer) """, remove_all_unused_imports=True)))
def test_filter_code_with_remove_all_unused_imports(self): self.assertEqual( """\ pass pass x = 1 """, ''.join(autoflake.filter_code("""\ import foo import zap x = 1 """, remove_all_unused_imports=True)))
def test_filter_code_with_additional_imports(self): self.assertEqual( """\ pass import zap x = 1 """, ''.join(autoflake.filter_code("""\ import foo import zap x = 1 """, additional_imports=['foo', 'bar'])))
def test_filter_code_expand_star_imports(self): self.assertEqual( """\ from math import sin sin(1) """, ''.join(autoflake.filter_code("""\ from math import * sin(1) """, expand_star_imports=True))) self.assertEqual( """\ from math import cos, sin sin(1) cos(1) """, ''.join(autoflake.filter_code("""\ from math import * sin(1) cos(1) """, expand_star_imports=True)))
def test_filter_code_should_ignore_semicolons(self): self.assertEqual( r"""\ import os pass import os; import math, subprocess os.foo() """, ''.join(autoflake.filter_code(r"""\ import os import re import os; import math, subprocess os.foo() """)))
def test_filter_code_should_ignore_imports_with_inline_comment(self): self.assertEqual( """\ from os import path # foo pass from fake_foo import z # foo, foo, zap x = 1 """, ''.join(autoflake.filter_code("""\ from os import path # foo from os import path from fake_foo import z # foo, foo, zap x = 1 """)))
def test_filter_code_ignore_multiple_star_import(self): self.assertEqual( """\ from math import * from re import * sin(1) cos(1) """, ''.join(autoflake.filter_code("""\ from math import * from re import * sin(1) cos(1) """, expand_star_imports=True)))
def test_filter_code_with_indented_import(self): self.assertEqual( """\ import os if True: pass os.foo() """, ''.join( autoflake.filter_code("""\ import os if True: import re os.foo() """)))
def test_filter_code_should_respect_noqa(self): self.assertEqual( """\ pass import re # noqa from subprocess import Popen # NOQA x = 1 """, ''.join(autoflake.filter_code("""\ from os import path import re # noqa from subprocess import Popen # NOQA x = 1 """)))
def test_filter_code_should_ignore_semicolons(self): self.assertEqual( r"""\ import os pass import os; import math, subprocess os.foo() """, ''.join( autoflake.filter_code(r"""\ import os import re import os; import math, subprocess os.foo() """)))
def test_filter_code_with_indented_import(self): self.assertEqual( """\ import os if True: pass os.foo() """, ''.join(autoflake.filter_code("""\ import os if True: import re os.foo() """)))
def test_filter_code_should_respect_noqa(self): self.assertEqual( """\ pass import re # noqa from subprocess import Popen # NOQA x = 1 """, ''.join( autoflake.filter_code("""\ from os import path import re # noqa from subprocess import Popen # NOQA x = 1 """)))
def test_filter_code_should_ignore_imports_with_inline_comment(self): self.assertEqual( """\ from os import path # foo pass from fake_foo import z # foo, foo, zap x = 1 """, ''.join( autoflake.filter_code("""\ from os import path # foo from os import path from fake_foo import z # foo, foo, zap x = 1 """)))
def test_filter_code_with_special_re_symbols_in_key(self): self.assertEqual( """\ a = { '????': 2, } print(a) """, ''.join(autoflake.filter_code("""\ a = { '????': 3, '????': 2, } print(a) """, remove_duplicate_keys=True)))
def test_filter_code_should_ignore_unsafe_imports(self): self.assertEqual( """\ import rlcompleter pass pass pass print(1) """, ''.join( autoflake.filter_code("""\ import rlcompleter import sys import io import os print(1) """)))
def test_filter_code_should_ignore_multiline_imports(self): self.assertEqual( r"""\ import os pass import os, \ math, subprocess os.foo() """, ''.join( autoflake.filter_code(r"""\ import os import re import os, \ math, subprocess os.foo() """)))
def test_filter_code_should_ignore_multiline_imports(self): self.assertEqual( r"""\ import os pass import os, \ math, subprocess os.foo() """, ''.join(autoflake.filter_code(r"""\ import os import re import os, \ math, subprocess os.foo() """)))
def test_filter_code_should_ignore_unsafe_imports(self): self.assertEqual( """\ import rlcompleter pass pass pass print(1) """, ''.join(autoflake.filter_code("""\ import rlcompleter import sys import io import os print(1) """)))
def test_filter_code_should_ignore_non_standard_library(self): self.assertEqual( """\ import os import my_own_module pass from my_package import another_module from my_package import subprocess from my_blah.my_blah_blah import blah os.foo() """, ''.join( autoflake.filter_code("""\ import os import my_own_module import re from my_package import another_module from my_package import subprocess from my_blah.my_blah_blah import blah os.foo() """)))
def test_filter_code_should_ignore_non_standard_library(self): self.assertEqual( """\ import os import my_own_module pass from my_package import another_module from my_package import subprocess from my_blah.my_blah_blah import blah os.foo() """, ''.join(autoflake.filter_code("""\ import os import my_own_module import re from my_package import another_module from my_package import subprocess from my_blah.my_blah_blah import blah os.foo() """)))