Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Marks private attributes as read-only with frozen=True #964

Open
chbndrhnns opened this issue Jul 24, 2024 · 0 comments
Open

Marks private attributes as read-only with frozen=True #964

chbndrhnns opened this issue Jul 24, 2024 · 0 comments

Comments

@chbndrhnns
Copy link

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.

Screenshots
Screenshot 2024-07-24 at 13 50 44

Environments (please complete the following information):

  • IDE: Pycharm 2024.2 EAP 6
  • OS: [e.g. macOS 12.2.0 ]: macOS
  • Pydantic Version [e.g. 1.9.0 ]: 2.8.2
  • Plugin version [e.g. 0.3.11 ]: 0.4.14

Additional context
Add any other context about the problem here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant