示例#1
0
def test_symbol_from_assignment():
    src = dedent('''
        def f(n): sys.stderr = n
        ''')
    unresolved, _ = Scope.from_source(
        src).find_unresolved_and_unreferenced_symbols()
    assert unresolved == set(['sys.stderr'])
示例#2
0
def test_importer_directives_not_referenced(index):
    src = dedent('''
        # Make sure the in thread reactor is installed.
        from Tribler.Core.Utilities.twisted_thread import reactor


        # importmagic: manage
        import re
        import sys


        print(os.path.basename('moo'))
        ''').strip()
    expected_src = dedent('''
        # Make sure the in thread reactor is installed.
        from Tribler.Core.Utilities.twisted_thread import reactor


        # importmagic: manage
        import os.path


        print(os.path.basename('moo'))
        ''').strip()
    scope = Scope.from_source(src)
    new_src = update_imports(
        src, index, *scope.find_unresolved_and_unreferenced_symbols()).strip()
    assert expected_src == new_src
def test_from_import_as(index):
    src = dedent('''
        from clastic import MiddleWare as WebMiddleWare
        ''').strip()
    scope = Scope.from_source(src)
    new_src = update_imports(src, index, *scope.find_unresolved_and_unreferenced_symbols())
    assert src == new_src.strip()
def test_import_future_preserved(index):
    src = 'from __future__ import absolute_import'
    unresolved, unreferenced = Scope.from_source(src).find_unresolved_and_unreferenced_symbols()
    assert not unresolved
    assert not unreferenced
    new_src = update_imports(src, index, unresolved, unreferenced).strip()
    assert src == new_src
示例#5
0
def test_path_from_node_subscript():
    src = dedent('''
        sys.path[0].tolower()
        ''')
    unresolved, _ = Scope.from_source(
        src).find_unresolved_and_unreferenced_symbols()
    assert unresolved == set(['sys.path'])
示例#6
0
def test_importer_wrapping_parentheses_longer(index):
    Imports.set_style(multiline='parentheses',
                      max_columns=80,
                      indent_with_tabs=False)
    src = dedent('''
        from injector import Binder, Injector, InstanceProvider, Key, MappingKey, Module, Scope, ScopeDecorator, SequenceKey, inject, provides, singleton, more, things, imported, foo, bar, baz, cux, lorem, ipsum
        from module_with_long_name.classes.some_prefix.another_prefix.waffle import stuff

        Binder, Injector, InstanceProvider, Key, MappingKey, Module, Scope, ScopeDecorator, SequenceKey, inject, provides, singleton, more, things, imported, foo, bar, baz, cux, lorem, ipsum, stuff
        ''').strip()
    expected_src = dedent('''
        from injector import (Binder, Injector, InstanceProvider, Key, MappingKey,
            Module, Scope, ScopeDecorator, SequenceKey, bar, baz, cux, foo, imported,
            inject, ipsum, lorem, more, provides, singleton, things)
        from module_with_long_name.classes.some_prefix.another_prefix.waffle import (
            stuff)


        Binder, Injector, InstanceProvider, Key, MappingKey, Module, Scope, ScopeDecorator, SequenceKey, inject, provides, singleton, more, things, imported, foo, bar, baz, cux, lorem, ipsum, stuff
        ''').strip()

    scope = Scope.from_source(src)
    new_src = update_imports(
        src, index, *scope.find_unresolved_and_unreferenced_symbols()).strip()
    assert expected_src == new_src
示例#7
0
def test_symbol_from_argument_defaults():
    src = dedent("""
        def f(a, b=os.path, c=os): pass
        """)

    symbols, _ = Scope.from_source(src).find_unresolved_and_unreferenced_symbols()
    assert symbols == set(['os.path', 'os'])
示例#8
0
def test_path_from_node_function():
    src = dedent('''
        os.path.basename(waz).tolower()
        ''')
    unresolved, _ = Scope.from_source(
        src).find_unresolved_and_unreferenced_symbols()
    assert unresolved == set(['waz', 'os.path.basename'])
