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

use warnings rather than logging a warning for DecimalField warnings #9367

Merged
Merged
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
8 changes: 3 additions & 5 deletions rest_framework/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import decimal
import functools
import inspect
import logging
import re
import uuid
import warnings
from collections.abc import Mapping
from enum import Enum

Expand Down Expand Up @@ -44,8 +44,6 @@
from rest_framework.utils.timezone import valid_datetime
from rest_framework.validators import ProhibitSurrogateCharactersValidator

logger = logging.getLogger("rest_framework.fields")


class empty:
"""
Expand Down Expand Up @@ -989,9 +987,9 @@ def __init__(self, max_digits, decimal_places, coerce_to_string=None, max_value=
self.min_value = min_value

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):
logger.warning("min_value in DecimalField should be Decimal type.")
warnings.warn("min_value should be a Decimal instance.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a stacklevel param so that python displays a trace with the faulty calling code rather than this line?


if self.max_digits is not None and self.decimal_places is not None:
self.max_whole_digits = self.max_digits - self.decimal_places
Expand Down
23 changes: 14 additions & 9 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import re
import sys
import uuid
import warnings
from decimal import ROUND_DOWN, ROUND_UP, Decimal
from enum import auto
from unittest.mock import patch
Expand Down Expand Up @@ -1254,15 +1255,19 @@ class TestMinMaxDecimalField(FieldValues):
)

def test_warning_when_not_decimal_types(self, caplog):
import logging
serializers.DecimalField(
max_digits=3, decimal_places=1,
min_value=10, max_value=20
)
assert caplog.record_tuples == [
("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.")
]
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')

serializers.DecimalField(
max_digits=3, decimal_places=1,
min_value=10, max_value=20
)

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):
Expand Down
Loading