mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-24 02:24:03 +03:00
use warnings rather than logging a warning for DecimalField warnings (#9367)
This commit is contained in:
parent
7f18ec1b53
commit
e596f43c4e
|
@ -4,9 +4,9 @@ import datetime
|
||||||
import decimal
|
import decimal
|
||||||
import functools
|
import functools
|
||||||
import inspect
|
import inspect
|
||||||
import logging
|
|
||||||
import re
|
import re
|
||||||
import uuid
|
import uuid
|
||||||
|
import warnings
|
||||||
from collections.abc import Mapping
|
from collections.abc import Mapping
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
|
@ -44,8 +44,6 @@ from rest_framework.utils.formatting import lazy_format
|
||||||
from rest_framework.utils.timezone import valid_datetime
|
from rest_framework.utils.timezone import valid_datetime
|
||||||
from rest_framework.validators import ProhibitSurrogateCharactersValidator
|
from rest_framework.validators import ProhibitSurrogateCharactersValidator
|
||||||
|
|
||||||
logger = logging.getLogger("rest_framework.fields")
|
|
||||||
|
|
||||||
|
|
||||||
class empty:
|
class empty:
|
||||||
"""
|
"""
|
||||||
|
@ -989,9 +987,9 @@ class DecimalField(Field):
|
||||||
self.min_value = min_value
|
self.min_value = min_value
|
||||||
|
|
||||||
if self.max_value is not None and not isinstance(self.max_value, decimal.Decimal):
|
if self.max_value is not None and not isinstance(self.max_value, decimal.Decimal):
|
||||||
logger.warning("max_value in DecimalField should be Decimal type.")
|
warnings.warn("max_value should be a Decimal instance.")
|
||||||
if self.min_value is not None and not isinstance(self.min_value, decimal.Decimal):
|
if self.min_value is not None and not isinstance(self.min_value, decimal.Decimal):
|
||||||
logger.warning("min_value in DecimalField should be Decimal type.")
|
warnings.warn("min_value should be a Decimal instance.")
|
||||||
|
|
||||||
if self.max_digits is not None and self.decimal_places is not None:
|
if self.max_digits is not None and self.decimal_places is not None:
|
||||||
self.max_whole_digits = self.max_digits - self.decimal_places
|
self.max_whole_digits = self.max_digits - self.decimal_places
|
||||||
|
|
|
@ -4,6 +4,7 @@ import os
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
import uuid
|
import uuid
|
||||||
|
import warnings
|
||||||
from decimal import ROUND_DOWN, ROUND_UP, Decimal
|
from decimal import ROUND_DOWN, ROUND_UP, Decimal
|
||||||
from enum import auto
|
from enum import auto
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
@ -1254,15 +1255,19 @@ class TestMinMaxDecimalField(FieldValues):
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_warning_when_not_decimal_types(self, caplog):
|
def test_warning_when_not_decimal_types(self, caplog):
|
||||||
import logging
|
with warnings.catch_warnings(record=True) as w:
|
||||||
serializers.DecimalField(
|
warnings.simplefilter('always')
|
||||||
max_digits=3, decimal_places=1,
|
|
||||||
min_value=10, max_value=20
|
serializers.DecimalField(
|
||||||
)
|
max_digits=3, decimal_places=1,
|
||||||
assert caplog.record_tuples == [
|
min_value=10, max_value=20
|
||||||
("rest_framework.fields", logging.WARNING, "max_value in DecimalField should be Decimal type."),
|
)
|
||||||
("rest_framework.fields", logging.WARNING, "min_value in DecimalField should be Decimal type.")
|
|
||||||
]
|
assert len(w) == 2
|
||||||
|
assert all(issubclass(i.category, UserWarning) for i in w)
|
||||||
|
|
||||||
|
assert 'max_value should be a Decimal instance' in str(w[0].message)
|
||||||
|
assert 'min_value should be a Decimal instance' in str(w[1].message)
|
||||||
|
|
||||||
|
|
||||||
class TestAllowEmptyStrDecimalFieldWithValidators(FieldValues):
|
class TestAllowEmptyStrDecimalFieldWithValidators(FieldValues):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user