From 0c8d1dc2dcc19c1be8781d9c29223cc6016febde Mon Sep 17 00:00:00 2001 From: BM804 <112770884+BM804@users.noreply.github.com> Date: Fri, 16 Oct 2026 22:36:48 +0530 Subject: [PATCH] Fix missing magnet model error recursion --- pyaml/magnet/cfm_magnet.py | 4 ++-- pyaml/magnet/magnet.py | 4 ++-- pyaml/magnet/serialized_magnet.py | 4 ++-- tests/magnet/test_missing_model_errors.py | 15 +++++++++++++++ 4 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 tests/magnet/test_missing_model_errors.py diff --git a/pyaml/magnet/cfm_magnet.py b/pyaml/magnet/cfm_magnet.py index 83c5578f..47140100 100644 --- a/pyaml/magnet/cfm_magnet.py +++ b/pyaml/magnet/cfm_magnet.py @@ -239,7 +239,7 @@ def strengths(self) -> abstract.ReadWriteFloatScalar: """ self.check_peer() if self.__strengths is None: - raise PyAMLException(f"{str(self)} has no model that supports physics units") + raise PyAMLException(f"{self.name} has no model that supports physics units") return self.__strengths @property @@ -250,7 +250,7 @@ def hardwares(self) -> abstract.ReadWriteFloatScalar: """ self.check_peer() if self.__hardwares is None: - raise PyAMLException(f"{str(self)} has no model that supports hardware units") + raise PyAMLException(f"{self.name} has no model that supports hardware units") return self.__hardwares def set_energy(self, E: float): diff --git a/pyaml/magnet/magnet.py b/pyaml/magnet/magnet.py index b01491d7..fd5a4a09 100644 --- a/pyaml/magnet/magnet.py +++ b/pyaml/magnet/magnet.py @@ -83,7 +83,7 @@ def strength(self) -> abstract.ReadWriteFloatScalar: """ self.check_peer() if self.__strength is None: - raise PyAMLException(f"{str(self)} has no model that supports physics units") + raise PyAMLException(f"{self.name} has no model that supports physics units") return self.__strength @property @@ -94,7 +94,7 @@ def hardware(self) -> abstract.ReadWriteFloatScalar: """ self.check_peer() if self.__hardware is None: - raise PyAMLException(f"{str(self)} has no model that supports hardware units") + raise PyAMLException(f"{self.name} has no model that supports hardware units") return self.__hardware @property diff --git a/pyaml/magnet/serialized_magnet.py b/pyaml/magnet/serialized_magnet.py index ddfe1733..c5aab6fa 100644 --- a/pyaml/magnet/serialized_magnet.py +++ b/pyaml/magnet/serialized_magnet.py @@ -355,7 +355,7 @@ def strength(self) -> abstract.ReadWriteFloatScalar: """ self.check_peer() if self.__strengths is None: - raise PyAMLException(f"{str(self)} has no model that supports physics units") + raise PyAMLException(f"{self.name} has no model that supports physics units") return self.__strengths @property @@ -365,7 +365,7 @@ def hardware(self) -> abstract.ReadWriteFloatScalar: """ self.check_peer() if self.__hardwares is None: - raise PyAMLException(f"{str(self)} has no model that supports hardware units") + raise PyAMLException(f"{self.name} has no model that supports hardware units") return self.__hardwares def set_energy(self, energy: float): diff --git a/tests/magnet/test_missing_model_errors.py b/tests/magnet/test_missing_model_errors.py new file mode 100644 index 00000000..37c25b57 --- /dev/null +++ b/tests/magnet/test_missing_model_errors.py @@ -0,0 +1,15 @@ +import pytest + +from pyaml import PyAMLException +from pyaml.magnet.hcorrector import HCorrector + + +@pytest.mark.parametrize( + ("accessor", "unit"), + [("strength", "physics"), ("hardware", "hardware")], +) +def test_missing_model_error_names_magnet_without_repr_recursion(accessor, unit): + magnet = HCorrector("CH1").attach(object(), strength=None, hardware=None) + + with pytest.raises(PyAMLException, match=f"CH1 has no model that supports {unit} units"): + getattr(magnet, accessor)