Skip to content

Commit e812f86

Browse files
committed
Fix LoadPolicy error handling + test.
1 parent 033d0a8 commit e812f86

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

lib/iris/_combine.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,16 +158,23 @@ def set(self, options: str | dict | None = None, **kwargs):
158158
"""
159159
if options is None:
160160
options_dict = {}
161-
elif isinstance(options, str) and options in self.SETTINGS:
162-
options_dict = self.SETTINGS[options]
161+
elif isinstance(options, str):
162+
if options in self.SETTINGS:
163+
options_dict = self.SETTINGS[options]
164+
else:
165+
msg = (
166+
f"arg 'options'={options!r}, which is not a valid settings name, "
167+
f"expected one of {self.SETTINGS_NAMES}."
168+
)
169+
raise ValueError(msg)
163170
elif isinstance(options, dict):
164171
options_dict = options
165172
else:
166-
msg = (
167-
f"arg `options` has unexpected type {type(options)!r}, "
168-
f"expected one of (None | str | dcit)."
169-
)
170-
raise TypeError(msg)
173+
msg = ( # type: ignore[unreachable]
174+
f"arg 'options' has unexpected type {type(options)!r}, "
175+
f"expected one of (None | str | dict)."
176+
) # type: ignore[unreachable]
177+
raise TypeError(msg) # type: ignore[unreachable]
171178

172179
# Override any options with keywords
173180
options_dict = options_dict.copy() # don't modify original

lib/iris/tests/unit/test_LoadPolicy.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_settings(self):
3636
options = LoadPolicy()
3737
settings = options.settings()
3838
assert isinstance(settings, dict)
39-
assert tuple(settings.keys()) == LoadPolicy.OPTION_KEYS
39+
assert list(settings.keys()) == LoadPolicy.OPTION_KEYS
4040
for key in LoadPolicy.OPTION_KEYS:
4141
assert settings[key] == getattr(options, key)
4242

@@ -74,13 +74,16 @@ def test_arg_bad_dict(self):
7474

7575
def test_arg_bad_string(self):
7676
options = LoadPolicy()
77-
expected = "Invalid arg options='unknown' : must be a dict, or one of"
78-
with pytest.raises(TypeError, match=expected):
79-
options.set("unknown")
77+
expected = (
78+
r"arg 'options'='oddthing'.*not a valid setting.*expected one of.* "
79+
"['legacy', 'default', 'recommended', 'comprehensive']"
80+
)
81+
with pytest.raises(ValueError, match=expected):
82+
options.set("oddthing")
8083

8184
def test_arg_bad_type(self):
8285
options = LoadPolicy()
83-
expected = "must be a dict, or one of"
86+
expected = r"arg 'options' has unexpected type \<class 'tuple'\>, expected one of \(None \| str \| dict\)\."
8487
with pytest.raises(TypeError, match=expected):
8588
options.set((1, 2, 3))
8689

0 commit comments

Comments
 (0)