From c1969b08b1f6dd6471e8fa61653815cbefed7908 Mon Sep 17 00:00:00 2001 From: Jaewon Yun Date: Wed, 2 Sep 2026 22:03:02 -0400 Subject: [PATCH] Report the offending input in the parametrized module error messages Error messages that only stated a rule now also report the value that broke it. Covers 5 messages: 3 in variable.py, where three of the four checks in Variable.__post_init__ did not say what was given, and 2 in paramobj.py, which now name the method that could not be serialized. The two errors raised for calls to parametrized objects are left unchanged: ParamObj.__call__ already warns at the call site and the warning carries the same object, so repeating it adds nothing. Existing assertions that stopped before the reported value are extended to the end of the message. --- pulser-core/pulser/parametrized/paramobj.py | 8 +++++--- pulser-core/pulser/parametrized/variable.py | 14 +++++++++++--- tests/test_abstract_repr.py | 5 ++++- tests/test_json.py | 5 ++++- tests/test_parametrized.py | 19 ++++++++++++++++--- 5 files changed, 40 insertions(+), 11 deletions(-) diff --git a/pulser-core/pulser/parametrized/paramobj.py b/pulser-core/pulser/parametrized/paramobj.py index 5050f5403..b26cc66b5 100644 --- a/pulser-core/pulser/parametrized/paramobj.py +++ b/pulser-core/pulser/parametrized/paramobj.py @@ -289,8 +289,9 @@ def class_to_dict(cls: Callable) -> dict[str, Any]: args[0] = class_to_dict(self.args[0]) else: raise NotImplementedError( - "Instance or static method " - "serialization is not supported." + "Instance or static method serialization is not " + "supported; got " + f"'{type(self.args[0]).__name__}.{self.cls.__name__}'." ) else: cls_dict = class_to_dict(self.cls) @@ -344,7 +345,8 @@ def _to_abstract_repr(self) -> dict[str, Any]: else: return abstract_repr(name, **all_args) raise NotImplementedError( - "Instance or static method serialization is not supported." + "Instance or static method serialization is not supported; " + f"got '{type(self.args[0]).__name__}.{op_name}'." ) elif op_name in SIGNATURES: signature = SIGNATURES[op_name] diff --git a/pulser-core/pulser/parametrized/variable.py b/pulser-core/pulser/parametrized/variable.py index f25164b3d..542d550ce 100644 --- a/pulser-core/pulser/parametrized/variable.py +++ b/pulser-core/pulser/parametrized/variable.py @@ -45,13 +45,21 @@ class Variable(Parametrized, OpSupport): def __post_init__(self) -> None: if not isinstance(self.name, str): - raise TypeError("Variable's 'name' has to be of type 'str'.") + raise TypeError( + "Variable's 'name' has to be of type 'str', not " + f"{type(self.name)}." + ) if self.dtype not in [int, float]: raise TypeError(f"Invalid data type '{self.dtype}' for Variable.") if not isinstance(self.size, int): - raise TypeError("Given variable 'size' is not of type 'int'.") + raise TypeError( + "Variable's 'size' has to be of type 'int', not " + f"{type(self.size)}." + ) elif self.size < 1: - raise ValueError("Variables must be of size 1 or larger.") + raise ValueError( + f"Variables must be of size 1 or larger; got {self.size}." + ) self._count: int object.__setattr__(self, "_count", -1) # Counts the updates diff --git a/tests/test_abstract_repr.py b/tests/test_abstract_repr.py index 3b4fe574c..91d837331 100644 --- a/tests/test_abstract_repr.py +++ b/tests/test_abstract_repr.py @@ -1189,7 +1189,10 @@ def test_paramobj_serialization(self, sequence): method_call = parametrize(BlackmanWaveform.with_new_duration)(wf, var) with pytest.raises( NotImplementedError, - match="Instance or static method serialization is not supported.", + match=re.escape( + "Instance or static method serialization is not supported; " + "got 'BlackmanWaveform.with_new_duration'." + ), ): method_call._to_abstract_repr() diff --git a/tests/test_json.py b/tests/test_json.py index fc98e7fa5..c3e70070e 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -218,7 +218,10 @@ def test_rare_cases(patch_plt_show, helpers): rotated_reg = parametrize(Register.rotated)(reg, var) with pytest.raises( NotImplementedError, - match="Instance or static method serialization is not supported.", + match=re.escape( + "Instance or static method serialization is not supported; " + "got 'Register.rotated'." + ), ): encode(rotated_reg) diff --git a/tests/test_parametrized.py b/tests/test_parametrized.py index 6ac2549c7..a20b078d5 100644 --- a/tests/test_parametrized.py +++ b/tests/test_parametrized.py @@ -56,13 +56,26 @@ def bwf(t, a): def test_var(a, b): - with pytest.raises(TypeError, match="'name' has to be of type 'str'"): + with pytest.raises( + TypeError, + match=re.escape( + "Variable's 'name' has to be of type 'str', not ." + ), + ): Variable(1, dtype=int) with pytest.raises(TypeError, match="Invalid data type"): Variable("x", dtype=list, size=4) - with pytest.raises(TypeError, match="'size' is not of type 'int'"): + with pytest.raises( + TypeError, + match=re.escape( + "Variable's 'size' has to be of type 'int', not ." + ), + ): Variable("x", dtype=float, size=(2, 2)) - with pytest.raises(ValueError, match="size 1 or larger"): + with pytest.raises( + ValueError, + match=re.escape("Variables must be of size 1 or larger; got 0."), + ): Variable("x", dtype=int, size=0) x = Variable("x", dtype=float) assert x.value is None