Skip to content
Open
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
20 changes: 17 additions & 3 deletions lume/variables/ndvariable.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@

import numpy as np
from numpy.typing import NDArray
from pydantic import ConfigDict, field_serializer, field_validator, model_validator
from pydantic import (
ConfigDict,
ValidationInfo,
field_serializer,
field_validator,
model_validator,
)

from lume.variables.variable import ConfigEnum, Variable

Expand Down Expand Up @@ -136,19 +142,27 @@ def serialize_default_value(self, value: Optional[NDArray]) -> Optional[List]:

@field_validator("default_value", mode="before")
@classmethod
def coerce_default_value(cls, value: Any) -> Any:
def coerce_default_value(cls, value: Any, info: ValidationInfo) -> Any:
"""Coerce list or tuple input to np.ndarray for round-trip deserialization.

When a model is reconstructed from a serialized dict (e.g. loaded
from JSON or YAML), default_value arrives as a nested list.
This validator converts it back to a NumPy array so that the model
invariants are maintained.

The array is built using the model's own ``dtype`` (already validated,
since ``dtype`` is declared before ``default_value``). Without this the
list would default to ``float64``/``int64`` and fail the exact-dtype
check in ``validate_default_value`` for any other dtype.

Parameters
----------
value : Any
Raw input value. If it is a list or tuple it is
converted to np.ndarray; otherwise it is returned unchanged.
info : ValidationInfo
Pydantic validation context, used to read the already-validated
``dtype`` field.

Returns
-------
Expand All @@ -157,7 +171,7 @@ def coerce_default_value(cls, value: Any) -> Any:

"""
if isinstance(value, (list, tuple)):
return np.asarray(value)
return np.asarray(value, dtype=info.data.get("dtype"))
return value

@field_validator("dtype", mode="before")
Expand Down
Loading