From 91ea13840699abca0e14e20732fd445d65c91914 Mon Sep 17 00:00:00 2001 From: Ryan P Kilby Date: Mon, 1 Jul 2019 05:22:04 -0700 Subject: [PATCH] Allow redundant SerializerMethodField method names (#6767) --- rest_framework/fields.py | 6 ------ tests/test_fields.py | 12 ++++-------- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index a68cd6ecc9..8e2281b524 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -1835,12 +1835,6 @@ def bind(self, field_name, parent): # 'method_name' argument has been used. For example: # my_field = serializer.SerializerMethodField(method_name='get_my_field') default_method_name = 'get_{field_name}'.format(field_name=field_name) - assert self.method_name != default_method_name, ( - "It is redundant to specify `%s` on SerializerMethodField '%s' in " - "serializer '%s', because it is the same as the default method name. " - "Remove the `method_name` argument." % - (self.method_name, field_name, parent.__class__.__name__) - ) # The method name should default to `get_{field_name}`. if self.method_name is None: diff --git a/tests/test_fields.py b/tests/test_fields.py index 468b33e57a..5313aa3952 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -2212,17 +2212,13 @@ def get_example_field(self, obj): } def test_redundant_method_name(self): + # Prior to v3.10, redundant method names were not allowed. + # This restriction has since been removed. class ExampleSerializer(serializers.Serializer): example_field = serializers.SerializerMethodField('get_example_field') - with pytest.raises(AssertionError) as exc_info: - ExampleSerializer().fields - assert str(exc_info.value) == ( - "It is redundant to specify `get_example_field` on " - "SerializerMethodField 'example_field' in serializer " - "'ExampleSerializer', because it is the same as the default " - "method name. Remove the `method_name` argument." - ) + field = ExampleSerializer().fields['example_field'] + assert field.method_name == 'get_example_field' class TestValidationErrorCode: