#29178: Mutable default in `Index` constructor
-------------------------------------+-------------------------------------
               Reporter:  Flavio     |          Owner:  nobody
  Curella                            |
                   Type:             |         Status:  new
  Cleanup/optimization               |
              Component:  Database   |        Version:  master
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  1
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 the {{{__init__}}} method of the {{{Index}}} class has the following
 signature
 (https://github.com/django/django/blob/master/django/db/models/indexes.py#L15):

 {{{
     def __init__(self, *, fields=[], name=None, db_tablespace=None):
 }}}

 The {{{fields}}} argument is set to a mutable object, which is usually
 considered bad practice as it can lead to unexpected results:

 * http://effbot.org/zone/default-values.htm
 * http://docs.python-guide.org/en/latest/writing/gotchas/

 There are some valid uses of mutable defaults, but I can't see any code
 taking advantage of the mutability by looking at the code of the
 {{{Index}}} class.

 If there's no good reason for the default to be mutable, it should be
 changed to:
 {{{
     def __init__(self, *, fields=None, name=None, db_tablespace=None):
         if fields is None:
             fields = []
         # ...rest of the code
 }}}

 If there's a good reason, I think we should document it in a comment.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29178>
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 django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/051.dbc8acc41f879beb11a0f5c91cf66ae5%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to