示例#9
0
def test_deep_package_reference_with_subscript():
    src = dedent('''
        print(sys.path[0])
        ''')
    symbols, _ = Scope.from_source(
        src).find_unresolved_and_unreferenced_symbols()
    assert symbols == set(['sys.path'])
示例#10
0
def test_symbol_from_decorator():
    src = dedent("""
        @foo.bar(a=waz)
        def bar(): pass
        """)
    symbols, _ = Scope.from_source(src).find_unresolved_and_unreferenced_symbols()
    assert symbols == set(['foo.bar', 'waz'])
示例#11
0
 def _collect(self, src, include_unreferenced=False):
     scope = Scope.from_source(src)
     unresolved, unreferenced = scope.find_unresolved_and_unreferenced_symbols(
     )
     if include_unreferenced:
         return unresolved, unreferenced
     return unresolved
示例#12
0
def test_deep_package_reference_with_function_call():
    src = dedent('''
        print(os.path.dirname('src/python'))
        ''')
    symbols, _ = Scope.from_source(
        src).find_unresolved_and_unreferenced_symbols()
    assert symbols == set(['os.path.dirname'])
示例#13
0
def test_from_import_as(index):
    src = dedent('''
        from clastic import MiddleWare as WebMiddleWare
        ''').strip()
    scope = Scope.from_source(src)
    new_src = update_imports(src, index, *scope.find_unresolved_and_unreferenced_symbols())
    assert src == new_src.strip()
示例#14
0
def test_import_future_preserved(index):
    src = 'from __future__ import absolute_import'
    unresolved, unreferenced = Scope.from_source(src).find_unresolved_and_unreferenced_symbols()
    assert not unresolved
    assert not unreferenced
    new_src = update_imports(src, index, unresolved, unreferenced).strip()
    assert src == new_src
示例#15
0
def test_importer_directives_not_referenced(index):
    src = dedent('''
        # Make sure the in thread reactor is installed.
        from Tribler.Core.Utilities.twisted_thread import reactor


        # importmagic: manage
        import re
        import sys


        print(os.path.basename('moo'))
        ''').strip()
    expected_src = dedent('''
        # Make sure the in thread reactor is installed.
        from Tribler.Core.Utilities.twisted_thread import reactor


        # importmagic: manage
        import os.path


        print(os.path.basename('moo'))
        ''').strip()
    scope = Scope.from_source(src)
    new_src = update_imports(src, index, *scope.find_unresolved_and_unreferenced_symbols()).strip()
    assert expected_src == new_src
示例#16
0
def test_symbol_from_nested_tuples():
    src = dedent("""
        a = (os, (os.path, sys))
        """)
    symbols, _ = Scope.from_source(
        src).find_unresolved_and_unreferenced_symbols()
    assert symbols == set(['os', 'os.path', 'sys'])
示例#17
0
def test_accepts_unicode_strings():
    src = dedent(u("""
        # coding: utf-8
        foo
    """)).strip()
    scope = Scope.from_source(src)
    unresolved, unreferenced = scope.find_unresolved_and_unreferenced_symbols()
    assert unresolved == set(['foo'])
示例#18
0
def test_referenced_symbols_from_decorated_function():
    src = dedent("""
        @foo.bar
        def bar():
            print(waz)
            print(baz)
        """)
    symbols, _ = Scope.from_source(src).find_unresolved_and_unreferenced_symbols()
    assert symbols == set(['foo.bar', 'waz', 'baz'])
示例#19
0
def test_symbol_in_expression():
    src = dedent('''
        (db.Event.creator_id == db.Account.id) & (db.Account.user_id == bindparam('user_id'))
        ''')
    unresolved, _ = Scope.from_source(
        src).find_unresolved_and_unreferenced_symbols()
    assert unresolved == set([
        'db.Event.creator_id', 'db.Account.id', 'db.Account.user_id',
        'bindparam'
    ])
