I've seen a few patches recently to move things toward duck-typing so this seems welcome.
On Saturday, July 16, 2016 at 4:41:59 PM UTC-4, Marcin Nowak wrote: > > Dear all. > > I would just say that implementation of SelectMultiple.value_from_datadict > is not based on duck typing, and has some disadvantages. > Source: > https://github.com/django/django/blob/master/django/forms/widgets.py#L606 > > def value_from_datadict(self, data, files, name): > if isinstance(data, MultiValueDict): > return data.getlist(name) > return data.get(name) > > First, always checking the instance type has worse efficiency than > try-except block, because most hits may be returned without any additional > checking. > Second, as an author of library which also works with Django, I'm > providing multivalue-like dict, but own implemented - it is not an instance > of MultiValueDict and never be, but it has similar interface (i.e. `get()` > and `getlist()` methods) > > Duck typing will work for both cases. The implementation might look like: > > def value_from_datadict(self, data, files, name): > try: > return data.getlist(name) > except AttributeError: > return data.get(name) > > For most cases a MultiValueDict / QueryDict are passed to forms, so the > function will immediately return a proper value, and will also work for > objects that "looks like MultiValueDict". On other cases it assumes that > the `data` object is dict-like. > > What do you think? > > Kind Regards, > Marcin > > -- You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" 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]. Visit this group at https://groups.google.com/group/django-developers. To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/f0a2f2d6-cc46-44a1-b3ab-4fa1a42b4cf1%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
