Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
85ee034
moving _assert_valid_python_varname() from particle.py to utils/strin…
nilodna Oct 7, 2025
87c05c5
including _assert_valid_python_varname() on VectorField, Field and Fi…
nilodna Oct 7, 2025
119cc39
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 7, 2025
a629e3e
Merge remote-tracking branch 'upstream/v4-dev' into pr/nilodna/2312
VeckoTheGecko Oct 8, 2025
ed3548d
Refactoring function name to unify the string check, isidentifier and…
nilodna Oct 9, 2025
8cd16e8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 9, 2025
2c98132
cleaning old function
nilodna Oct 9, 2025
3e4e51d
solving conflicts
nilodna Oct 9, 2025
7a1cac0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 9, 2025
5016e4d
Merge branch 'v4-dev' into assert_variables_names
VeckoTheGecko Oct 10, 2025
88d585a
Refactor error message handling in _assert_str_and_python_varname for…
nilodna Oct 16, 2025
f0b560b
Creating tests for add_constant with invalid names and types
nilodna Oct 16, 2025
e74b84f
Improve name type validation in test_field_init_param_types for Field…
nilodna Oct 16, 2025
e877b1f
Quickfix to refactor field name (e.g, from invalid names like 'U (A g…
nilodna Oct 16, 2025
3eeca09
Improve name type validation in test_variable_invalid_init for Variab…
nilodna Oct 16, 2025
41a7cbd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 16, 2025
3e7a3a7
Merge branch 'v4-dev' into assert_variables_names
VeckoTheGecko Oct 24, 2025
afbfabe
Update field names
VeckoTheGecko Oct 24, 2025
c402fd4
Update error message checking
VeckoTheGecko Oct 24, 2025
dd26c1a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 24, 2025
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
9 changes: 9 additions & 0 deletions parcels/_core/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
AllParcelsErrorCodes,
StatusCode,
)
from parcels._core.utils.string import _assert_valid_python_varname
from parcels._core.utils.time import TimeInterval
from parcels._core.uxgrid import UxGrid
from parcels._core.xgrid import XGrid, _transpose_xfield_data_to_tzyx
Expand Down Expand Up @@ -103,6 +104,9 @@ def __init__(
)
if not isinstance(name, str):
raise ValueError(f"Expected `name` to be a string, got {type(name)}.")
else:
_assert_valid_python_varname(name)

if not isinstance(grid, (UxGrid, XGrid)):
raise ValueError(f"Expected `grid` to be a parcels UxGrid, or parcels XGrid object, got {type(grid)}.")

Expand Down Expand Up @@ -246,6 +250,11 @@ class VectorField:
def __init__(
self, name: str, U: Field, V: Field, W: Field | None = None, vector_interp_method: Callable | None = None
):
if not isinstance(name, str):
raise ValueError(f"Expected `name` to be a string, got {type(name)}.")
else:
_assert_valid_python_varname(name)

self.name = name
self.U = U
self.V = V
Expand Down
6 changes: 6 additions & 0 deletions parcels/_core/fieldset.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from parcels._core.converters import Geographic, GeographicPolar
from parcels._core.field import Field, VectorField
from parcels._core.utils.string import _assert_valid_python_varname
from parcels._core.utils.time import get_datetime_type_calendar
from parcels._core.utils.time import is_compatible as datetime_is_compatible
from parcels._core.xgrid import _DEFAULT_XGCM_KWARGS, XGrid
Expand Down Expand Up @@ -163,6 +164,11 @@ def add_constant(self, name, value):
`Diffusion <../examples/tutorial_diffusion.ipynb>`__
`Periodic boundaries <../examples/tutorial_periodic_boundaries.ipynb>`__
"""
if not isinstance(name, str):
raise ValueError(f"Expected `name` to be a string, got {type(name)}.")
else:
_assert_valid_python_varname(name)

if name in self.constants:
raise ValueError(f"FieldSet already has a constant with name '{name}'")
if not isinstance(value, (float, np.floating, int, np.integer)):
Expand Down
13 changes: 4 additions & 9 deletions parcels/_core/particle.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import enum
import operator
from keyword import iskeyword
from typing import Literal

import numpy as np

from parcels._compat import _attrgetter_helper
from parcels._core.statuscodes import StatusCode
from parcels._core.utils.string import _assert_valid_python_varname
from parcels._core.utils.time import TimeInterval
from parcels._reprs import _format_list_items_multiline

Expand Down Expand Up @@ -46,8 +46,9 @@ def __init__(
attrs: dict | None = None,
):
if not isinstance(name, str):
raise TypeError(f"Variable name must be a string. Got {name=!r}")
_assert_valid_python_varname(name)
raise ValueError(f"Expected `name` to be a string, got {type(name)}.")
else:
_assert_valid_python_varname(name)

try:
dtype = np.dtype(dtype)
Expand Down Expand Up @@ -153,12 +154,6 @@ def _assert_no_duplicate_variable_names(*, existing_vars: list[Variable], new_va
raise ValueError(f"Variable name already exists: {var.name}")


def _assert_valid_python_varname(name):
if name.isidentifier() and not iskeyword(name):
return
raise ValueError(f"Particle variable has to be a valid Python variable name. Got {name=!r}")


def get_default_particle(spatial_dtype: np.float32 | np.float64) -> ParticleClass:
if spatial_dtype not in [np.float32, np.float64]:
raise ValueError(f"spatial_dtype must be np.float32 or np.float64. Got {spatial_dtype=!r}")
Expand Down
9 changes: 9 additions & 0 deletions parcels/_core/utils/string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from keyword import iskeyword, kwlist


def _assert_valid_python_varname(name):
if name.isidentifier() and not iskeyword(name):
return
raise ValueError(
f"Received invalid Python variable name {name!r}. Avoid using the following names: {', '.join(kwlist)}"
)
Loading