#25085: Select Widget's __deepcopy__ does not copy its choices.
-------------------------------------+-------------------------------------
     Reporter:  ericfc               |                    Owner:  ericfc
         Type:  Bug                  |                   Status:  assigned
    Component:  Forms                |                  Version:  1.8
     Severity:  Normal               |               Resolution:
     Keywords:  Select Widget        |             Triage Stage:
  deepcopy __deepcopy__ choices      |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by ericfc:

Old description:

> Select inherits its parent's (Widget's) __deepcopy__ which does not take
> into consideration its choices field and we get weird behavior like
> below:
>
> {{{
> from django import forms
>
> class Fields(forms.Form):
>     field = forms.ChoiceField(choices=(('one', 'one'), ('two', 'two')))
>
> class Foo(Fields):
>     def __init__(self, *args, **kwargs):
>         super(Foo, self).__init__(*args, **kwargs)
>         self.fields['field'].widget.choices[0] = ('foo', 'foo')
>
> class Bar(Fields):
>     pass
>
> foo = Foo()
> bar = Bar()
>
> foo.fields['field'].widget.choices
> >>>[('foo', 'foo'), ('two', 'two')]
>
> bar.fields['field'].widget.choices
> >>>[('foo', 'foo'), ('two', 'two')]
> }}}
>
> Where foo and bar have nothing to do with one another.
> A more simple example:
> {{{
> import copy
> from django import forms
>
> widget = forms.Select()
> widget_copy = copy.deepcopy(widget)
>
> widget.attrs is widget_copy.attrs
> >>>False
> widget.choices is widget_copy.choices
> >>>True
> }}}
> Since we did a deepcopy the {{widget.choices is widget_copy.choices}}}
> must have returned {{False}} yet it returned {{{True}}}.

New description:

 Select inherits its parent's (Widget's) __deepcopy__ which does not take
 into consideration its choices field and we get weird behavior like below:

 {{{
 from django import forms

 class Fields(forms.Form):
     field = forms.ChoiceField(choices=(('one', 'one'), ('two', 'two')))

 class Foo(Fields):
     def __init__(self, *args, **kwargs):
         super(Foo, self).__init__(*args, **kwargs)
         self.fields['field'].widget.choices[0] = ('foo', 'foo')

 class Bar(Fields):
     pass

 foo = Foo()
 bar = Bar()

 foo.fields['field'].widget.choices
 >>>[('foo', 'foo'), ('two', 'two')]

 bar.fields['field'].widget.choices
 >>>[('foo', 'foo'), ('two', 'two')]
 }}}

 Where foo and bar have nothing to do with one another.
 A more simple example:
 {{{
 import copy
 from django import forms

 widget = forms.Select()
 widget_copy = copy.deepcopy(widget)

 widget.attrs is widget_copy.attrs
 >>>False
 widget.choices is widget_copy.choices
 >>>True
 }}}
 Since we did a deepcopy the {{{widget.choices is widget_copy.choices}}}
 must have returned {{{False}}} yet it returned {{{True}}}.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/25085#comment:3>
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/064.d3fc651decbd07dab26a6fbabdf99545%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to