diff --git a/ctapipe/core/telescope_component.py b/ctapipe/core/telescope_component.py index 2bac22e615e..6b49dae9227 100644 --- a/ctapipe/core/telescope_component.py +++ b/ctapipe/core/telescope_component.py @@ -217,8 +217,10 @@ def __getitem__(self, tel: Optional[Union[int, str]]): try: return self._value_for_tel_id[tel] except KeyError: + if tel not in self._subarray.tel: + raise KeyError(f"No telescope with id {tel} in subarray") raise KeyError( - f"TelescopeParameterLookup: no " + "TelescopeParameterLookup: no " f"parameter value was set for telescope with tel_id=" f"{tel}. Please set it explicitly, " f"or by telescope type or '*'." diff --git a/ctapipe/core/tests/test_telescope_component.py b/ctapipe/core/tests/test_telescope_component.py index 7aae1de47f9..e7f389953c2 100644 --- a/ctapipe/core/tests/test_telescope_component.py +++ b/ctapipe/core/tests/test_telescope_component.py @@ -342,3 +342,16 @@ class Foo(TelescopeComponent): f = Foo(mock_subarray, bar=[("type", "*", 1), ("id", 1, None)]) assert f.bar.tel[1] is None + + +def test_telescope_parameter_nonexistent_telescope(mock_subarray): + class Foo(TelescopeComponent): + bar = IntTelescopeParameter( + default_value=None, + allow_none=True, + ).tag(config=True) + + foo = Foo(subarray=mock_subarray) + + with pytest.raises(KeyError, match="No telescope with id 0"): + foo.bar.tel[0] diff --git a/docs/changes/2429.feature.rst b/docs/changes/2429.feature.rst new file mode 100644 index 00000000000..6dc4cec824d --- /dev/null +++ b/docs/changes/2429.feature.rst @@ -0,0 +1,3 @@ +In case no configuration is found for a telescope in ``TelescopeParameter``, +it is now checked whether the telescope exists at all to provide a better +error message.