Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove assertions for redundant SerializerMethodField source args. #2422

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions docs/topics/3.0-announcement.md
Original file line number Diff line number Diff line change
Expand Up @@ -699,18 +699,6 @@ The argument to `SerializerMethodField` is now optional, and defaults to `get_<f
def get_billing_details(self, account):
return calculate_billing(account)

In order to ensure a consistent code style an assertion error will be raised if you include a redundant method name argument that matches the default method name. For example, the following code *will raise an error*:

billing_details = serializers.SerializerMethodField('get_billing_details')

#### Enforcing consistent `source` usage.

I've see several codebases that unnecessarily include the `source` argument, setting it to the same value as the field name. This usage is redundant and confusing, making it less obvious that `source` is usually not required.

The following usage will *now raise an error*:

email = serializers.EmailField(source='email')

#### The `UniqueValidator` and `UniqueTogetherValidator` classes.

REST framework now provides new validators that allow you to ensure field uniqueness, while still using a completely explicit `Serializer` class instead of using `ModelSerializer`.
Expand Down
20 changes: 0 additions & 20 deletions rest_framework/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,16 +210,6 @@ def bind(self, field_name, parent):
Called when a field is added to the parent serializer instance.
"""

# In order to enforce a consistent style, we error if a redundant
# 'source' argument has been used. For example:
# my_field = serializer.CharField(source='my_field')
assert self.source != field_name, (
"It is redundant to specify `source='%s'` on field '%s' in "
"serializer '%s', because it is the same as the field name. "
"Remove the `source` keyword argument." %
(field_name, self.__class__.__name__, parent.__class__.__name__)
)

self.field_name = field_name
self.parent = parent

Expand Down Expand Up @@ -1219,17 +1209,7 @@ def __init__(self, method_name=None, **kwargs):
super(SerializerMethodField, self).__init__(**kwargs)

def bind(self, field_name, parent):
# In order to enforce a consistent style, we error if a redundant
# 'method_name' argument has been used. For example:
# my_field = serializer.CharField(source='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:
self.method_name = default_method_name
Expand Down
24 changes: 0 additions & 24 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,6 @@ class ExampleSerializer(serializers.Serializer):
assert serializer.is_valid()
assert serializer.validated_data == {'other': 'abc'}

def test_redundant_source(self):
class ExampleSerializer(serializers.Serializer):
example_field = serializers.CharField(source='example_field')
with pytest.raises(AssertionError) as exc_info:
ExampleSerializer().fields
assert str(exc_info.value) == (
"It is redundant to specify `source='example_field'` on field "
"'CharField' in serializer 'ExampleSerializer', because it is the "
"same as the field name. Remove the `source` keyword argument."
)


class TestReadOnly:
def setup(self):
Expand Down Expand Up @@ -1078,16 +1067,3 @@ def get_example_field(self, obj):
assert serializer.data == {
'example_field': 'ran get_example_field(123)'
}

def test_redundant_method_name(self):
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."
)