From 7a766acd506db8b63bcf3c0b1aa6e985d9f031e3 Mon Sep 17 00:00:00 2001 From: Maxim V4S Date: Wed, 29 Jan 2025 14:41:38 +0300 Subject: [PATCH 1/3] feat: forbid extra args to runnable parameters --- qualibrate/parameters.py | 4 +++- qualibrate/qualibration_graph.py | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/qualibrate/parameters.py b/qualibrate/parameters.py index e165cda..b3e3d49 100644 --- a/qualibrate/parameters.py +++ b/qualibrate/parameters.py @@ -7,7 +7,7 @@ cast, ) -from pydantic import BaseModel, Field, model_validator +from pydantic import BaseModel, ConfigDict, Field, model_validator from qualibrate.utils.logger_m import logger from qualibrate.utils.naming import get_full_class_path @@ -31,6 +31,8 @@ class RunnableParameters(BaseModel): + model_config = ConfigDict(extra="forbid") + @classmethod def serialize(cls, **kwargs: Any) -> Mapping[str, Any]: schema = cls.model_json_schema() diff --git a/qualibrate/qualibration_graph.py b/qualibrate/qualibration_graph.py index ded0051..ee92374 100644 --- a/qualibrate/qualibration_graph.py +++ b/qualibrate/qualibration_graph.py @@ -369,7 +369,7 @@ def _run(self, **passed_parameters: Any) -> None: orchestrator = self._orchestrator_or_error() self.cleanup() nodes = self._get_all_nodes_parameters( - passed_parameters.get("nodes", {}) + passed_parameters.pop("nodes", {}) ) self._parameters = self.parameters.model_validate(passed_parameters) self.full_parameters = self.full_parameters_class.model_validate( From de50c04b9275b6476e3aa41982cd23d163f5da3a Mon Sep 17 00:00:00 2001 From: Maxim V4S Date: Wed, 29 Jan 2025 16:18:51 +0300 Subject: [PATCH 2/3] test: forbid extra args to runnable parameters --- .../test_parameters/test_node_and_graph_parameters.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/unit/test_parameters/test_node_and_graph_parameters.py b/tests/unit/test_parameters/test_node_and_graph_parameters.py index 9e21899..aab138a 100644 --- a/tests/unit/test_parameters/test_node_and_graph_parameters.py +++ b/tests/unit/test_parameters/test_node_and_graph_parameters.py @@ -1,4 +1,5 @@ from typing import Optional +from pydantic import ValidationError import pytest @@ -14,6 +15,16 @@ class SampleGraphParameters(GraphParameters): qubits: Optional[list[str]] = None other_param: str = "test" + @pytest.mark.parametrize( + "parameters_class", [SampleNodeParameters, SampleGraphParameters] + ) + def test_forbid_extra_parameters(self, parameters_class): + with pytest.raises(ValidationError) as ex: + parameters_class.model_validate({"invalid_key": None}) + errors = ex.value.errors() + assert errors[0]["type"] == "extra_forbidden" + assert errors[0]["loc"] == ('invalid_key',) + def test_node_targets_name(self): assert NodeParameters.targets_name == "qubits" From 6e9e27df83e2564265b4a836bed62baa1587481f Mon Sep 17 00:00:00 2001 From: Maxim V4S Date: Wed, 29 Jan 2025 16:32:19 +0300 Subject: [PATCH 3/3] refactor: run formatter --- tests/unit/test_parameters/test_node_and_graph_parameters.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/test_parameters/test_node_and_graph_parameters.py b/tests/unit/test_parameters/test_node_and_graph_parameters.py index aab138a..d75dc6a 100644 --- a/tests/unit/test_parameters/test_node_and_graph_parameters.py +++ b/tests/unit/test_parameters/test_node_and_graph_parameters.py @@ -1,7 +1,7 @@ from typing import Optional -from pydantic import ValidationError import pytest +from pydantic import ValidationError from qualibrate.parameters import GraphParameters, NodeParameters @@ -23,7 +23,7 @@ def test_forbid_extra_parameters(self, parameters_class): parameters_class.model_validate({"invalid_key": None}) errors = ex.value.errors() assert errors[0]["type"] == "extra_forbidden" - assert errors[0]["loc"] == ('invalid_key',) + assert errors[0]["loc"] == ("invalid_key",) def test_node_targets_name(self): assert NodeParameters.targets_name == "qubits"