Skip to content

Commit dc2a00c

Browse files
sk-sobolevn
andauthored
fix: allow to query decimal fields with int (#2838)
* fix: allow to query decimal fields with int Django allow to query decimal fields with int, and given there should be no precision loss, we should accept ints to query such fields. Note: even though we do accept floats when setting a value in a decimal field, I opted for only accepting ints in this PR, as to minimize the impact of the change. Fixes #2831. Co-authored-by: Sebastian Kreft <911768+sk-@users.noreply.github.com> Co-authored-by: sobolevn <mail@sobolevn.me>
1 parent d8aa20d commit dc2a00c

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

django-stubs/db/models/fields/__init__.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ class FloatField(Field[_ST, _GT]):
263263
class DecimalField(Field[_ST, _GT]):
264264
_pyi_private_set_type: str | float | decimal.Decimal | Combinable
265265
_pyi_private_get_type: decimal.Decimal
266-
_pyi_lookup_exact_type: str | decimal.Decimal
266+
_pyi_lookup_exact_type: str | int | decimal.Decimal
267267
# attributes
268268
max_digits: int
269269
decimal_places: int

tests/typecheck/managers/querysets/test_filter.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,26 @@
4545
class User(models.Model):
4646
age = models.IntegerField()
4747
48+
# Regression test for #2831
49+
- case: filter_with_decimal
50+
main: |
51+
from myapp.models import MyModel
52+
from decimal import Decimal
53+
54+
MyModel.objects.filter(a=1)
55+
MyModel.objects.filter(a="1.0")
56+
MyModel.objects.filter(a=Decimal(1.0))
57+
MyModel.objects.filter(a=1.0) # E: Incompatible type for lookup 'a': (got "float", expected "str | int | Decimal") [misc]
58+
installed_apps:
59+
- myapp
60+
files:
61+
- path: myapp/__init__.py
62+
- path: myapp/models.py
63+
content: |
64+
from django.db import models
65+
66+
class MyModel(models.Model):
67+
a = models.DecimalField()
4868
4969
- case: filter_with_multiple_fields
5070
main: |

0 commit comments

Comments
 (0)