Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if tel_id exists when looking up TelescopeParameters #2429

Merged
merged 1 commit into from
Oct 26, 2023
Merged
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
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.