Skip to content

Commit 2f8e84c

Browse files
authored
1071 Fix compare_dicts for numeric values (#1077)
* fix `compare_dicts` for numeric values * version pin esmerald for now
1 parent 47e5305 commit 2f8e84c

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

piccolo/apps/asgi/commands/new.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"fastapi": ["fastapi>=0.112.1"],
1616
"blacksheep": ["blacksheep"],
1717
"litestar": ["litestar"],
18-
"esmerald": ["esmerald"],
18+
"esmerald": ["esmerald==3.3.0"],
1919
"lilya": ["lilya"],
2020
}
2121
ROUTERS = list(ROUTER_DEPENDENCIES.keys())

piccolo/apps/migrations/auto/diffable_table.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,17 @@ def compare_dicts(
4040

4141
for key, value in dict_1.items():
4242
dict_2_value = dict_2.get(key, ...)
43+
4344
if (
44-
dict_2_value is not ...
45-
and dict_2_value != value
46-
or dict_2_value is ...
45+
# If the value is `...` then it means no value was found.
46+
(dict_2_value is ...)
47+
# We have to compare the types, because if we just use equality
48+
# then 1.0 == 1 is True.
49+
# See this issue:
50+
# https://github.com/piccolo-orm/piccolo/issues/1071
51+
or (type(value) is not type(dict_2_value))
52+
# Finally compare the actual values.
53+
or (dict_2_value != value)
4754
):
4855
output[key] = value
4956

tests/apps/migrations/auto/integration/test_migrations.py

+19
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,25 @@ def test_column_type_conversion_float_decimal(self):
888888
]
889889
)
890890

891+
def test_column_type_conversion_integer_float(self):
892+
"""
893+
Make sure conversion between ``Integer`` and ``Real`` works - related
894+
to this bug:
895+
896+
https://github.com/piccolo-orm/piccolo/issues/1071
897+
898+
"""
899+
self._test_migrations(
900+
table_snapshots=[
901+
[self.table(column)]
902+
for column in [
903+
Real(default=1.0),
904+
Integer(default=1),
905+
Real(default=1.0),
906+
]
907+
]
908+
)
909+
891910
def test_column_type_conversion_json(self):
892911
self._test_migrations(
893912
table_snapshots=[

tests/apps/migrations/auto/test_diffable_table.py

+13
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@ def test_enum_values(self):
7373
response = compare_dicts(dict_1, dict_2)
7474
self.assertEqual(response, {"a": OnDelete.set_default})
7575

76+
def test_numeric_values(self):
77+
"""
78+
Make sure that if we have two numbers which are equal, but different
79+
types, then they are identified as being different.
80+
81+
https://github.com/piccolo-orm/piccolo/issues/1071
82+
83+
"""
84+
dict_1 = {"a": 1}
85+
dict_2 = {"a": 1.0}
86+
response = compare_dicts(dict_1, dict_2)
87+
self.assertEqual(response, {"a": 1})
88+
7689

7790
class TestDiffableTable(TestCase):
7891
def test_subtract(self):

0 commit comments

Comments
 (0)