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
We'd like to configure public (no leading _) variables using pydantic with validate_assignment but still would like private variables _ to be possible, e.g. for logging:
importpydanticimportloggingclassModelWithLog(pydantic.BaseModel):
option: int=10classConfig:
validate_assignment=Truedef__init__(self, **kwargs):
super().__init__(**kwargs)
self._log=logging.getLogger(self.__class__.__name__) # This failsm=ModelWithLog()
m.option='foo'# this should failm.optoin=10# typo, this should fail as well
Result:
Traceback (most recent call last):
File "model_log.py", line 15, in <module>
m = ModelWithLog()
File "model_log.py", line 12, in __init__
self._log = logging.getLogger(self.__class__.__name__) # This fails
File "pydantic/main.py", line 281, in pydantic.main.BaseModel.__setattr__
ValueError: "ModelWithLog" object has no field "_log"```
The text was updated successfully, but these errors were encountered:
Feature Request
We'd like to configure public (no leading
_
) variables using pydantic withvalidate_assignment
but still would like private variables_
to be possible, e.g. for logging:Result:
The text was updated successfully, but these errors were encountered: