Strange named parameter passing style

46 views
Skip to first unread message

Alexey Gerasimov

unread,
Dec 15, 2016, 7:40:02 AM12/15/16
to Django users

Why "flat" keyword is accepted that way? Why not just as def values_list(self, *fields, flat=False)? Because of that I cannot have support in my IDE (autocomplete)

def values_list(self, *fields, **kwargs):
        flat = kwargs.pop('flat', False)
        if kwargs:
            raise TypeError('Unexpected keyword arguments to values_list: %s' % (list(kwargs),))

        if flat and len(fields) > 1:
           raise TypeError("'flat' is not valid when values_list is called with more than one field.")

        _fields = []
        expressions = {}

        for field in fields:
           if hasattr(field, 'resolve_expression'):
               field_id = str(id(field))
               expressions[field_id] = field
               _fields.append(field_id)
           else:
               _fields.append(field)
   
        clone = self._values(*_fields, **expressions)
        clone._iterable_class = FlatValuesListIterable if flat else ValuesListIterable
        return clone


GMail

unread,
Dec 15, 2016, 7:45:16 AM12/15/16
to django...@googlegroups.com
Probably for adding other arguments later without braking function signature? I'm with you on this one, though. Using kwargs everywhere it's possible really helps developing using Django. Maybe this is a subject for django-d...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/237365e6-2566-4ef3-b86a-005487afb5b4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

knbk

unread,
Dec 15, 2016, 10:21:37 AM12/15/16
to Django users
Python 2 does not support named arguments after *fields, and raises a SyntaxError. As long as Django supports Python 2, we're stuck with the current approach. I'm sure the new style will be used once Python 2 support is dropped.
Reply all
Reply to author
Forward
0 new messages