<s> """<STR_LIT>""" <EOL> from contextlib import contextmanager <EOL> import abc <EOL> import threading <EOL> import traceback <EOL> import warnings <EOL> import pytest <EOL> from injector import ( <EOL> Binder , CallError , Injector , Scope , InstanceProvider , ClassProvider , <EOL> inject , singleton , threadlocal , UnsatisfiedRequirement , <EOL> CircularDependency , Module , provides , Key , SingletonScope , <EOL> ScopeDecorator , with_injector , AssistedBuilder , BindingKey , <EOL> SequenceKey , MappingKey , ProviderOf , <EOL> ) <EOL> def prepare_basic_injection ( ) : <EOL> class B ( object ) : <EOL> pass <EOL> class A ( object ) : <EOL> @ inject ( b = B ) <EOL> def __init__ ( self , b ) : <EOL> """<STR_LIT>""" <EOL> self . b = b <EOL> return A , B <EOL> def prepare_nested_injectors ( ) : <EOL> def configure ( binder ) : <EOL> binder . bind ( str , to = '<STR_LIT>' ) <EOL> parent = Injector ( configure ) <EOL> child = parent . create_child_injector ( ) <EOL> return parent , child <EOL> def check_exception_contains_stuff ( exception , stuff ) : <EOL> stringified = str ( exception ) <EOL> for thing in stuff : <EOL> assert thing in stringified , ( <EOL> '<STR_LIT>' % ( <EOL> thing , stringified ) ) <EOL> def test_child_injector_inherits_parent_bindings ( ) : <EOL> parent , child = prepare_nested_injectors ( ) <EOL> assert ( child . get ( str ) == parent . get ( str ) ) <EOL> def test_child_injector_overrides_parent_bindings ( ) : <EOL> parent , child = prepare_nested_injectors ( ) <EOL> child . binder . bind ( str , to = '<STR_LIT>' ) <EOL> assert ( ( parent . get ( str ) , child . get ( str ) ) == ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def test_child_injector_rebinds_arguments_for_parent_scope ( ) : <EOL> I = Key ( "<STR_LIT>" ) <EOL> Cls = Key ( "<STR_LIT>" ) <EOL> class A ( object ) : <EOL> @ inject ( val = I ) <EOL> def __init__ ( self , val ) : <EOL> self . val = val <EOL> def configure_parent ( binder ) : <EOL> binder . bind ( Cls , to = A ) <EOL> binder . bind ( I , to = "<STR_LIT>" ) <EOL> def configure_child ( binder ) : <EOL> binder . bind ( I , to = "<STR_LIT>" ) <EOL> parent = Injector ( configure_parent ) <EOL> assert ( parent . get ( Cls ) . val == "<STR_LIT>" ) <EOL> child = parent . create_child_injector ( configure_child ) <EOL> assert ( child . get ( Cls ) . val == "<STR_LIT>" ) <EOL> def test_scopes_are_only_bound_to_root_injector ( ) : <EOL> parent , child = prepare_nested_injectors ( ) <EOL> class A ( object ) : <EOL> pass <EOL> parent . binder . bind ( A , to = A , scope = singleton ) <EOL> assert ( parent . get ( A ) is child . get ( A ) ) <EOL> def test_key_cannot_be_instantiated ( ) : <EOL> Interface = Key ( '<STR_LIT>' ) <EOL> with pytest . raises ( Exception ) : <EOL> Interface ( ) <EOL> with pytest . raises ( Exception ) : <EOL> Injector ( ) . get ( Interface ) <EOL> def test_get_default_injected_instances ( ) : <EOL> A , B = prepare_basic_injection ( ) <EOL> def configure ( binder ) : <EOL> binder . bind ( A ) <EOL> binder . bind ( B ) <EOL> injector = Injector ( configure ) <EOL> assert ( injector . get ( Injector ) is injector ) <EOL> assert ( injector . get ( Binder ) is injector . binder ) <EOL> def test_instantiate_injected_method ( ) : <EOL> A , _ = prepare_basic_injection ( ) <EOL> a = A ( '<STR_LIT>' ) <EOL> assert ( a . b == '<STR_LIT>' ) <EOL> def test_method_decorator_is_wrapped ( ) : <EOL> A , _ = prepare_basic_injection ( ) <EOL> assert ( A . __init__ . __doc__ == '<STR_LIT>' ) <EOL> assert ( A . __init__ . __name__ == '<STR_LIT>' ) <EOL> def test_decorator_works_for_function_with_no_args ( ) : <EOL> @ inject ( s = str ) <EOL> def wrapped ( * args , ** kwargs ) : <EOL> pass <EOL> def test_providers_arent_called_for_dependencies_that_are_already_provided ( ) : <EOL> def configure ( binder ) : <EOL> binder . bind ( int , to = lambda : <NUM_LIT:1> / <NUM_LIT:0> ) <EOL> class A ( object ) : <EOL> @ inject ( i = int ) <EOL> def __init__ ( self , i ) : <EOL> pass <EOL> injector = Injector ( configure ) <EOL> builder = injector . get ( AssistedBuilder ( A ) ) <EOL> with pytest . raises ( ZeroDivisionError ) : <EOL> builder . build ( ) <EOL> builder . build ( i = <NUM_LIT:3> ) <EOL> def test_inject_direct ( ) : <EOL> A , B = prepare_basic_injection ( ) <EOL> def configure ( binder ) : <EOL> binder . bind ( A ) <EOL> binder . bind ( B ) <EOL> injector = Injector ( configure ) <EOL> a = injector . get ( A ) <EOL> assert ( isinstance ( a , A ) ) <EOL> assert ( isinstance ( a . b , B ) ) <EOL> def test_configure_multiple_modules ( ) : <EOL> A , B = prepare_basic_injection ( ) <EOL> def configure_a ( binder ) : <EOL> binder . bind ( A ) <EOL> def configure_b ( binder ) : <EOL> binder . bind ( B ) <EOL> injector = Injector ( [ configure_a , configure_b ] ) <EOL> a = injector . get ( A ) <EOL> assert ( isinstance ( a , A ) ) <EOL> assert ( isinstance ( a . b , B ) ) <EOL> def test_inject_with_missing_dependency ( ) : <EOL> A , _ = prepare_basic_injection ( ) <EOL> def configure ( binder ) : <EOL> binder . bind ( A ) <EOL> injector = Injector ( configure , auto_bind = False ) <EOL> with pytest . raises ( UnsatisfiedRequirement ) : <EOL> injector . get ( A ) <EOL> def test_inject_named_interface ( ) : <EOL> class B ( object ) : <EOL> pass <EOL> class A ( object ) : <EOL> @ inject ( b = B ) <EOL> def __init__ ( self , b ) : <EOL> self . b = b <EOL> def configure ( binder ) : <EOL> binder . bind ( A ) <EOL> binder . bind ( B ) <EOL> injector = Injector ( configure ) <EOL> a = injector . get ( A ) <EOL> assert ( isinstance ( a , A ) ) <EOL> assert ( isinstance ( a . b , B ) ) <EOL> def prepare_transitive_injection ( ) : <EOL> class C ( object ) : <EOL> pass <EOL> class B ( object ) : <EOL> @ inject ( c = C ) <EOL> def __init__ ( self , c ) : <EOL> self . c = c <EOL> class A ( object ) : <EOL> @ inject ( b = B ) <EOL> def __init__ ( self , b ) : <EOL> self . b = b <EOL> return A , B , C <EOL> def test_transitive_injection ( ) : <EOL> A , B , C = prepare_transitive_injection ( ) <EOL> def configure ( binder ) : <EOL> binder . bind ( A ) <EOL> binder . bind ( B ) <EOL> binder . bind ( C ) <EOL> injector = Injector ( configure ) <EOL> a = injector . get ( A ) <EOL> assert ( isinstance ( a , A ) ) <EOL> assert ( isinstance ( a . b , B ) ) <EOL> assert ( isinstance ( a . b . c , C ) ) <EOL> def test_transitive_injection_with_missing_dependency ( ) : <EOL> A , B , _ = prepare_transitive_injection ( ) <EOL> def configure ( binder ) : <EOL> binder . bind ( A ) <EOL> binder . bind ( B ) <EOL> injector = Injector ( configure , auto_bind = False ) <EOL> with pytest . raises ( UnsatisfiedRequirement ) : <EOL> injector . get ( A ) <EOL> with pytest . raises ( UnsatisfiedRequirement ) : <EOL> injector . get ( B ) <EOL> def test_inject_singleton ( ) : <EOL> class B ( object ) : <EOL> pass <EOL> class A ( object ) : <EOL> @ inject ( b = B ) <EOL> def __init__ ( self , b ) : <EOL> self . b = b <EOL> def configure ( binder ) : <EOL> binder . bind ( A ) <EOL> binder . bind ( B , scope = SingletonScope ) <EOL> injector1 = Injector ( configure ) <EOL> a1 = injector1 . get ( A ) <EOL> a2 = injector1 . get ( A ) <EOL> assert ( a1 . b is a2 . b ) <EOL> def test_inject_decorated_singleton_class ( ) : <EOL> @ singleton <EOL> class B ( object ) : <EOL> pass <EOL> class A ( object ) : <EOL> @ inject ( b = B ) <EOL> def __init__ ( self , b ) : <EOL> self . b = b <EOL> def configure ( binder ) : <EOL> binder . bind ( A ) <EOL> binder . bind ( B ) <EOL> injector1 = Injector ( configure ) <EOL> a1 = injector1 . get ( A ) <EOL> a2 = injector1 . get ( A ) <EOL> assert ( a1 . b is a2 . b ) <EOL> def test_threadlocal ( ) : <EOL> @ threadlocal <EOL> class A ( object ) : <EOL> def __init__ ( self ) : <EOL> pass <EOL> def configure ( binder ) : <EOL> binder . bind ( A ) <EOL> injector = Injector ( configure ) <EOL> a1 = injector . get ( A ) <EOL> a2 = injector . get ( A ) <EOL> assert ( a1 is a2 ) <EOL> a3 = [ None ] <EOL> ready = threading . Event ( ) <EOL> def inject_a3 ( ) : <EOL> a3 [ <NUM_LIT:0> ] = injector . get ( A ) <EOL> ready . set ( ) <EOL> threading . Thread ( target = inject_a3 ) . start ( ) <EOL> ready . wait ( <NUM_LIT:1.0> ) <EOL> assert ( a2 is not a3 [ <NUM_LIT:0> ] and a3 [ <NUM_LIT:0> ] is not None ) <EOL> def test_injecting_interface_implementation ( ) : <EOL> class Interface ( object ) : <EOL> pass <EOL> class Implementation ( object ) : <EOL> pass <EOL> class A ( object ) : <EOL> @ inject ( i = Interface ) <EOL> def __init__ ( self , i ) : <EOL> self . i = i <EOL> def configure ( binder ) : <EOL> binder . bind ( A ) <EOL> binder . bind ( Interface , to = Implementation ) <EOL> injector = Injector ( configure ) <EOL> a = injector . get ( A ) <EOL> assert ( isinstance ( a . i , Implementation ) ) <EOL> def test_cyclic_dependencies ( ) : <EOL> class Interface ( object ) : <EOL> pass <EOL> class A ( object ) : <EOL> @ inject ( i = Interface ) <EOL> def __init__ ( self , i ) : <EOL> self . i = i <EOL> class B ( object ) : <EOL> @ inject ( a = A ) <EOL> def __init__ ( self , a ) : <EOL> self . a = a <EOL> def configure ( binder ) : <EOL> binder . bind ( Interface , to = B ) <EOL> binder . bind ( A ) <EOL> injector = Injector ( configure ) <EOL> with pytest . raises ( CircularDependency ) : <EOL> injector . get ( A ) <EOL> def test_avoid_circular_dependency_with_method_injection ( ) : <EOL> class Interface ( object ) : <EOL> pass <EOL> class A ( object ) : <EOL> @ inject ( i = Interface ) <EOL> def __init__ ( self , i ) : <EOL> self . i = i <EOL> class B ( object ) : <EOL> @ inject ( a = A ) <EOL> def method ( self , a ) : <EOL> self . a = a <EOL> def configure ( binder ) : <EOL> binder . bind ( Interface , to = B ) <EOL> binder . bind ( A ) <EOL> binder . bind ( B ) <EOL> injector = Injector ( configure ) <EOL> a = injector . get ( A ) <EOL> assert ( isinstance ( a . i , B ) ) <EOL> b = injector . get ( B ) <EOL> b . method ( ) <EOL> assert ( isinstance ( b . a , A ) ) <EOL> def test_that_injection_is_lazy ( ) : <EOL> class Interface ( object ) : <EOL> constructed = False <EOL> def __init__ ( self ) : <EOL> Interface . constructed = True <EOL> class A ( object ) : <EOL> @ inject ( i = Interface ) <EOL> def __init__ ( self , i ) : <EOL> self . i = i <EOL> def configure ( binder ) : <EOL> binder . bind ( Interface ) <EOL> binder . bind ( A ) <EOL> injector = Injector ( configure ) <EOL> assert not ( Interface . constructed ) <EOL> injector . get ( A ) <EOL> assert ( Interface . constructed ) <EOL> def test_module_provides ( ) : <EOL> class MyModule ( Module ) : <EOL> @ provides ( str ) <EOL> def provide_name ( self ) : <EOL> return '<STR_LIT>' <EOL> module = MyModule ( ) <EOL> injector = Injector ( module ) <EOL> assert injector . get ( str ) == '<STR_LIT>' <EOL> def test_module_class_gets_instantiated ( ) : <EOL> name = '<STR_LIT>' <EOL> class MyModule ( Module ) : <EOL> def configure ( self , binder ) : <EOL> binder . bind ( str , to = name ) <EOL> injector = Injector ( MyModule ) <EOL> assert ( injector . get ( str ) == name ) <EOL> def test_with_injector_works ( ) : <EOL> name = '<STR_LIT>' <EOL> def configure ( binder ) : <EOL> binder . bind ( str , to = name ) <EOL> class Aaa ( object ) : <EOL> @ with_injector ( configure ) <EOL> @ inject ( username = str ) <EOL> def __init__ ( self , username ) : <EOL> self . username = username <EOL> aaa = Aaa ( ) <EOL> assert ( aaa . username == name ) <EOL> def test_bind_using_key ( ) : <EOL> Name = Key ( '<STR_LIT:name>' ) <EOL> Age = Key ( '<STR_LIT>' ) <EOL> class MyModule ( Module ) : <EOL> @ provides ( Name ) <EOL> def provides_name ( self ) : <EOL> return '<STR_LIT>' <EOL> def configure ( self , binder ) : <EOL> binder . bind ( Age , to = <NUM_LIT> ) <EOL> injector = Injector ( MyModule ( ) ) <EOL> assert ( injector . get ( Age ) == <NUM_LIT> ) <EOL> assert ( injector . get ( Name ) == '<STR_LIT>' ) <EOL> def test_inject_using_key ( ) : <EOL> Name = Key ( '<STR_LIT:name>' ) <EOL> Description = Key ( '<STR_LIT:description>' ) <EOL> class MyModule ( Module ) : <EOL> @ provides ( Name ) <EOL> def provide_name ( self ) : <EOL> return '<STR_LIT>' <EOL> @ provides ( Description ) <EOL> @ inject ( name = Name ) <EOL> def provide_description ( self , name ) : <EOL> return '<STR_LIT>' % name <EOL> assert ( Injector ( MyModule ( ) ) . get ( Description ) == '<STR_LIT>' ) <EOL> def test_inject_and_provide_coexist_happily ( ) : <EOL> class MyModule ( Module ) : <EOL> @ provides ( float ) <EOL> def provide_weight ( self ) : <EOL> return <NUM_LIT> <EOL> @ provides ( int ) <EOL> def provide_age ( self ) : <EOL> return <NUM_LIT> <EOL> @ provides ( str ) <EOL> @ inject ( age = int , weight = float ) <EOL> def provide_description ( self , age , weight ) : <EOL> return '<STR_LIT>' % ( age , weight ) <EOL> assert ( Injector ( MyModule ( ) ) . get ( str ) == '<STR_LIT>' ) <EOL> def test_multibind ( ) : <EOL> Names = Key ( '<STR_LIT>' ) <EOL> def configure_one ( binder ) : <EOL> binder . multibind ( Names , to = [ '<STR_LIT>' ] ) <EOL> def configure_two ( binder ) : <EOL> binder . multibind ( Names , to = [ '<STR_LIT>' ] ) <EOL> assert ( Injector ( [ configure_one , configure_two ] ) . get ( Names ) == [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_provides_sequence_decorator ( ) : <EOL> Names = SequenceKey ( '<STR_LIT>' ) <EOL> class MyModule ( Module ) : <EOL> @ provides ( Names ) <EOL> def bob ( self ) : <EOL> return [ '<STR_LIT>' ] <EOL> @ provides ( Names ) <EOL> def tom ( self ) : <EOL> return [ '<STR_LIT>' ] <EOL> assert ( Injector ( MyModule ( ) ) . get ( Names ) == [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_auto_bind ( ) : <EOL> class A ( object ) : <EOL> pass <EOL> injector = Injector ( ) <EOL> assert ( isinstance ( injector . get ( A ) , A ) ) <EOL> def test_custom_scope ( ) : <EOL> class RequestScope ( Scope ) : <EOL> def configure ( self ) : <EOL> self . context = None <EOL> @ contextmanager <EOL> def __call__ ( self , request ) : <EOL> assert self . context is None <EOL> self . context = { } <EOL> binder = self . injector . get ( Binder ) <EOL> binder . bind ( Request , to = request , scope = RequestScope ) <EOL> yield <EOL> self . context = None <EOL> def get ( self , key , provider ) : <EOL> if self . context is None : <EOL> raise UnsatisfiedRequirement ( None , key ) <EOL> try : <EOL> return self . context [ key ] <EOL> except KeyError : <EOL> provider = InstanceProvider ( provider . get ( self . injector ) ) <EOL> self . context [ key ] = provider <EOL> return provider <EOL> request = ScopeDecorator ( RequestScope ) <EOL> class Request ( object ) : <EOL> pass <EOL> @ request <EOL> class Handler ( object ) : <EOL> def __init__ ( self , request ) : <EOL> self . request = request <EOL> class RequestModule ( Module ) : <EOL> def configure ( self , binder ) : <EOL> binder . bind_scope ( RequestScope ) <EOL> @ provides ( Handler ) <EOL> @ inject ( request = Request ) <EOL> def handler ( self , request ) : <EOL> return Handler ( request ) <EOL> injector = Injector ( [ RequestModule ( ) ] , auto_bind = False ) <EOL> with pytest . raises ( UnsatisfiedRequirement ) : <EOL> injector . get ( Handler ) <EOL> scope = injector . get ( RequestScope ) <EOL> request = Request ( ) <EOL> with scope ( request ) : <EOL> handler = injector . get ( Handler ) <EOL> assert ( handler . request is request ) <EOL> with pytest . raises ( UnsatisfiedRequirement ) : <EOL> injector . get ( Handler ) <EOL> def test_bind_interface_of_list_of_types ( ) : <EOL> def configure ( binder ) : <EOL> binder . multibind ( [ int ] , to = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> binder . multibind ( [ int ] , to = [ <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] ) <EOL> injector = Injector ( configure ) <EOL> assert ( injector . get ( [ int ] ) == [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] ) <EOL> def test_provides_mapping ( ) : <EOL> StrInt = MappingKey ( '<STR_LIT>' ) <EOL> def configure ( binder ) : <EOL> binder . multibind ( StrInt , to = { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> binder . multibind ( StrInt , to = { '<STR_LIT>' : <NUM_LIT:2> } ) <EOL> class MyModule ( Module ) : <EOL> @ provides ( StrInt ) <EOL> def provide_numbers ( self ) : <EOL> return { '<STR_LIT>' : <NUM_LIT:3> } <EOL> @ provides ( StrInt ) <EOL> def provide_more_numbers ( self ) : <EOL> return { '<STR_LIT>' : <NUM_LIT:4> } <EOL> injector = Injector ( [ configure , MyModule ( ) ] ) <EOL> assert ( injector . get ( StrInt ) == { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT>' : <NUM_LIT:4> } ) <EOL> def test_binder_install ( ) : <EOL> class ModuleA ( Module ) : <EOL> def configure ( self , binder ) : <EOL> binder . bind ( str , to = '<STR_LIT>' ) <EOL> class ModuleB ( Module ) : <EOL> def configure ( self , binder ) : <EOL> binder . install ( ModuleA ( ) ) <EOL> injector = Injector ( [ ModuleB ( ) ] ) <EOL> assert ( injector . get ( str ) == '<STR_LIT>' ) <EOL> def test_binder_provider_for_method_with_explicit_provider ( ) : <EOL> injector = Injector ( ) <EOL> binder = injector . binder <EOL> provider = binder . provider_for ( int , to = InstanceProvider ( <NUM_LIT:1> ) ) <EOL> assert ( type ( provider ) is InstanceProvider ) <EOL> assert ( provider . get ( injector ) == <NUM_LIT:1> ) <EOL> def test_binder_provider_for_method_with_instance ( ) : <EOL> injector = Injector ( ) <EOL> binder = injector . binder <EOL> provider = binder . provider_for ( int , to = <NUM_LIT:1> ) <EOL> assert ( type ( provider ) is InstanceProvider ) <EOL> assert ( provider . get ( injector ) == <NUM_LIT:1> ) <EOL> def test_binder_provider_for_method_with_class ( ) : <EOL> injector = Injector ( ) <EOL> binder = injector . binder <EOL> provider = binder . provider_for ( int ) <EOL> assert ( type ( provider ) is ClassProvider ) <EOL> assert ( provider . get ( injector ) == <NUM_LIT:0> ) <EOL> def test_binder_provider_for_method_with_class_to_specific_subclass ( ) : <EOL> class A ( object ) : <EOL> pass <EOL> class B ( A ) : <EOL> pass <EOL> injector = Injector ( ) <EOL> binder = injector . binder <EOL> provider = binder . provider_for ( A , B ) <EOL> assert ( type ( provider ) is ClassProvider ) <EOL> assert ( isinstance ( provider . get ( injector ) , B ) ) <EOL> def test_binder_provider_for_type_with_metaclass ( ) : <EOL> A = abc . ABCMeta ( '<STR_LIT:A>' , ( object , ) , { } ) <EOL> injector = Injector ( ) <EOL> binder = injector . binder <EOL> assert ( isinstance ( binder . provider_for ( A , None ) . get ( injector ) , A ) ) <EOL> def test_injecting_undecorated_class_with_missing_dependencies_raises_the_right_error ( ) : <EOL> class ClassA ( object ) : <EOL> def __init__ ( self , parameter ) : <EOL> pass <EOL> class ClassB ( object ) : <EOL> @ inject ( a = ClassA ) <EOL> def __init__ ( self , a ) : <EOL> pass <EOL> injector = Injector ( ) <EOL> try : <EOL> injector . get ( ClassB ) <EOL> except CallError as ce : <EOL> check_exception_contains_stuff ( ce , ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def test_call_to_method_with_legitimate_call_error_raises_type_error ( ) : <EOL> class A ( object ) : <EOL> def __init__ ( self ) : <EOL> max ( ) <EOL> injector = Injector ( ) <EOL> with pytest . raises ( TypeError ) : <EOL> injector . get ( A ) <EOL> def test_call_to_method_containing_noninjectable_and_unsatisfied_dependencies_raises_the_right_error ( ) : <EOL> class A ( object ) : <EOL> @ inject ( something = str ) <EOL> def fun ( self , something , something_different ) : <EOL> pass <EOL> injector = Injector ( ) <EOL> a = injector . get ( A ) <EOL> try : <EOL> a . fun ( ) <EOL> except CallError as ce : <EOL> assert ( ce . args [ <NUM_LIT:0> ] == a ) <EOL> function = A . fun <EOL> try : <EOL> function = function . __func__ <EOL> except AttributeError : <EOL> pass <EOL> assert ( ce . args [ <NUM_LIT:1> ] . __name__ == function . __name__ ) <EOL> assert ( ce . args [ <NUM_LIT:2> ] == ( ) ) <EOL> assert ( ce . args [ <NUM_LIT:3> ] == { '<STR_LIT>' : str ( ) } ) <EOL> def test_call_error_is_raised_with_correct_traceback ( ) : <EOL> class A ( object ) : <EOL> @ inject ( x = str ) <EOL> def fun_a ( self , x = '<STR_LIT>' ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> class B ( object ) : <EOL> @ inject ( a = A ) <EOL> def fun_b ( self , a ) : <EOL> a . fun_a ( ) <EOL> injector = Injector ( ) <EOL> b = injector . get ( B ) <EOL> try : <EOL> b . fun_b ( ) <EOL> except : <EOL> tb = traceback . format_exc ( ) <EOL> assert '<STR_LIT>' in tb <EOL> def test_call_error_str_representation_handles_single_arg ( ) : <EOL> ce = CallError ( '<STR_LIT>' ) <EOL> assert str ( ce ) == '<STR_LIT>' <EOL> class NeedsAssistance ( object ) : <EOL> @ inject ( a = str ) <EOL> def __init__ ( self , a , b ) : <EOL> self . a = a <EOL> self . b = b <EOL> def test_assisted_builder_works_when_got_directly_from_injector ( ) : <EOL> injector = Injector ( ) <EOL> builder = injector . get ( AssistedBuilder ( NeedsAssistance ) ) <EOL> obj = builder . build ( b = <NUM_LIT> ) <EOL> assert ( ( obj . a , obj . b ) == ( str ( ) , <NUM_LIT> ) ) <EOL> def test_assisted_builder_works_when_injected ( ) : <EOL> class X ( object ) : <EOL> @ inject ( builder = AssistedBuilder ( NeedsAssistance ) ) <EOL> def __init__ ( self , builder ) : <EOL> self . obj = builder . build ( b = <NUM_LIT> ) <EOL> injector = Injector ( ) <EOL> x = injector . get ( X ) <EOL> assert ( ( x . obj . a , x . obj . b ) == ( str ( ) , <NUM_LIT> ) ) <EOL> def test_assisted_builder_uses_bindings ( ) : <EOL> Interface = Key ( '<STR_LIT>' ) <EOL> def configure ( binder ) : <EOL> binder . bind ( Interface , to = NeedsAssistance ) <EOL> injector = Injector ( configure ) <EOL> builder = injector . get ( AssistedBuilder ( Interface ) ) <EOL> x = builder . build ( b = <NUM_LIT> ) <EOL> assert ( ( type ( x ) , x . b ) == ( NeedsAssistance , <NUM_LIT> ) ) <EOL> def test_assisted_builder_uses_concrete_class_when_specified ( ) : <EOL> class X ( object ) : <EOL> pass <EOL> def configure ( binder ) : <EOL> binder . bind ( X , to = lambda : <NUM_LIT:1> / <NUM_LIT:0> ) <EOL> injector = Injector ( configure ) <EOL> builder = injector . get ( AssistedBuilder ( cls = X ) ) <EOL> builder . build ( ) <EOL> def test_assisted_builder_accepts_callables ( ) : <EOL> injector = Injector ( ) <EOL> builder = injector . get ( AssistedBuilder ( callable = lambda x : x * <NUM_LIT:2> ) ) <EOL> assert builder . build ( x = <NUM_LIT:3> ) == <NUM_LIT:6> <EOL> def test_assisted_builder_injection_is_safe_to_use_with_multiple_injectors ( ) : <EOL> class X ( object ) : <EOL> @ inject ( builder = AssistedBuilder ( NeedsAssistance ) ) <EOL> def y ( self , builder ) : <EOL> return builder <EOL> i1 , i2 = Injector ( ) , Injector ( ) <EOL> b1 = i1 . get ( X ) . y ( ) <EOL> b2 = i2 . get ( X ) . y ( ) <EOL> assert ( ( b1 . injector , b2 . injector ) == ( i1 , i2 ) ) <EOL> def test_assisted_builder_injection_uses_the_same_binding_key_every_time ( ) : <EOL> gen_key = lambda : BindingKey ( AssistedBuilder ( NeedsAssistance ) ) <EOL> assert gen_key ( ) == gen_key ( ) <EOL> class TestThreadSafety ( object ) : <EOL> def setup ( self ) : <EOL> self . event = threading . Event ( ) <EOL> def configure ( binder ) : <EOL> binder . bind ( str , to = lambda : self . event . wait ( ) and '<STR_LIT>' ) <EOL> class XXX ( object ) : <EOL> @ inject ( s = str ) <EOL> def __init__ ( self , s ) : <EOL> pass <EOL> self . injector = Injector ( configure ) <EOL> self . cls = XXX <EOL> def gather_results ( self , count ) : <EOL> objects = [ ] <EOL> lock = threading . Lock ( ) <EOL> def target ( ) : <EOL> o = self . injector . get ( self . cls ) <EOL> with lock : <EOL> objects . append ( o ) <EOL> threads = [ threading . Thread ( target = target ) for i in range ( count ) ] <EOL> for t in threads : <EOL> t . start ( ) <EOL> self . event . set ( ) <EOL> for t in threads : <EOL> t . join ( ) <EOL> return objects <EOL> def test_injection_is_thread_safe ( self ) : <EOL> objects = self . gather_results ( <NUM_LIT:2> ) <EOL> assert ( len ( objects ) == <NUM_LIT:2> ) <EOL> def test_singleton_scope_is_thread_safe ( self ) : <EOL> self . injector . binder . bind ( self . cls , scope = singleton ) <EOL> a , b = self . gather_results ( <NUM_LIT:2> ) <EOL> assert ( a is b ) <EOL> class TestClassInjection ( object ) : <EOL> def setup ( self ) : <EOL> class A ( object ) : <EOL> counter = <NUM_LIT:0> <EOL> def __init__ ( self ) : <EOL> A . counter += <NUM_LIT:1> <EOL> @ inject ( a = A ) <EOL> class B ( object ) : <EOL> pass <EOL> @ inject ( injectable = A ) <EOL> class C ( object ) : <EOL> def __init__ ( self , noninjectable ) : <EOL> self . noninjectable = noninjectable <EOL> self . injector = Injector ( ) <EOL> self . A = A <EOL> self . B = B <EOL> self . C = C <EOL> def test_inject_decorator_works_when_metaclass_used ( self ) : <EOL> WithABCMeta = abc . ABCMeta ( str ( '<STR_LIT>' ) , ( object , ) , { } ) <EOL> @ inject ( y = int ) <EOL> class X ( WithABCMeta ) : <EOL> pass <EOL> self . injector . get ( X ) <EOL> def test_instantiation_still_requires_parameters ( self ) : <EOL> for cls in ( self . B , self . C ) : <EOL> with pytest . raises ( Exception ) : <EOL> cls ( ) <EOL> try : <EOL> self . C ( noninjectable = <NUM_LIT:1> ) <EOL> assert False , '<STR_LIT>' <EOL> except Exception as e : <EOL> check_exception_contains_stuff ( e , ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> with pytest . raises ( Exception ) : <EOL> self . C ( injectable = self . A ( ) ) <EOL> def test_injection_works ( self ) : <EOL> b = self . injector . get ( self . B ) <EOL> a = b . a <EOL> assert ( type ( a ) == self . A ) <EOL> def test_assisted_injection_works ( self ) : <EOL> builder = self . injector . get ( AssistedBuilder ( self . C ) ) <EOL> c = builder . build ( noninjectable = <NUM_LIT:5> ) <EOL> assert ( ( type ( c . injectable ) , c . noninjectable ) == ( self . A , <NUM_LIT:5> ) ) <EOL> def test_members_are_injected_only_once ( self ) : <EOL> b = self . injector . get ( self . B ) <EOL> _1 = b . a <EOL> _2 = b . a <EOL> assert ( self . A . counter == <NUM_LIT:1> and _1 is _2 ) <EOL> def test_each_instance_gets_new_injection ( self ) : <EOL> count = <NUM_LIT:3> <EOL> objs = [ self . injector . get ( self . B ) . a for i in range ( count ) ] <EOL> assert ( self . A . counter == count ) <EOL> assert ( len ( set ( objs ) ) == count ) <EOL> def test_members_can_be_overwritten ( self ) : <EOL> b = self . injector . get ( self . B ) <EOL> b . a = <NUM_LIT> <EOL> assert ( b . a == <NUM_LIT> ) <EOL> def test_injected_members_starting_with_underscore_generate_sane_constructor ( self ) : <EOL> @ inject ( _b = self . B ) <EOL> class X ( object ) : <EOL> pass <EOL> x = self . injector . get ( X ) <EOL> assert ( type ( x . _b ) == self . B ) <EOL> x = X ( b = <NUM_LIT> ) <EOL> assert ( x . _b == <NUM_LIT> ) <EOL> def test_correct_exception_is_raised_when_argument_is_missing ( self ) : <EOL> @ inject ( s = str ) <EOL> class X ( object ) : <EOL> pass <EOL> with pytest . raises ( CallError ) : <EOL> self . B ( ) <EOL> with pytest . raises ( CallError ) : <EOL> self . B ( '<STR_LIT>' ) <EOL> def test_mutating_dict_while_iterating_it_bug ( self ) : <EOL> bindings = dict ( ( '<STR_LIT:_>' + str ( i ) , str ) for i in range ( <NUM_LIT:1000> ) ) <EOL> @ inject ( ** bindings ) <EOL> class X ( object ) : <EOL> pass <EOL> injector = Injector ( ) <EOL> injector . get ( X ) <EOL> def test_provides_and_scope_decorator_collaboration ( ) : <EOL> @ provides ( int ) <EOL> @ singleton <EOL> def provides_singleton ( ) : <EOL> return <NUM_LIT:10> <EOL> @ singleton <EOL> @ provides ( int ) <EOL> def singleton_provides ( ) : <EOL> return <NUM_LIT:10> <EOL> assert provides_singleton . __binding__ . scope == SingletonScope <EOL> assert singleton_provides . __binding__ . scope == SingletonScope <EOL> def test_injecting_into_method_of_object_that_is_falseish_works ( ) : <EOL> class X ( dict ) : <EOL> @ inject ( s = str ) <EOL> def __init__ ( self , s ) : <EOL> pass <EOL> injector = Injector ( ) <EOL> injector . get ( X ) <EOL> def test_injection_fails_when_injector_cant_install_itself_into_an_object_with_slots ( ) : <EOL> try : <EOL> class ClassName ( object ) : <EOL> __slots__ = ( ) <EOL> injector = Injector ( ) <EOL> injector . get ( ClassName ) <EOL> except Exception as e : <EOL> check_exception_contains_stuff ( e , ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> else : <EOL> assert False , '<STR_LIT>' <EOL> def test_injecting_function_will_work ( ) : <EOL> @ inject ( s = str ) <EOL> def function ( s ) : <EOL> return '<STR_LIT>' % s <EOL> def configure ( binder ) : <EOL> binder . bind ( str , to = '<STR_LIT>' ) <EOL> injector = Injector ( configure ) <EOL> wrapped = injector . get ( function ) <EOL> assert wrapped . __name__ == function . __name__ <EOL> result = wrapped ( ) <EOL> assert result == '<STR_LIT>' <EOL> def test_deprecated_module_configure_injection ( ) : <EOL> class Test ( Module ) : <EOL> @ inject ( name = int ) <EOL> def configure ( self , binder , name ) : <EOL> pass <EOL> class Test2 ( Module ) : <EOL> @ inject ( name = int ) <EOL> def __init__ ( self , name ) : <EOL> pass <EOL> @ inject ( name = int ) <EOL> class Test3 ( Module ) : <EOL> pass <EOL> @ inject ( name = int ) <EOL> def configure ( binder , name ) : <EOL> pass <EOL> for module in [ Test , Test2 , Test3 , configure , Test ( ) ] : <EOL> with warnings . catch_warnings ( record = True ) as w : <EOL> print ( module ) <EOL> Injector ( module ) <EOL> assert len ( w ) == <NUM_LIT:1> , w <EOL> def test_callable_provider_injection ( ) : <EOL> Name = Key ( "<STR_LIT:Name>" ) <EOL> Message = Key ( "<STR_LIT>" ) <EOL> @ inject ( name = Name ) <EOL> def create_message ( name ) : <EOL> return "<STR_LIT>" + name <EOL> def configure ( binder ) : <EOL> binder . bind ( Name , to = "<STR_LIT>" ) <EOL> binder . bind ( Message , to = create_message ) <EOL> injector = Injector ( [ configure ] ) <EOL> msg = injector . get ( Message ) <EOL> assert msg == "<STR_LIT>" <EOL> def test_providerof ( ) : <EOL> counter = [ <NUM_LIT:0> ] <EOL> def provide_str ( ) : <EOL> counter [ <NUM_LIT:0> ] += <NUM_LIT:1> <EOL> return '<STR_LIT:content>' <EOL> def configure ( binder ) : <EOL> binder . bind ( str , to = provide_str ) <EOL> injector = Injector ( configure ) <EOL> assert counter [ <NUM_LIT:0> ] == <NUM_LIT:0> <EOL> provider = injector . get ( ProviderOf ( str ) ) <EOL> assert counter [ <NUM_LIT:0> ] == <NUM_LIT:0> <EOL> assert provider . get ( ) == '<STR_LIT:content>' <EOL> assert counter [ <NUM_LIT:0> ] == <NUM_LIT:1> <EOL> assert provider . get ( ) == injector . get ( str ) <EOL> assert counter [ <NUM_LIT:0> ] == <NUM_LIT:3> <EOL> def test_providerof_cannot_be_bound ( ) : <EOL> def configure ( binder ) : <EOL> binder . bind ( ProviderOf ( int ) , to = InstanceProvider ( None ) ) <EOL> with pytest . raises ( Exception ) : <EOL> Injector ( configure ) <EOL> def test_providerof_is_safe_to_use_with_multiple_injectors ( ) : <EOL> def configure1 ( binder ) : <EOL> binder . bind ( int , to = <NUM_LIT:1> ) <EOL> def configure2 ( binder ) : <EOL> binder . bind ( int , to = <NUM_LIT:2> ) <EOL> injector1 = Injector ( configure1 ) <EOL> injector2 = Injector ( configure2 ) <EOL> provider_of = ProviderOf ( int ) <EOL> provider1 = injector1 . get ( provider_of ) <EOL> provider2 = injector2 . get ( provider_of ) <EOL> assert provider1 . get ( ) == <NUM_LIT:1> <EOL> assert provider2 . get ( ) == <NUM_LIT:2> <EOL> def test_special_interfaces_work_with_auto_bind_disabled ( ) : <EOL> class InjectMe ( object ) : <EOL> pass <EOL> def configure ( binder ) : <EOL> binder . bind ( InjectMe , to = InstanceProvider ( InjectMe ( ) ) ) <EOL> injector = Injector ( configure , auto_bind = False ) <EOL> injector . get ( ProviderOf ( InjectMe ) ) <EOL> injector . get ( AssistedBuilder ( cls = InjectMe ) ) </s>
