Skip to content

Commit

Permalink
Merge pull request #2429 from cta-observatory/fix_2233_telescope_para…
Browse files Browse the repository at this point in the history
…meter_nonexistent_telescope

Check if tel_id exists when looking up TelescopeParameters
  • Loading branch information
maxnoe authored Oct 26, 2023
2 parents 273f642 + eba9cf9 commit 6817199
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion ctapipe/core/telescope_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 '*'."
Expand Down
13 changes: 13 additions & 0 deletions ctapipe/core/tests/test_telescope_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
3 changes: 3 additions & 0 deletions docs/changes/2429.feature.rst
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 6817199

Please sign in to comment.