Skip to content

Commit

Permalink
✅ Enforce behavior of private attributes having double leading unders…
Browse files Browse the repository at this point in the history
…core (#7265)
  • Loading branch information
lig committed Sep 11, 2023
1 parent 9c03680 commit 132a90c
Showing 1 changed file with 23 additions and 0 deletions.
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

0 comments on commit 132a90c

Please sign in to comment.