From 97acd4d2f92eef8c285bac070d437bf0fd52e071 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Wed, 19 Nov 2025 08:22:44 +0100 Subject: Fixed #26609 -- Extended fields.E004 system check for unordered iterables. Co-authored-by: Karl Wooster --- tests/invalid_models_tests/test_ordinary_fields.py | 42 +++++++++++++++++++--- 1 file changed, 38 insertions(+), 4 deletions(-) (limited to 'tests') diff --git a/tests/invalid_models_tests/test_ordinary_fields.py b/tests/invalid_models_tests/test_ordinary_fields.py index 2c2653a538..04c18d7ddd 100644 --- a/tests/invalid_models_tests/test_ordinary_fields.py +++ b/tests/invalid_models_tests/test_ordinary_fields.py @@ -199,8 +199,8 @@ class CharFieldTests(TestCase): field.check(), [ Error( - "'choices' must be a mapping (e.g. a dictionary) or an iterable " - "(e.g. a list or tuple).", + "'choices' must be a mapping (e.g. a dictionary) or an " + "ordered iterable (e.g. a list or tuple, but not a set).", obj=field, id="fields.E004", ), @@ -906,8 +906,42 @@ class IntegerFieldTests(SimpleTestCase): field.check(), [ Error( - "'choices' must be a mapping (e.g. a dictionary) or an iterable " - "(e.g. a list or tuple).", + "'choices' must be a mapping (e.g. a dictionary) or an " + "ordered iterable (e.g. a list or tuple, but not a set).", + obj=field, + id="fields.E004", + ), + ], + ) + + def test_unordered_choices_set(self): + class Model(models.Model): + field = models.IntegerField(choices={1, 2, 3}) + + field = Model._meta.get_field("field") + self.assertEqual( + field.check(), + [ + Error( + "'choices' must be a mapping (e.g. a dictionary) or an " + "ordered iterable (e.g. a list or tuple, but not a set).", + obj=field, + id="fields.E004", + ), + ], + ) + + def test_unordered_choices_frozenset(self): + class Model(models.Model): + field = models.IntegerField(choices=frozenset({1, 2, 3})) + + field = Model._meta.get_field("field") + self.assertEqual( + field.check(), + [ + Error( + "'choices' must be a mapping (e.g. a dictionary) or an " + "ordered iterable (e.g. a list or tuple, but not a set).", obj=field, id="fields.E004", ), -- cgit v1.3