示例#20
0
def test_annotations_without_imports():
    src = dedent("""
        def print_it(it: Iterable):
            for i in it:
                method1(i)
        print_it(['a', 'b', 'c'])
        """)
    scope = Scope.from_source(src)
    unresolved, unreferenced = scope.find_unresolved_and_unreferenced_symbols()
    assert unresolved == set(['method1', 'Iterable'])
    assert unreferenced == set([])
示例#21
0
def test_annotations_without_imports():
    src = dedent("""
        def print_it(it: Iterable):
            for i in it:
                method1(i)
        print_it(['a', 'b', 'c'])
        """)
    scope = Scope.from_source(src)
    unresolved, unreferenced = scope.find_unresolved_and_unreferenced_symbols()
    assert unresolved == set(['method1', 'Iterable'])
    assert unreferenced == set([])
示例#22
0
def test_annotations_return_type():
    # https://www.python.org/dev/peps/pep-3107/
    src = dedent("""
        def foo(a) -> CustomType:
            print(a)
        
        foo()
        """)
    scope = Scope.from_source(src)
    unresolved, unreferenced = scope.find_unresolved_and_unreferenced_symbols()
    assert unresolved == set(['CustomType'])
    assert unreferenced == set([])
示例#23
0
def test_annotations_with_imports():
    src = dedent("""
        from typing import Iterable

        def print_it(it: Iterable):
            for i in it:
                print(i)
        """)
    scope = Scope.from_source(src)
    unresolved, unreferenced = scope.find_unresolved_and_unreferenced_symbols()
    assert unresolved == set([])
    assert unreferenced == set(['print_it'])
示例#24
0
def test_annotations_return_type():
    # https://www.python.org/dev/peps/pep-3107/
    src = dedent("""
        def foo(a) -> CustomType:
            print(a)
        
        foo()
        """)
    scope = Scope.from_source(src)
    unresolved, unreferenced = scope.find_unresolved_and_unreferenced_symbols()
    assert unresolved == set(['CustomType'])
    assert unreferenced == set([])
示例#25
0
def test_annotations_with_imports():
    src = dedent("""
        from typing import Iterable

        def print_it(it: Iterable):
            for i in it:
                print(i)
        """)
    scope = Scope.from_source(src)
    unresolved, unreferenced = scope.find_unresolved_and_unreferenced_symbols()
    assert unresolved == set([])
    assert unreferenced == set(['print_it'])
示例#26
0
def test_deep_import_of_unknown_symbol(index):
    src = dedent("""
         print(os.unknown('/'))
         """).strip()
    unresolved, unreferenced = Scope.from_source(src).find_unresolved_and_unreferenced_symbols()
    assert unresolved == set(['os.unknown'])
    new_src = update_imports(src, index, unresolved, unreferenced)
    assert dedent("""
        import os


        print(os.unknown('/'))
        """).strip() == new_src.strip()
示例#27
0
def test_annotations_complex():
    # https://www.python.org/dev/peps/pep-3107/
    src = dedent("""
        def foo(a: 'x', b: 5 + 6, c: list, \
            d: Iterable, e: CustomType) -> max(2, 9):
                print(a)
        
        foo()
        """)
    scope = Scope.from_source(src)
    unresolved, unreferenced = scope.find_unresolved_and_unreferenced_symbols()
    assert unresolved == set(['Iterable', 'CustomType'])
    assert unreferenced == set([])
示例#28
0
def test_update_imports_correctly_aliases(index):
    src = dedent('''
        print(basename('src/foo'))
        ''').strip()
    unresolved, unreferenced = Scope.from_source(src).find_unresolved_and_unreferenced_symbols()
    assert unresolved == set(['basename'])
    new_src = update_imports(src, index, unresolved, unreferenced)
    assert dedent('''
        from os.path import basename


        print(basename('src/foo'))
        ''').strip() == new_src.strip()
