def test_config__class(): """Test `config` can handle a class and class functions.""" add({Obj: Args(a=1), Obj.func: Args(b=2)}) obj = Obj(**get()) assert obj.results == (tuple(), {"a": 1}) obj = partial(Obj)() assert obj.results == (tuple(), {"a": 1}) result = obj.func(**get()) assert result == (tuple(), {"b": 2}) result = partial(obj.func)() assert result == (tuple(), {"b": 2})
def test_config__decorators(): """Test `config` unwraps decorators.""" add({_dec_func: Args(a=1), _dec_other_func: Args(b=2)}) result = _dec_func(**get()) assert result == (tuple(), {"a": 1}) result = _dec_other_func(**get()) assert result == (tuple(), {"b": 2}) add( { _dec_func.__wrapped__: Args(a=3), _dec_other_func.__wrapped__: Args(b=4) }, overwrite=True) result = _dec_func(**get()) assert result == (tuple(), {"a": 3}) result = _dec_other_func(**get()) assert result == (tuple(), {"b": 4}) add({_dec_other_func.__wrapped__.__wrapped__: Args(b=5)}, overwrite=True) result = _dec_other_func(**get()) assert result == (tuple(), {"b": 5}) with pytest.warns(DiffArgsWarning, match=_diff_args_message(_dec_other_func, "b")): assert _dec_other_func(b=6) == (tuple(), {"b": 6}) assert partial(_dec_other_func)() == (tuple(), {"b": 5}) add({Obj.dec_func: Args(c=7)}) obj = Obj() result = obj.dec_func(**get()) assert result == (tuple(), {"c": 7})
def test_config__new_class(): """Test `config` can handle class with `__new__` implemented.""" add({NewObj: Args(a=1, k=2)}) obj = NewObj(**get()) assert obj.results == (tuple(), {"a": 1, "k": 2}) assert partial(NewObj)().results == (tuple(), {"a": 1, "k": 2}) with pytest.warns(DiffArgsWarning, match=_diff_args_message(NewObj, "a")): assert NewObj(a=3).results == (tuple(), {"a": 3})
def test_config__dec_class(): """Test `config` can handle decorated class init.""" add({DecObj: Args(a=1)}) obj = DecObj(**get()) assert obj.results == (tuple(), {"a": 1}) assert partial(DecObj)().results == (tuple(), {"a": 1}) with pytest.warns(DiffArgsWarning, match=_diff_args_message(DecObj, "a")): assert DecObj(a=2).results == (tuple(), {"a": 2})
def test_config__partial(): """Test `config.partial` configures a partial.""" add({enumerate: Args(start=1)}) result = list(partial(enumerate)(range(3))) assert result == [(1, 0), (2, 1), (3, 2)]
def test__get_func_and_arg__partial_with_attributes(): """Test `_get_func_and_arg` can resolve partials with attributes.""" partial = functools.partial(_func.attr.bttr, a=_get_func_and_arg()) assert partial() == (tuple(), {"a": (_func.attr.bttr, "a")})
def test__get_func_and_arg__partial(): """Test `_get_func_and_arg` can resolve partials.""" partial = functools.partial(_func, a=_get_func_and_arg()) assert partial() == (tuple(), {"a": (_func, "a")})