Skip to content
Open
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
8 changes: 5 additions & 3 deletions pulser-core/pulser/parametrized/paramobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand Down
14 changes: 11 additions & 3 deletions pulser-core/pulser/parametrized/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}."
Comment on lines +49 to +50

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe is it worth returning the input that failed ?

Suggested change
"Variable's 'name' has to be of type 'str', not "
f"{type(self.name)}."
"Variable's 'name' has to be of type 'str', not "
f"{type(self.name)} (got {self.name!r})."

)
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)}."
Comment on lines +56 to +57

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here as well

Suggested change
"Variable's 'size' has to be of type 'int', not "
f"{type(self.size)}."
"Variable's 'size' has to be of type 'int', not "
f"{type(self.size)} (got {self.size!r})."

)
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
Expand Down
5 changes: 4 additions & 1 deletion tests/test_abstract_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
5 changes: 4 additions & 1 deletion tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
19 changes: 16 additions & 3 deletions tests/test_parametrized.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <class 'int'>."
),
):
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 <class 'tuple'>."
),
):
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
Expand Down
Loading