示例#29
0
def test_annotations_complex():
    # https://www.python.org/dev/peps/pep-3107/
    src = dedent("""
        def foo(a: 'x', b: 5 + 6, c: list, \
            d: Iterable, e: CustomType) -> max(2, 9):
                print(a)
        
        foo()
        """)
    scope = Scope.from_source(src)
    unresolved, unreferenced = scope.find_unresolved_and_unreferenced_symbols()
    assert unresolved == set(['Iterable', 'CustomType'])
    assert unreferenced == set([])
def test_update_imports_correctly_aliases(index):
    src = dedent('''
        print(basename('src/foo'))
        ''').strip()
    unresolved, unreferenced = Scope.from_source(src).find_unresolved_and_unreferenced_symbols()
    assert unresolved == set(['basename'])
    new_src = update_imports(src, index, unresolved, unreferenced)
    assert dedent('''
        from os.path import basename


        print(basename('src/foo'))
        ''').strip() == new_src
def test_deep_import_of_unknown_symbol(index):
    src = dedent("""
        print(os.unknown('/'))
         """).strip()
    unresolved, unreferenced = Scope.from_source(src).find_unresolved_and_unreferenced_symbols()
    assert unresolved == set(['os.unknown'])
    new_src = update_imports(src, index, unresolved, unreferenced)
    assert dedent("""
        import os


        print(os.unknown('/'))
        """).strip() == new_src
示例#32
0
def test_get_update(index):
    src = dedent("""
         # header comment
         import sys

         print(os.unknown('/'))
         """).strip()
    unresolved, unreferenced = Scope.from_source(src).find_unresolved_and_unreferenced_symbols()
    start_line, end_line, new_block = get_update(src, index, unresolved, unreferenced)
    assert dedent("""\
        import os


        """).lstrip() == new_block
示例#33
0
def test_imports_module_assignment(index):
    src = dedent('''

        def func(n):
            sys.stderr = n
        ''').strip()
    scope = Scope.from_source(src)
    new_src = update_imports(src, index, *scope.find_unresolved_and_unreferenced_symbols())
    assert dedent('''
        import sys


        def func(n):
            sys.stderr = n
        ''').strip() == new_src.strip()
示例#34
0
def test_import_as_kept(index):
    src = dedent('''
        import time as taim


        taim.sleep(0)
        ''').strip()
    scope = Scope.from_source(src)
    new_src = update_imports(src, index, *scope.find_unresolved_and_unreferenced_symbols())
    assert dedent('''
        import time as taim


        taim.sleep(0)
        ''').strip() == new_src.strip()
def test_imports_removes_unused(index):
    src = dedent('''
        import sys

        def func(n):
            print(basename(n))
        ''').strip()
    scope = Scope.from_source(src)
    new_src = update_imports(src, index, *scope.find_unresolved_and_unreferenced_symbols())
    assert dedent('''
        from os.path import basename


        def func(n):
            print(basename(n))
        ''').strip() == new_src
示例#36
0
def test_imports_removes_unused(index):
    src = dedent('''
        import sys

        def func(n):
            print(basename(n))
        ''').strip()
    scope = Scope.from_source(src)
    new_src = update_imports(src, index, *scope.find_unresolved_and_unreferenced_symbols())
    assert dedent('''
        from os.path import basename


        def func(n):
            print(basename(n))
        ''').strip() == new_src.strip()
示例#37
0
def test_annotations_from_typing():
    src = dedent("""
        from typing import Dict, Tuple

        Vector = List[float]

        def scale(scalar: float, vector: Vector) -> Vector:
            return [scalar * num for num in vector]

        new_vector = scale(2.0, [1.0, -4.2, 5.4])
        print(new_vector)
        """)
    scope = Scope.from_source(src)
    unresolved, unreferenced = scope.find_unresolved_and_unreferenced_symbols()
    assert unresolved == set(['List'])
    assert unreferenced == set(['Tuple', 'Dict'])
