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

Fix 1275: Make MaxValueValidator default an integer #1276

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion django_filters/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def get_max_validator(self):
"""
Return a MaxValueValidator for the field, or None to disable.
"""
return MaxValueValidator(1e50)
return MaxValueValidator(int(1e50))
Copy link
Owner

Choose a reason for hiding this comment

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

I just want to have a look at the order of validation in the field, but maybe this should be a decimal already...
(i.e. did we already run the value from the widget through to_python, giving us a decimal in the value < limit check...)


@property
def field(self):
Expand Down
25 changes: 24 additions & 1 deletion tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from decimal import FloatOperation, getcontext

from django import forms
from django.test import TestCase, override_settings

Expand Down Expand Up @@ -267,5 +269,26 @@ class Meta:
self.assertFalse(f.is_valid())
self.assertEqual(
f.errors,
{'average_rating': ['Ensure this value is less than or equal to 1e+50.']}
{'average_rating': [
'Ensure this value is less than or equal to %d.' % int(1e50)
]}
)


def test_number_filter_max_value_validation_no_float(self):
class F(FilterSet):
class Meta:
model = Book
fields = ['average_rating']

ctx = getcontext()
ctx.traps[FloatOperation] = True
f = F({'average_rating': '1E1001'})
self.assertTrue(f.is_bound)
self.assertFalse(f.is_valid())
self.assertEqual(
f.errors,
{'average_rating': [
'Ensure this value is less than or equal to %d.' % int(1e50)
]}
)