You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello,
I have defined an abstract base class inherited from BaseSettings and I would like to validate that all fields in subclasses of my base class are restricted to being int, float, str, or bool. What is the best way to implement this? I have the following, but it seems like there is probably a built-in way of doing this that I just haven't realized. Help would be appreciated!
class BaseTaskParameters(BaseSettings, ABC):
"""Subclass this for key-value pair config sections."""
@model_validator(mode="before")
@classmethod
def validate_serializable_types(cls, v):
for field_name, field_info in cls.model_fields.items():
if not field_info.annotation in [str, float, int, bool]:
raise TypeError(f"{field_name} must be of type str, float, int, or bool")
return v
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello,
I have defined an abstract base class inherited from
BaseSettings
and I would like to validate that all fields in subclasses of my base class are restricted to beingint
,float
,str
, orbool
. What is the best way to implement this? I have the following, but it seems like there is probably a built-in way of doing this that I just haven't realized. Help would be appreciated!Beta Was this translation helpful? Give feedback.
All reactions