diff --git a/app/models/__init__.py b/app/models/__init__.py index d63f21bbe..c7770b1e1 100644 --- a/app/models/__init__.py +++ b/app/models/__init__.py @@ -18,6 +18,9 @@ class JSONModel(): def __hash__(self): return hash(self.id) + def __dir__(self): + return super().__dir__() + list(sorted(self.ALLOWED_PROPERTIES)) + def __eq__(self, other): return self.id == other.id diff --git a/tests/app/models/test_base_model.py b/tests/app/models/test_base_model.py index 0c3ccf2f7..7f5c748ee 100644 --- a/tests/app/models/test_base_model.py +++ b/tests/app/models/test_base_model.py @@ -68,3 +68,11 @@ def test_model_doesnt_swallow_attribute_errors(json_response): Custom(json_response).foo assert str(e.value) == 'Something has gone wrong' + + +def test_dynamic_properties_are_introspectable(): + + class Custom(JSONModel): + ALLOWED_PROPERTIES = {'foo', 'bar', 'baz'} + + assert dir(Custom({}))[-3:] == ['bar', 'baz', 'foo']