Skip to content
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
19 changes: 11 additions & 8 deletions src/parcels/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,27 @@
KernelFunction = Callable[..., None]


def _is_xarray_object(obj): # with no imports
try:
return "xarray.core" in obj.__module__
except AttributeError:
return False
Comment on lines +42 to +46
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here we avoid importing xarray (otherwise we risk circular imports)



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.
"""
# 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):
msg = f"Invalid value {value!r}. Valid options are {get_args(typing_literal)!r}"
raise ValueError(msg)


# Assertion functions to clean user input
Copy link
Contributor Author

@VeckoTheGecko VeckoTheGecko Mar 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert_valid_interp_method and assert_valid_gridindexingtype unused in v4

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)
31 changes: 0 additions & 31 deletions tests-v3/test_typing.py

This file was deleted.

20 changes: 20 additions & 0 deletions tests/test_typing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import numpy as np
import pytest
import xarray as xr

from parcels._typing import (
assert_valid_mesh,
)


def test_invalid_assert_valid_mesh():
with pytest.raises(ValueError, match="Invalid value"):
assert_valid_mesh("invalid option")

ds = xr.Dataset({"A": (("a", "b"), np.arange(20).reshape(4, 5))})
with pytest.raises(ValueError, match="Invalid input type"):
assert_valid_mesh(ds)


def test_assert_valid_mesh():
assert_valid_mesh("spherical")
Loading