#26609: Add system check for field choices using unorderable iterables
-------------------------------------+-------------------------------------
     Reporter:  Harry Percival       |                    Owner:  Karl
         Type:                       |                   Status:  assigned
  Cleanup/optimization               |
    Component:  Database layer       |                  Version:  dev
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  migrations           |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Comment (by Karl):

 For example, this test passes:

 {{{
 def test_unordered_choices(self):
         class Model(models.Model):
             CHOICES_INT = {1,2}
             field = models.IntegerField(choices=CHOICES_INT)

         field = Model._meta.get_field("field")
         self.assertEqual(
             field.check(),
             [
                 Error(
                     "'choices' must be a mapping (e.g. a dictionary) or an
 order-stable "
                     " iterable (e.g. a list or tuple).",
                     obj=field,
                     id="fields.E004",
                 ),
             ],
         )
 }}}

 But this one does not (i.e. the check does not get flagged):
 {{{
 def test_unordered_choices(self):
         class Model(models.Model):
             CHOICES_INT = {(1, "2"), (2, "2")}
             field = models.IntegerField(choices=CHOICES_INT)

         field = Model._meta.get_field("field")
         self.assertEqual(
             field.check(),
             [
                 Error(
                     "'choices' must be a mapping (e.g. a dictionary) or an
 order-stable "
                     " iterable (e.g. a list or tuple).",
                     obj=field,
                     id="fields.E004",
                 ),
             ],
         )
 }}}

 {{{
 AssertionError: Lists differ: [] != [<Error: level=40, msg="'choices' must
 be [135 chars]13'>]

 Second list contains 1 additional elements.
 First extra element 0:
 <Error: level=40, msg="'choices' must be a 'orderable' iterable (e.g. a
 list or tuple), not 'set'.", hint=None,
 obj=<django.db.models.fields.IntegerField: field>, id='fields.E013'>

 - []
 + [<Error: level=40, msg="'choices' must be a 'orderable' iterable (e.g. a
 list or tuple), not 'set'.", hint=None,
 obj=<django.db.models.fields.IntegerField: field>, id='fields.E013'>]
 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/26609#comment:14>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/01070192ba860af4-1dbf3523-b68d-451d-af20-5bb82ddf45db-000000%40eu-central-1.amazonses.com.

Reply via email to