From 921eaf86dccb1cc6014691063f1f89403655f4e5 Mon Sep 17 00:00:00 2001 From: Vecko <36369090+VeckoTheGecko@users.noreply.github.com> Date: Mon, 2 Mar 2026 16:40:05 +0100 Subject: [PATCH 1/3] Move file --- {tests-v3 => tests}/test_typing.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {tests-v3 => tests}/test_typing.py (100%) diff --git a/tests-v3/test_typing.py b/tests/test_typing.py similarity index 100% rename from tests-v3/test_typing.py rename to tests/test_typing.py From f907ef21e8611ebca0ef17c956171f221d29ce23 Mon Sep 17 00:00:00 2001 From: Vecko <36369090+VeckoTheGecko@users.noreply.github.com> Date: Mon, 2 Mar 2026 16:58:10 +0100 Subject: [PATCH 2/3] Update _validate_against_pure_literal Also cleaned out the test_typing.py and _typing.py modules --- src/parcels/_typing.py | 20 ++++++++++++-------- tests/test_typing.py | 31 ++++++++++--------------------- 2 files changed, 22 insertions(+), 29 deletions(-) diff --git a/src/parcels/_typing.py b/src/parcels/_typing.py index 7c2e12b8fd..8558cbb5df 100644 --- a/src/parcels/_typing.py +++ b/src/parcels/_typing.py @@ -39,24 +39,28 @@ KernelFunction = Callable[..., None] +def _is_xarray_object(obj): # with no imports + try: + return "xarray.core" in obj.__module__ + except AttributeError: + return False + + def _validate_against_pure_literal(value, typing_literal): """Uses a Literal type alias to validate. Can't be used with ``Literal[...] | None`` etc. as its not a pure literal. """ + if _is_xarray_object( + value + ): # Xarray objects don't work normally in `in` statements due to https://github.com/pydata/xarray/issues/11209 + raise ValueError(f"Invalid input type {type(value)}") + if value not in get_args(typing_literal): msg = f"Invalid value {value!r}. Valid options are {get_args(typing_literal)!r}" raise ValueError(msg) # Assertion functions to clean user input -def assert_valid_interp_method(value: Any): - _validate_against_pure_literal(value, InterpMethodOption) - - def assert_valid_mesh(value: Any): _validate_against_pure_literal(value, Mesh) - - -def assert_valid_gridindexingtype(value: Any): - _validate_against_pure_literal(value, GridIndexingType) diff --git a/tests/test_typing.py b/tests/test_typing.py index f4efba3efd..4f1d3e0c56 100644 --- a/tests/test_typing.py +++ b/tests/test_typing.py @@ -1,31 +1,20 @@ +import numpy as np import pytest +import xarray as xr from parcels._typing import ( - assert_valid_gridindexingtype, - assert_valid_interp_method, assert_valid_mesh, ) -validators = ( - assert_valid_interp_method, - assert_valid_mesh, - assert_valid_gridindexingtype, -) - - -@pytest.mark.parametrize("validator", validators) -def test_invalid_option(validator): - with pytest.raises(ValueError): - validator("invalid option") +def test_invalid_assert_valid_mesh(): + with pytest.raises(ValueError, match="Invalid value"): + assert_valid_mesh("invalid option") -validation_mapping = [ - (assert_valid_interp_method, "nearest"), - (assert_valid_mesh, "spherical"), - (assert_valid_gridindexingtype, "pop"), -] + ds = xr.Dataset({"A": (("a", "b"), np.arange(20).reshape(4, 5))}) + with pytest.raises(ValueError, match="Invalid input type"): + assert_valid_mesh(ds) -@pytest.mark.parametrize("validator, value", validation_mapping) -def test_valid_option(validator, value): - validator(value) +def test_assert_valid_mesh(): + assert_valid_mesh("spherical") From 34b4b0960c04cffd734bd2be53bb18f104980a1d Mon Sep 17 00:00:00 2001 From: Vecko <36369090+VeckoTheGecko@users.noreply.github.com> Date: Mon, 2 Mar 2026 17:42:59 +0100 Subject: [PATCH 3/3] Add TODO --- src/parcels/_typing.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/parcels/_typing.py b/src/parcels/_typing.py index 8558cbb5df..3d44281209 100644 --- a/src/parcels/_typing.py +++ b/src/parcels/_typing.py @@ -51,9 +51,8 @@ def _validate_against_pure_literal(value, typing_literal): Can't be used with ``Literal[...] | None`` etc. as its not a pure literal. """ - if _is_xarray_object( - value - ): # Xarray objects don't work normally in `in` statements due to https://github.com/pydata/xarray/issues/11209 + # TODO remove once https://github.com/pydata/xarray/issues/11209 is resolved - Xarray objects don't work normally in `in` statements + if _is_xarray_object(value): raise ValueError(f"Invalid input type {type(value)}") if value not in get_args(typing_literal):