def update_imports_for_view(edit, view, index):
    # Extract symbols from source
    src = view.substr(sublime.Region(0, view.size()))
    scope = Scope.from_source(src)
    unresolved, unreferenced = scope.find_unresolved_and_unreferenced_symbols()

    # Get update region and replacement text.
    start_line, end_line, text = get_update(src, index, unresolved, unreferenced)

    # Get region that needs updating
    start = view.text_point(start_line, 0)
    end = view.text_point(end_line, 0)
    region = sublime.Region(start, end)

    # Replace existing imports!
    view.replace(edit, region, text)
示例#39
0
def test_annotations_from_typing():
    src = dedent("""
        from typing import Dict, Tuple

        Vector = List[float]

        def scale(scalar: float, vector: Vector) -> Vector:
            return [scalar * num for num in vector]

        new_vector = scale(2.0, [1.0, -4.2, 5.4])
        print(new_vector)
        """)
    scope = Scope.from_source(src)
    unresolved, unreferenced = scope.find_unresolved_and_unreferenced_symbols()
    assert unresolved == set(['List'])
    assert unreferenced == set(['Tuple', 'Dict'])
示例#40
0
def test_import_as_kept(index):
    src = dedent('''
        import time as taim


        taim.sleep(0)
        ''').strip()
    scope = Scope.from_source(src)
    new_src = update_imports(src, index,
                             *scope.find_unresolved_and_unreferenced_symbols())
    assert dedent('''
        import time as taim


        taim.sleep(0)
        ''').strip() == new_src.strip()
示例#41
0
def test_imports_module_assignment(index):
    src = dedent('''

        def func(n):
            sys.stderr = n
        ''').strip()
    scope = Scope.from_source(src)
    new_src = update_imports(src, index,
                             *scope.find_unresolved_and_unreferenced_symbols())
    assert dedent('''
        import sys


        def func(n):
            sys.stderr = n
        ''').strip() == new_src.strip()
示例#42
0
def test_parser_class_methods_namespace_correctly():
    src = dedent('''
        class Class(object):
            def __init__(self):
                self.value = 1
                get_value()  # Should be unresolved

            def get_value(self):
                return self.value

            def set_value(self, value):
                self.value = value

            setter = set_value  # Should be resolved
        ''')
    unresolved, _ = Scope.from_source(src).find_unresolved_and_unreferenced_symbols()
    assert unresolved == set(['get_value'])
示例#43
0
def test_parser_symbol_in_global_function():
    src = dedent('''
        import posixpath
        import os as thisos

        class Class(object):
            def foo(self):
                print(self.bar)

        def basename_no_ext(filename, default=1):
            def inner():
                print(basename)

            basename, _ = os.path.splitext(os.path.basename(filename))
            moo = 10
            inner()

            with open('foo') as fd:
                print(fd.read())

            try:
                print('foo')
            except Exception as e:
                print(e)

        basename_no_ext(sys.path)

        for path in sys.path:
            print(path)

        sys.path[0] = 10

        moo = lambda a: True

        comp = [p for p in sys.path if p]

        sys.path[10] = 2

        posixpath.join(['a', 'b'])


        ''')
    symbols, _ = Scope.from_source(
        src).find_unresolved_and_unreferenced_symbols()
    assert symbols == set(['sys.path', 'os.path.splitext', 'os.path.basename'])
def test_update_imports_inserts_imports(index):
    src = dedent("""
        import sys

        print(os.path.basename("sys/foo"))
        print(sys.path[0])
        """).strip()
    unresolved, unreferenced = Scope.from_source(src).find_unresolved_and_unreferenced_symbols()
    assert unresolved == set(['os.path.basename'])
    new_src = update_imports(src, index, unresolved, unreferenced)
    assert dedent("""
        import os.path
        import sys


        print(os.path.basename("sys/foo"))
        print(sys.path[0])
        """).strip() == new_src
def test_imports_dont_delete_trailing_comments(index):
    src = dedent('''
        import sys

        # Some function
        def func(n):
            print(basename(n))
        ''').strip()
    scope = Scope.from_source(src)
    new_src = update_imports(src, index, *scope.find_unresolved_and_unreferenced_symbols())
    assert dedent('''
        from os.path import basename


        # Some function
        def func(n):
            print(basename(n))
        ''').strip() == new_src
