Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forbid extra args to runnable parameters #83

Merged
merged 4 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion qualibrate/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion qualibrate/qualibration_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/test_parameters/test_node_and_graph_parameters.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Optional

import pytest
from pydantic import ValidationError

from qualibrate.parameters import GraphParameters, NodeParameters

Expand All @@ -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"

Expand Down