def describe_compositing_GuiceData_instances(): class GrandDaddy(object): pass g_data = GuiceData.from_class(GrandDaddy) g_data.init = object() g_data.methods = {'g_method': object(),'method': object()} class Daddy(GrandDaddy): pass d_data = GuiceData.from_class(Daddy) d_data.init = object() d_data.methods = {'d_method': object(),'method': object()} class Son(Daddy): pass s_data = GuiceData.from_class(Son) s_data.init = object() s_data.methods = {'s_method': object()} data = GuiceData.composite_from_class(Son) def should_have_create_a_new_GuiceData_instance(): assert isinstance(data, GuiceData) assert data not in (g_data, d_data, s_data) def should_have_all_unique_methods(): assert data.methods['g_method'] == g_data.methods['g_method'] assert data.methods['d_method'] == d_data.methods['d_method'] assert data.methods['s_method'] == s_data.methods['s_method'] def should_use_the_last_method_defined(): assert data.methods['method'] == d_data.methods['method']
def inject_members(self, instance): # this may be a little slow; done twice guice_data = _GuiceData.composite_from_class(instance.__class__) for name, gm in guice_data.methods.items(): kwargs = {} for param, guicearg in gm.items(): kwargs[param] = self.get_instance(guicearg.datatype, guicearg.annotation) getattr(instance, name)(**kwargs) return instance
def create_object(self, cls): if not hasattr(cls, '__mro__'): warnings.warn("can't create an instance of %r - no __mro__; " "this legacy behavior will be removed in a future " "version") return cls guice_data = _GuiceData.composite_from_class(cls) if not guice_data.init: instance = cls() else: kwargs = {} for name, guicearg in guice_data.init.items(): kwargs[name] = self.get_instance(guicearg.datatype, guicearg.annotation) instance = cls(**kwargs) return instance