示例#46
0
def test_parser_symbol_in_global_function():
    src = dedent('''
        import posixpath
        import os as thisos

        class Class(object):
            def foo(self):
                print(self.bar)

        def basename_no_ext(filename, default=1):
            def inner():
                print(basename)

            basename, _ = os.path.splitext(os.path.basename(filename))
            moo = 10
            inner()

            with open('foo') as fd:
                print(fd.read())

            try:
                print('foo')
            except Exception as e:
                print(e)

        basename_no_ext(sys.path)

        for path in sys.path:
            print(path)

        sys.path[0] = 10

        moo = lambda a: True

        comp = [p for p in sys.path if p]

        sys.path[10] = 2

        posixpath.join(['a', 'b'])


        ''')
    symbols, _ = Scope.from_source(src).find_unresolved_and_unreferenced_symbols()
    assert symbols == set(['sys.path', 'os.path.splitext', 'os.path.basename'])
def test_importer_wrapping(index):
    src = dedent('''
        from injector import Binder, Injector, InstanceProvider, Key, MappingKey, Module, Scope, ScopeDecorator, SequenceKey, inject, provides, singleton
        from waffle import stuff

        Binder, Injector, InstanceProvider, Key, MappingKey, Module, Scope, ScopeDecorator, SequenceKey, inject, provides, singleton, stuff
        ''').strip()
    expected_src = dedent('''
        from injector import Binder, Injector, InstanceProvider, Key, MappingKey, \\
            Module, Scope, ScopeDecorator, SequenceKey, inject, provides, singleton
        from waffle import stuff


        Binder, Injector, InstanceProvider, Key, MappingKey, Module, Scope, ScopeDecorator, SequenceKey, inject, provides, singleton, stuff
        ''').strip()

    scope = Scope.from_source(src)
    new_src = update_imports(src, index, *scope.find_unresolved_and_unreferenced_symbols()).strip()
    assert expected_src == new_src
def test_parse_imports(index):
    src = dedent('''
        import os, sys as sys
        import sys as sys
        from os.path import basename

        from os import (
            path,
            posixpath
            )

        def main():
            pass
        ''').strip()
    unresolved, unreferenced = Scope.from_source(src).find_unresolved_and_unreferenced_symbols()
    new_src = update_imports(src, index, unresolved, unreferenced)
    assert dedent(r'''
        def main():
            pass
        ''').strip() == new_src
示例#49
0
def test_importer_wrapping_parentheses(index):
    Imports.set_style(multiline='parentheses', max_columns=80)
    src = dedent('''
        from injector import Binder, Injector, InstanceProvider, Key, MappingKey, Module, Scope, ScopeDecorator, SequenceKey, inject, provides, singleton
        from waffle import stuff

        Binder, Injector, InstanceProvider, Key, MappingKey, Module, Scope, ScopeDecorator, SequenceKey, inject, provides, singleton, stuff
        ''').strip()
    expected_src = dedent('''
        from injector import (Binder, Injector, InstanceProvider, Key, MappingKey,
            Module, Scope, ScopeDecorator, SequenceKey, inject, provides, singleton)
        from waffle import stuff


        Binder, Injector, InstanceProvider, Key, MappingKey, Module, Scope, ScopeDecorator, SequenceKey, inject, provides, singleton, stuff
        ''').strip()

    scope = Scope.from_source(src)
    new_src = update_imports(src, index, *scope.find_unresolved_and_unreferenced_symbols()).strip()
    assert expected_src == new_src
