def test_non_strict_evaluate(): def foo(bar): return bar assert evaluate(foo, bar=True) is True # first the evaluated case assert evaluate( foo, quuz=True ) is foo # now we missed the signature, so we get the function unevaluated back
def test_evaluate_on_methods(): class Foo(object): def bar(self, x): return x @staticmethod def baz(x): return x assert 17 == evaluate(Foo().bar, x=17) assert 17 == evaluate(Foo().baz, x=17) f = Foo().bar assert f is evaluate(f, y=17)
def test_evaluate_on_methods(): class Foo: # noinspection PyMethodMayBeStatic def bar(self, x): return x @staticmethod def baz(x): return x assert evaluate(Foo().bar, x=17) == 17 assert evaluate(Foo().baz, x=17) == 17 f = Foo().bar assert evaluate(f, y=17) is f
def test_evaluate_subset_parameters(): def f(x, **_): return x assert 17 == evaluate(f, x=17, y=42)
def test_no_evaluate_kwargs_mismatch(): def f(x): return x * 2 assert f is evaluate(f) assert f is evaluate(f, y=1)
def test_evaluate_extra_kwargs_with_defaults(): def f(x, y=17): return x assert 17 == evaluate(f, x=17)
def evaluate_member(obj, key, strict=True, **kwargs): value = getattr(obj, key) new_value = evaluate(value, __strict=strict, **kwargs) if new_value is not value: setattr(obj, key, new_value)
def member_from_model(cls, model, factory_lookup, defaults_factory, factory_lookup_register_function=None, field_name=None, model_field=None, **kwargs): if model_field is None: assert field_name is not None, "Field can't be automatically created from model, you must specify it manually" sub_field_name, _, field_path_rest = field_name.partition('__') # noinspection PyProtectedMember model_field = model._meta.get_field(sub_field_name) if field_path_rest: result = member_from_model(cls=cls, model=model_field.remote_field.model, factory_lookup=factory_lookup, defaults_factory=defaults_factory, factory_lookup_register_function= factory_lookup_register_function, field_name=field_path_rest, **kwargs) result.name = field_name result.attr = field_name return result factory = factory_lookup.get(type(model_field), MISSING) if factory is MISSING: for django_field_type, foo in reversed(list(factory_lookup.items())): if isinstance(model_field, django_field_type): factory = foo break # pragma: no mutate optimization if factory is MISSING: message = 'No factory for %s.' % type(model_field) if factory_lookup_register_function is not None: message += ' Register a factory with %s, you can also register one that returns None to not handle this field type' % factory_lookup_register_function.__name__ raise AssertionError(message) if factory is None: return None # Not strict evaluate on purpose factory = evaluate(factory, model_field=model_field, field_name=field_name) setdefaults_path( kwargs, name=field_name, call_target__cls=cls, ) defaults = defaults_factory(model_field) if isinstance(factory, Namespace): factory = setdefaults_path( Namespace(), factory, defaults, ) else: kwargs.update(**defaults) return factory(model_field=model_field, model=model, **kwargs)
def test_evaluate_extra_kwargs_with_defaults(): # noinspection PyUnusedLocal def f(x, y=17): return x assert evaluate(f, x=17) == 17