Skip to content

Commit

Permalink
Turn style assertions into warnings, so they can be opted out of.
Browse files Browse the repository at this point in the history
  • Loading branch information
craigds committed Dec 16, 2016
1 parent 3effb3e commit 5cb243d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
14 changes: 8 additions & 6 deletions rest_framework/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import json
import re
import uuid
import warnings
from collections import OrderedDict

from django.conf import settings
Expand Down Expand Up @@ -330,12 +331,13 @@ def bind(self, field_name, parent):
# 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__)
)
if field_name == self.source:
warnings.warn(
"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
10 changes: 6 additions & 4 deletions rest_framework/relations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# coding: utf-8
from __future__ import unicode_literals

import warnings
from collections import OrderedDict

from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist
Expand Down Expand Up @@ -84,10 +85,11 @@ def __init__(self, **kwargs):
'Relational field must provide a `queryset` argument, '
'override `get_queryset`, or set read_only=`True`.'
)
assert not (self.queryset is not None and kwargs.get('read_only', None)), (
'Relational fields should not provide a `queryset` argument, '
'when setting read_only=`True`.'
)
if self.queryset is not None and kwargs.get('read_only'):
warnings.warn(
'Relational fields should not provide a `queryset` argument, '
'when setting read_only=`True`.'
)
kwargs.pop('many', None)
kwargs.pop('allow_empty', None)
super(RelatedField, self).__init__(**kwargs)
Expand Down
5 changes: 3 additions & 2 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ class ExampleSerializer(serializers.Serializer):
def test_redundant_source(self):
class ExampleSerializer(serializers.Serializer):
example_field = serializers.CharField(source='example_field')
with pytest.raises(AssertionError) as exc_info:
with pytest.warns(UserWarning) as record:
ExampleSerializer().fields
assert str(exc_info.value) == (
assert len(record) == 1
assert record[0].message.args[0] == (
"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."
Expand Down

0 comments on commit 5cb243d

Please sign in to comment.