Skip to content

Commit c4618a8

Browse files
committed
fix(lockeddict): block dot notation
1 parent 1527bd1 commit c4618a8

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

simulation/lockeddict.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ def __init__(self, *args, **kwargs):
4343
self._locked_keys = set(self.data)
4444
self._locked_keys_initialised = True
4545

46+
def __setattr__(self, name, value):
47+
"""
48+
Block silent attribute assignment by only allowing internal, private
49+
and known base-class-needed attributes.
50+
51+
This avoids silent failure when users try to set dict.new_attribute,
52+
where it does not change the dictionary as it set an attribute.
53+
"""
54+
if name.startswith("_") or name == "data":
55+
super().__setattr__(name, value)
56+
else:
57+
raise AttributeError(
58+
f"Cannot set attribute '{name}'. "
59+
f"Use item syntax: obj['{name}'] = value"
60+
)
61+
4662
def __setitem__(self, key, value):
4763
"""
4864
Restrict assignment to existing top-level keys.

0 commit comments

Comments
 (0)