Skip to content

Commit e31a2f7

Browse files
committed
Test interpolation function checking (and remove checking of type annotations)
Dynamically checking of type annotations like this is flaky due to how Python internally handles type annotations
1 parent 5cd8f84 commit e31a2f7

2 files changed

Lines changed: 37 additions & 25 deletions

File tree

parcels/field.py

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -124,19 +124,11 @@ def _validate_interp_function(self, func: Callable) -> None:
124124
raise ValueError(
125125
f"Parameter '{_name2}' has incorrect parameter kind. Expected {param1.kind}, got {param2.kind}"
126126
)
127-
if param1.annotation != param2.annotation:
127+
if param1.name != param2.name:
128128
raise ValueError(
129-
f"Parameter '{_name2}' has incorrect type annotation. Expected {param1.annotation}, got {param2.annotation}"
129+
f"Parameter '{_name2}' has incorrect name. Expected '{param1.name}', got '{param2.name}'"
130130
)
131131

132-
return_annotation = func_sig.return_annotation
133-
template_return = template_sig.return_annotation
134-
135-
if return_annotation != template_return:
136-
raise ValueError(
137-
f"Interpolation function has incorrect return type. Expected {template_return}, got {return_annotation}"
138-
)
139-
140132
def __init__(
141133
self,
142134
name: str,
@@ -345,20 +337,6 @@ def __contains__(self, key: str):
345337
class VectorField:
346338
"""VectorField class that holds vector field data needed to execute particles."""
347339

348-
@staticmethod
349-
def _vector_interp_template(
350-
self,
351-
ti: int,
352-
position: dict[str, tuple[int, float | np.ndarray]],
353-
tau: np.float32 | np.float64,
354-
t: np.float32 | np.float64,
355-
z: np.float32 | np.float64,
356-
y: np.float32 | np.float64,
357-
x: np.float32 | np.float64,
358-
) -> np.float32 | np.float64:
359-
"""Template function used for the signature check of the lateral interpolation methods."""
360-
return 0.0
361-
362340
def _validate_vector_interp_function(self, func: Callable):
363341
"""Ensures that the function has the correct signature."""
364342
expected_params = ["ti", "ei", "bcoords", "t", "z", "y", "x"]

tests/v4/test_field.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
from __future__ import annotations
2+
13
import numpy as np
24
import pytest
35
import uxarray as ux
46
import xarray as xr
57

6-
from parcels import Field, UXPiecewiseConstantFace, UXPiecewiseLinearNode, xgcm
8+
from parcels import Field, UXPiecewiseConstantFace, UXPiecewiseLinearNode, VectorField, xgcm
79
from parcels._datasets.structured.generic import T as T_structured
810
from parcels._datasets.structured.generic import datasets as datasets_structured
911
from parcels._datasets.unstructured.generic import datasets as datasets_unstructured
@@ -115,6 +117,38 @@ def test_vectorfield_init_different_time_intervals():
115117
...
116118

117119

120+
def test_field_invalid_interpolator():
121+
"""Test that Field initialization fails with invalid interpolation methods."""
122+
ds = datasets_structured["ds_2d_left"]
123+
grid = XGrid(xgcm.Grid(ds))
124+
125+
def invalid_interpolator_wrong_signature(self, ti, position, tau, t, z, y, invalid):
126+
return 0.0
127+
128+
# Test invalid interpolator with wrong signature
129+
with pytest.raises(ValueError, match=".*incorrect name.*"):
130+
Field(name="test", data=ds["data_g"], grid=grid, interp_method=invalid_interpolator_wrong_signature)
131+
132+
133+
@pytest.mark.xfail
134+
def test_vectorfield_invalid_interpolator():
135+
"""Test that VectorField initialization fails with invalid interpolation methods."""
136+
ds = datasets_structured["ds_2d_left"]
137+
grid = XGrid(xgcm.Grid(ds))
138+
139+
def invalid_interpolator_wrong_signature(self):
140+
# Missing required parameters from _interp_template signature
141+
return 0.0
142+
143+
# Create component fields
144+
U = Field(name="U", data=ds["data_g"], grid=grid)
145+
V = Field(name="V", data=ds["data_g"], grid=grid)
146+
147+
# Test invalid interpolator with wrong signature
148+
with pytest.raises(ValueError, match=".*incorrect name.*"):
149+
VectorField(name="UV", U=U, V=V, vector_interp_method=invalid_interpolator_wrong_signature)
150+
151+
118152
def test_field_unstructured_z_linear():
119153
"""Tests correctness of piecewise constant and piecewise linear interpolation methods on an unstructured grid with a vertical coordinate.
120154
The example dataset is a FESOM2 square Delaunay grid with uniform z-coordinate. Cell centered and layer registered data are defined to be

0 commit comments

Comments
 (0)