示例#50
0
def test_find_unresolved_and_unreferenced_symbols():
    src = dedent("""
        import os
        import sys
        import urllib2
        from os.path import basename

        def f(p):
            def b():
                f = 10
                print(f)
            return basename(p)

        class A(object):
            etc = os.walk('/etc')

            def __init__(self):
                print(sys.path, urllib.urlquote('blah'))

            def exc_handler(self):
                try:
                    raise SomeException(some_value)
                except:
                    pass
                else:
                    print(SOME_MSG)

            def cond(self, *args, **kwds):
                if cond:
                    pass
                elif cond2:
                    print('foo')
                while cond3:
                    print(kwds)

        """).strip()
    scope = Scope.from_source(src)
    unresolved, unreferenced = scope.find_unresolved_and_unreferenced_symbols()
    assert unresolved == set(['urllib.urlquote', 'SomeException', 'some_value',
                              'SOME_MSG', 'cond', 'cond2', 'cond3'])
    assert unreferenced == set(['A', 'urllib2', 'f'])
示例#51
0
def test_imports_dont_delete_imports_after_middle_comments(index):
    src = dedent('''
        import sys
        # Some comment
        import json

        def func(n):
            print(basename(n))
            print(json)
        ''').strip()
    scope = Scope.from_source(src)
    new_src = update_imports(src, index, *scope.find_unresolved_and_unreferenced_symbols())
    assert dedent('''
        import json
        from os.path import basename


        def func(n):
            print(basename(n))
            print(json)
        ''').strip() == new_src.strip()
示例#52
0
def test_importer_wrapping_escaped_longer(index):
    Imports.set_style(multiline='backslash', max_columns=80)
    src = dedent('''
        from injector import Binder, Injector, InstanceProvider, Key, MappingKey, Module, Scope, ScopeDecorator, SequenceKey, inject, provides, singleton, more, things, imported, foo, bar, baz, cux, lorem, ipsum
        from waffle import stuff

        Binder, Injector, InstanceProvider, Key, MappingKey, Module, Scope, ScopeDecorator, SequenceKey, inject, provides, singleton, more, things, imported, foo, bar, baz, cux, lorem, ipsum, stuff
        ''').strip()
    expected_src = dedent('''
        from injector import Binder, Injector, InstanceProvider, Key, MappingKey, \\
            Module, Scope, ScopeDecorator, SequenceKey, bar, baz, cux, foo, imported, \\
            inject, ipsum, lorem, more, provides, singleton, things
        from waffle import stuff


        Binder, Injector, InstanceProvider, Key, MappingKey, Module, Scope, ScopeDecorator, SequenceKey, inject, provides, singleton, more, things, imported, foo, bar, baz, cux, lorem, ipsum, stuff
        ''').strip()

    scope = Scope.from_source(src)
    new_src = update_imports(src, index, *scope.find_unresolved_and_unreferenced_symbols()).strip()
    assert expected_src == new_src
def test_update_imports_inserts_initial_imports(index):
    src = dedent("""
        print(os.path.basename('sys/foo'))
        print(sys.path[0])
        print(basename('sys/foo'))
        print(path.basename('sys/foo'))
        """).strip()
    unresolved, unreferenced = Scope.from_source(src).find_unresolved_and_unreferenced_symbols()
    assert unresolved == set(['os.path.basename', 'sys.path', 'basename', 'path.basename'])
    new_src = update_imports(src, index, unresolved, unreferenced)
    assert dedent("""
        import os.path
        import sys
        from os import path
        from os.path import basename


        print(os.path.basename('sys/foo'))
        print(sys.path[0])
        print(basename('sys/foo'))
        print(path.basename('sys/foo'))
        """).strip() == new_src
def test_importer_directives(index):
    src = dedent('''
        from gevent.monkey import patch_all; patch_all()

        # importmagic: manage
        import re
        import sys

        print(os.path.basename('moo'))
        ''').strip()
    expected_src = dedent('''
        from gevent.monkey import patch_all; patch_all()

        # importmagic: manage
        import os.path


        print(os.path.basename('moo'))
        ''').strip()
    scope = Scope.from_source(src)
    new_src = update_imports(src, index, *scope.find_unresolved_and_unreferenced_symbols()).strip()
    assert expected_src == new_src