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

✅ Enforce behavior of private attributes having double leading underscore #7265

Merged
merged 1 commit into from Sep 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions tests/test_private_attributes.py
Expand Up @@ -27,6 +27,29 @@ class Model(BaseModel):
assert m.__dict__ == {}


def test_private_attribute_double_leading_underscore():
default = {'a': {}}

class Model(BaseModel):
__foo = PrivateAttr(default)

assert set(Model.__private_attributes__) == {'_Model__foo'}

m = Model()

with pytest.raises(AttributeError, match='__foo'):
m.__foo
assert m._Model__foo == default
assert m._Model__foo is not default
assert m._Model__foo['a'] is not default['a']

m._Model__foo = None
assert m._Model__foo is None

assert m.model_dump() == {}
assert m.__dict__ == {}


def test_private_attribute_nested():
class SubModel(BaseModel):
_foo = PrivateAttr(42)
Expand Down