Skip to content

Commit

Permalink
Helpful error message
Browse files Browse the repository at this point in the history
  • Loading branch information
ecomodeller committed Sep 3, 2024
1 parent 9b8b550 commit c41e59c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build: typecheck test
python -m build

lint:
ruff check .
ruff check $(LIB)

test:
pytest --disable-warnings
Expand Down
5 changes: 4 additions & 1 deletion modelskill/matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,10 @@ def match(
spatial_method=spatial_method,
)

assert isinstance(obs, Collection)
if isinstance(obs, Collection):
assert all(isinstance(o, get_args(ObsInputType)) for o in obs)
else:
raise TypeError(f"Obs is not the correct type: it is {type(obs)}. Check the order of the arguments (obs, mod).")

if len(obs) > 1 and isinstance(mod, Collection) and len(mod) > 1:
if not all(isinstance(m, (DfsuModelResult, GridModelResult)) for m in mod):
Expand Down
10 changes: 10 additions & 0 deletions tests/test_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,3 +542,13 @@ def test_compare_model_vs_dummy_for_track(mr1, o3):

# better than dummy 🙂
assert cmp2.score()["SW_1"] == pytest.approx(0.3524703)

def test_match_obs_model_pos_args_wrong_order_helpful_error_message():

# match is pretty helpful in converting strings or dataset
# so we need to use a ModelResult to trigger the error
mr = ms.PointModelResult(data=pd.Series([0.0,0.0], index=pd.date_range("1970", periods=2, freq='d')), name="Zero")
obs = ms.PointObservation(data=pd.Series([1.0, 2.0, 3.0], index=pd.date_range("1970", periods=3, freq='h')), name="MyStation")

with pytest.raises(TypeError, match="order"):
ms.match(mr, obs)

0 comments on commit c41e59c

Please sign in to comment.