Describe the bug
PyCharm marks private fields as read-only although pydantic docs say that private attributes are not validated.
Attributes whose name has a leading underscore are not treated as fields by Pydantic, and are not included in the model schema. Instead, these are converted into a "private attribute" which is not validated or even set during calls to init, model_validate, etc.
I am thinking a warning might be useful when trying to modify a frozen model but the red underline might be too much.
To Reproduce
from datetime import datetime
from random import randint
from pydantic import BaseModel, PrivateAttr, ConfigDict
class TimeAwareModel(BaseModel):
_processed_at: datetime = PrivateAttr(default_factory=datetime.now)
_secret_value: int
model_config = ConfigDict(frozen=True)
@property
def value(self):
return self._secret_value
def __init__(self, **data):
super().__init__(**data)
self._secret_value = randint(1, 5)
def test_():
assert isinstance(TimeAwareModel().value, int)
Expected behavior
Private attributes are excluded from such inspection.
Environments (please complete the following information):
Additional context
Add any other context about the problem here.
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too