I'm using pydantic with fastapi. And, I make Model like this.
# model.py
from multiprocessing import RLock
from pydantic import BaseModel
class ModelA(BaseModel):
file_1: str = 'test'
def __init__(self, **data: Any):
super().__init__(**data)
self._lock = RLock()
self._instance_variable: int = 1
@property
def lock(self):
return self._lock
@property
def instance_variable(self) -> int:
with self.lock:
return self._instance_variable
@instance_variable.setter
def instance_variable(self, v: int) -> int:
assert isinstance(v, int)
with self.lock:
self._instance_variable = v
And I make test like this
# test_model_a.py
def test_model_a():
instance = ModelA()
assert instance.json() == '{"field_1": "test"}'
After, I run the test, but the instance can't create with this error.
E ValueError: "ModelA" object has no field "_lock"
So, How can I pass this test...? Please... help me...
BaseModel.dict()instead and compare 2 objects.