From 8cc15bd5ae5ba99053eef4353fe39ae0a1159f92 Mon Sep 17 00:00:00 2001 From: Sebastian Kreft <911768+sk-@users.noreply.github.com> Date: Tue, 23 Sep 2025 16:22:49 -0300 Subject: [PATCH 1/3] 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. --- django-stubs/db/models/fields/__init__.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django-stubs/db/models/fields/__init__.pyi b/django-stubs/db/models/fields/__init__.pyi index 3f8bf4bc1..4a1981b4a 100644 --- a/django-stubs/db/models/fields/__init__.pyi +++ b/django-stubs/db/models/fields/__init__.pyi @@ -263,7 +263,7 @@ class FloatField(Field[_ST, _GT]): class DecimalField(Field[_ST, _GT]): _pyi_private_set_type: str | float | decimal.Decimal | Combinable _pyi_private_get_type: decimal.Decimal - _pyi_lookup_exact_type: str | decimal.Decimal + _pyi_lookup_exact_type: str | int | decimal.Decimal # attributes max_digits: int decimal_places: int From aaf626a63e0b2530d8b1c76e1026558c5bd4bee5 Mon Sep 17 00:00:00 2001 From: Sebastian Kreft <911768+sk-@users.noreply.github.com> Date: Tue, 23 Sep 2025 21:38:45 -0300 Subject: [PATCH 2/3] add missing test file --- .../managers/querysets/test_filter.yml | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/typecheck/managers/querysets/test_filter.yml b/tests/typecheck/managers/querysets/test_filter.yml index 143b817b8..6243db42d 100644 --- a/tests/typecheck/managers/querysets/test_filter.yml +++ b/tests/typecheck/managers/querysets/test_filter.yml @@ -45,6 +45,29 @@ class User(models.Model): age = models.IntegerField() +# Regression test for #2831 +- case: filter_with_decimal + main: | + from myapp.models import MyModel + from decimal import Decimal + + MyModel.objects.filter(a=1) + MyModel.objects.filter(a="1.0") + MyModel.objects.filter(a=Decimal(1.0)) + MyModel.objects.filter(a=1.0) + out: | + main:7: error: Incompatible type for lookup 'a': (got "float", expected "str | int | Decimal") [misc] + + installed_apps: + - myapp + files: + - path: myapp/__init__.py + - path: myapp/models.py + content: | + from django.db import models + + class MyModel(models.Model): + a = models.DecimalField() - case: filter_with_multiple_fields main: | From 620288f5709ee455fb55d6f39e0f0ccbc80ff86d Mon Sep 17 00:00:00 2001 From: sobolevn Date: Wed, 24 Sep 2025 08:43:09 +0300 Subject: [PATCH 3/3] Update tests/typecheck/managers/querysets/test_filter.yml --- tests/typecheck/managers/querysets/test_filter.yml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/typecheck/managers/querysets/test_filter.yml b/tests/typecheck/managers/querysets/test_filter.yml index 6243db42d..45a46c742 100644 --- a/tests/typecheck/managers/querysets/test_filter.yml +++ b/tests/typecheck/managers/querysets/test_filter.yml @@ -54,10 +54,7 @@ MyModel.objects.filter(a=1) MyModel.objects.filter(a="1.0") MyModel.objects.filter(a=Decimal(1.0)) - MyModel.objects.filter(a=1.0) - out: | - main:7: error: Incompatible type for lookup 'a': (got "float", expected "str | int | Decimal") [misc] - + MyModel.objects.filter(a=1.0) # E: Incompatible type for lookup 'a': (got "float", expected "str | int | Decimal") [misc] installed_apps: - myapp files: