Skip to content

Commit

Permalink
Better error message for edc.AutoCast assignement
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 581914801
  • Loading branch information
Conchylicultor authored and The etils Authors committed Nov 14, 2023
1 parent 5341645 commit ae9d4a2
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion etils/edc/field_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import typing
from typing import Any, Callable, Generic, Optional, Type, TypeVar

from etils import epy

_Dataclass = Any
_In = Any
_Out = Any
Expand Down Expand Up @@ -69,7 +71,7 @@ def __init__(
self._attribute_name: Optional[str] = None
self._objtype: Optional[Type[_Dataclass]] = None

self._validate = validate
self._validate_fn = validate
self._field_kwargs = field_kwargs

# Whether `__get__` has not been called yet. See `__get__` for details.
Expand Down Expand Up @@ -115,6 +117,12 @@ def __set__(self, obj: _Dataclass, value: _InT) -> None:
# Validate the value during assignement
_setattr(obj, self._attribute_name, self._validate(value))

def _validate(self, value: _InT) -> _OutT:
try:
return self._validate_fn(value)
except Exception as e: # pylint: disable=broad-exception-caught
epy.reraise(e, prefix=f'Error assigning {self._attribute_name!r}: ')


# Because there is one instance of the `_Field` per class, shared across all
# class instances, we need to store the per-object state somewhere.
Expand Down

0 comments on commit ae9d4a2

Please sign in to comment.