I want to make a attribute private but with a pydantic field:
from pydantic import BaseModel, Field, PrivateAttr, validator
class A(BaseModel):
_a: str = "" # I want a pydantic field for this private value.
_computed_from_a: str = PrivateAttr(default="")
@property
def a(self):
return self._a
@a.setter
def a(self,v):
self._a = v
self._computed_from_a = "b" + self._a
# This returns a type Field<...> which crashes...
assert isinstance(A().a, str)
I think I cannot name a field _
with underscore because pydantic does black magic underneath. How can I achieve a private field _a
which has defined setters and getters, where the getter computes some other value _computed_from_a
.