Skip to content

Commit 28e553a

Browse files
authored
Use assert when checking for reserved field names (#1025)
This check is for development purposes to verify that the source code is well configured but not needed during production. Therefore a assert is more appropriate.
1 parent 5403d50 commit 28e553a

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

rest_framework_json_api/serializers.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,11 @@ def get_fields(self):
165165
found_reserved_field_names = self._reserved_field_names.intersection(
166166
fields.keys()
167167
)
168-
if found_reserved_field_names:
169-
raise AttributeError(
170-
f"Serializer class {self.__class__.__module__}.{self.__class__.__qualname__} "
171-
f"uses following reserved field name(s) which is not allowed: "
172-
f"{', '.join(sorted(found_reserved_field_names))}"
173-
)
168+
assert not found_reserved_field_names, (
169+
f"Serializer class {self.__class__.__module__}.{self.__class__.__qualname__} "
170+
f"uses following reserved field name(s) which is not allowed: "
171+
f"{', '.join(sorted(found_reserved_field_names))}"
172+
)
174173

175174
if "type" in fields:
176175
# see https://jsonapi.org/format/#document-resource-object-fields

tests/test_serializers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Meta:
3737

3838

3939
def test_reserved_field_names():
40-
with pytest.raises(AttributeError) as e:
40+
with pytest.raises(AssertionError) as e:
4141

4242
class ReservedFieldNamesSerializer(serializers.Serializer):
4343
meta = serializers.CharField()

0 commit comments

Comments
 (0)