Raise exception when overriding a custom property

If a subclass of `JSONModel` defines a property then we shouldn’t try
to override it with the value from the underlying dictionary.

Rather than silently fail we should raise an exception because it will
help keep our list of `ALLOWED_PROPERTIES` nice and tidy.
This commit is contained in:
Chris Hill-Scott
2020-10-28 10:03:02 +00:00
parent 89c63e3243
commit 38e9e84f77
3 changed files with 7 additions and 5 deletions

View File

@@ -13,7 +13,7 @@ class JSONModel(SerialisedModel):
# in the case of a bad request _dict may be `None`
self._dict = _dict or {}
for property in self.ALLOWED_PROPERTIES:
if property in self._dict and not hasattr(self, property):
if property in self._dict:
setattr(self, property, self._dict[property])
def __bool__(self):

View File

@@ -28,7 +28,6 @@ class Job(JSONModel):
'template_version',
'original_file_name',
'created_at',
'processing_started',
'notification_count',
'created_by',
'template_type',

View File

@@ -11,7 +11,7 @@ def test_looks_up_from_dict():
assert Custom({'foo': 'bar'}).foo == 'bar'
def test_prefers_property_to_dict():
def test_raises_when_overriding_custom_properties():
class Custom(JSONModel):
@@ -19,9 +19,12 @@ def test_prefers_property_to_dict():
@property
def foo(self):
return 'bar'
pass
assert Custom({'foo': 'NOPE'}).foo == 'bar'
with pytest.raises(AttributeError) as e:
Custom({'foo': 'NOPE'})
assert str(e.value) == "can't set attribute"
@pytest.mark.parametrize('json_response', (