From d96f3f21b81cf7fb305debc338323c176e22a6b5 Mon Sep 17 00:00:00 2001 From: Oliver Sauder Date: Thu, 9 Dec 2021 22:03:14 +0400 Subject: [PATCH] Use assert when checking for reserved field names 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. --- rest_framework_json_api/serializers.py | 11 +++++------ tests/test_serializers.py | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/rest_framework_json_api/serializers.py b/rest_framework_json_api/serializers.py index 0b4a8dfd..1ac504ab 100644 --- a/rest_framework_json_api/serializers.py +++ b/rest_framework_json_api/serializers.py @@ -165,12 +165,11 @@ def get_fields(self): found_reserved_field_names = self._reserved_field_names.intersection( fields.keys() ) - if found_reserved_field_names: - raise AttributeError( - f"Serializer class {self.__class__.__module__}.{self.__class__.__qualname__} " - f"uses following reserved field name(s) which is not allowed: " - f"{', '.join(sorted(found_reserved_field_names))}" - ) + assert not found_reserved_field_names, ( + f"Serializer class {self.__class__.__module__}.{self.__class__.__qualname__} " + f"uses following reserved field name(s) which is not allowed: " + f"{', '.join(sorted(found_reserved_field_names))}" + ) if "type" in fields: # see https://jsonapi.org/format/#document-resource-object-fields diff --git a/tests/test_serializers.py b/tests/test_serializers.py index 70bb140f..19ee23d6 100644 --- a/tests/test_serializers.py +++ b/tests/test_serializers.py @@ -37,7 +37,7 @@ class Meta: def test_reserved_field_names(): - with pytest.raises(AttributeError) as e: + with pytest.raises(AssertionError) as e: class ReservedFieldNamesSerializer(serializers.Serializer): meta = serializers.CharField()