mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-04 20:40:14 +03:00
Turn style assertions into warnings, so they can be opted out of.
This commit is contained in:
parent
06dd55ac1c
commit
ec0f4c46ff
|
@ -8,6 +8,7 @@ import inspect
|
||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
import uuid
|
import uuid
|
||||||
|
import warnings
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
@ -325,12 +326,13 @@ class Field(object):
|
||||||
# In order to enforce a consistent style, we error if a redundant
|
# In order to enforce a consistent style, we error if a redundant
|
||||||
# 'source' argument has been used. For example:
|
# 'source' argument has been used. For example:
|
||||||
# my_field = serializer.CharField(source='my_field')
|
# my_field = serializer.CharField(source='my_field')
|
||||||
assert self.source != field_name, (
|
if field_name == self.source:
|
||||||
"It is redundant to specify `source='%s'` on field '%s' in "
|
warnings.warn(
|
||||||
"serializer '%s', because it is the same as the field name. "
|
"It is redundant to specify `source='%s'` on field '%s' in "
|
||||||
"Remove the `source` keyword argument." %
|
"serializer '%s', because it is the same as the field name. "
|
||||||
(field_name, self.__class__.__name__, parent.__class__.__name__)
|
"Remove the `source` keyword argument." %
|
||||||
)
|
(field_name, self.__class__.__name__, parent.__class__.__name__),
|
||||||
|
)
|
||||||
|
|
||||||
self.field_name = field_name
|
self.field_name = field_name
|
||||||
self.parent = parent
|
self.parent = parent
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
import warnings
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
|
||||||
from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist
|
from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist
|
||||||
|
@ -80,10 +81,11 @@ class RelatedField(Field):
|
||||||
'Relational field must provide a `queryset` argument, '
|
'Relational field must provide a `queryset` argument, '
|
||||||
'override `get_queryset`, or set read_only=`True`.'
|
'override `get_queryset`, or set read_only=`True`.'
|
||||||
)
|
)
|
||||||
assert not (self.queryset is not None and kwargs.get('read_only', None)), (
|
if self.queryset is not None and kwargs.get('read_only'):
|
||||||
'Relational fields should not provide a `queryset` argument, '
|
warnings.warn(
|
||||||
'when setting read_only=`True`.'
|
'Relational fields should not provide a `queryset` argument, '
|
||||||
)
|
'when setting read_only=`True`.'
|
||||||
|
)
|
||||||
kwargs.pop('many', None)
|
kwargs.pop('many', None)
|
||||||
kwargs.pop('allow_empty', None)
|
kwargs.pop('allow_empty', None)
|
||||||
super(RelatedField, self).__init__(**kwargs)
|
super(RelatedField, self).__init__(**kwargs)
|
||||||
|
|
|
@ -90,9 +90,10 @@ class TestSource:
|
||||||
def test_redundant_source(self):
|
def test_redundant_source(self):
|
||||||
class ExampleSerializer(serializers.Serializer):
|
class ExampleSerializer(serializers.Serializer):
|
||||||
example_field = serializers.CharField(source='example_field')
|
example_field = serializers.CharField(source='example_field')
|
||||||
with pytest.raises(AssertionError) as exc_info:
|
with pytest.warns(UserWarning) as record:
|
||||||
ExampleSerializer().fields
|
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 "
|
"It is redundant to specify `source='example_field'` on field "
|
||||||
"'CharField' in serializer 'ExampleSerializer', because it is the "
|
"'CharField' in serializer 'ExampleSerializer', because it is the "
|
||||||
"same as the field name. Remove the `source` keyword argument."
|
"same as the field name. Remove the `source` keyword argument."
|
||||||
|
|
Loading…
Reference in New Issue
Block a user