From b8607edb561cc5c44458fb33396fbb1aee40e9be Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 7 Dec 2020 00:18:58 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Implement=20params=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- royalnet/engineer/params.py | 12 ++++++------ royalnet/engineer/tests/test_params.py | 15 +++++++++------ 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/royalnet/engineer/params.py b/royalnet/engineer/params.py index a3505d88..c79fd411 100644 --- a/royalnet/engineer/params.py +++ b/royalnet/engineer/params.py @@ -31,7 +31,7 @@ def parameter_to_field(param: inspect.Parameter, **kwargs) -> Tuple[type, pydant return ( param.annotation, pydantic.Field( - default=param.default if param.default else None, + default=param.default if param.default is not inspect.Parameter.empty else ..., title=param.name, **kwargs, ), @@ -52,12 +52,12 @@ def signature_to_model(f: Callable, __config__: pydantic.BaseConfig = ModelConfi name: str = f.__name__ signature: inspect.Signature = inspect.signature(f) - params = {key: parameter_to_field(value) for key, value in signature.parameters if not key.startswith("_")} + params = {key: parameter_to_field(value) for key, value in signature.parameters.items() if not key.startswith("_")} - model: Type[pydantic.BaseModel] = pydantic.create_model(name, - __config__=ModelConfig, - **params, - **extra_params) + model = pydantic.create_model(name, + __config__=ModelConfig, + **params, + **extra_params) return model diff --git a/royalnet/engineer/tests/test_params.py b/royalnet/engineer/tests/test_params.py index 705b841b..e636c672 100644 --- a/royalnet/engineer/tests/test_params.py +++ b/royalnet/engineer/tests/test_params.py @@ -7,7 +7,7 @@ import royalnet.engineer as re @pytest.fixture def a_random_function(): - def f(big_f: str, _hidden: int) -> str: + def f(*, big_f: str, _hidden: int) -> str: return big_f return f @@ -15,9 +15,9 @@ def a_random_function(): def test_parameter_to_field(a_random_function): signature = inspect.signature(a_random_function) parameter = signature.parameters["big_f"] - fieldinfo = re.parameter_to_field(parameter) + t, fieldinfo = re.parameter_to_field(parameter) assert isinstance(fieldinfo, pydantic.fields.FieldInfo) - assert fieldinfo.default == parameter.default == str + assert fieldinfo.default is ... assert fieldinfo.title == parameter.name == "big_f" @@ -33,8 +33,11 @@ def test_signature_to_model(a_random_function): with pytest.raises(pydantic.ValidationError): Model() - with pytest.raises(pydantic.ValidationError): - Model(big_f="exists", _hidden="no") + model = Model(big_f="exists", _hidden="no") + assert isinstance(model, pydantic.BaseModel) + assert model.big_f == "exists" + with pytest.raises(AttributeError): + model._hidden with pytest.raises(pydantic.ValidationError): - Model(big_f=1) + Model(big_f=...)