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
When create a model instance, computed_field is not actually computed. It's deferred to later when the property of that instance is actually requested.
In my instance, I need to build a rather complicated model, and need to do a lot of field validation. The only input to the model will be a byte array, and all properties of the model needs to be computed using the computed_field decorator.
Here is a overly simplistic example of the problem:
from pydantic import BaseModel, computed_field
from enum import Enum
class MySuperSpecificException(Exception):
"""We have to handle this in special ways"""
def __init__(self, response_code):
super().__init__(f"Non-zero HSM reponse received: {response_code}")
class RepsonseCode(Enum):
NO_ERROR = "00"
VERIFICATION_FAILURE = "01"
class Thing(BaseModel):
raw_bytes: bytes
@computed_field
@property
def message_size(self) -> int:
hex_value = self.raw_bytes[0:2].hex()
return int(hex_value, 16)
@computed_field
@property
def full_message(self) -> str:
return self.raw_bytes[2:].decode()
@computed_field
@property
def response_code(self) -> RepsonseCode:
_response_code = RepsonseCode(self.full_message[0:2])
if _response_code != RepsonseCode.NO_ERROR:
raise MySuperSpecificException(response_code=_response_code)
else:
return _response_code
def main():
data: bytes = b"\x00/00AB12"
thing1: Thing = Thing(raw_bytes=data)
data: bytes = b"\x00/01AB12"
thing2: Thing = Thing(raw_bytes=data) # no exception was raised here
print(f"Only here, when working with the model, do we get the exception: {thing2}")
if __name__ == "__main__":
main()
Is there a way to force pydantic, to compute the computed_field fields upon instantiation?
This isn't an expensive operation, so we don't mind doing it as often as required, but need to catch bad data early in our pipeline.
I tried creating a field_validator for the property as well, that didn't seem to work either.
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
-
When create a model instance, computed_field is not actually computed. It's deferred to later when the property of that instance is actually requested.
In my instance, I need to build a rather complicated model, and need to do a lot of field validation. The only input to the model will be a byte array, and all properties of the model needs to be computed using the computed_field decorator.
Here is a overly simplistic example of the problem:
Is there a way to force pydantic, to compute the computed_field fields upon instantiation?
This isn't an expensive operation, so we don't mind doing it as often as required, but need to catch bad data early in our pipeline.
I tried creating a field_validator for the property as well, that didn't seem to work either.
Beta Was this translation helpful? Give feedback.
All reactions