diff options
| author | Vytis Banaitis <vytis.banaitis@gmail.com> | 2017-02-01 18:41:56 +0200 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2017-02-01 11:41:56 -0500 |
| commit | 8838d4dd498c8f66ea4237fe8a79a5f77d6f95c9 (patch) | |
| tree | b75fa27930b8758ad36669b523b084ac09ce290b /django/db | |
| parent | 0ec4dc91e0e7befdd06aa0613b5d0fbe3c785ee7 (diff) | |
Refs #23919 -- Replaced kwargs.pop() with keyword-only arguments.
Diffstat (limited to 'django/db')
| -rw-r--r-- | django/db/backends/oracle/functions.py | 10 | ||||
| -rw-r--r-- | django/db/models/expressions.py | 11 | ||||
| -rw-r--r-- | django/db/models/fields/__init__.py | 10 | ||||
| -rw-r--r-- | django/db/models/fields/related.py | 10 | ||||
| -rw-r--r-- | django/db/models/fields/related_descriptors.py | 30 | ||||
| -rw-r--r-- | django/db/models/functions/base.py | 5 | ||||
| -rw-r--r-- | django/db/models/query.py | 21 |
7 files changed, 31 insertions, 66 deletions
diff --git a/django/db/backends/oracle/functions.py b/django/db/backends/oracle/functions.py index 7e9b6a6204..1aeb4597e3 100644 --- a/django/db/backends/oracle/functions.py +++ b/django/db/backends/oracle/functions.py @@ -10,15 +10,13 @@ class IntervalToSeconds(Func): EXTRACT(second from %(expressions)s) """ - def __init__(self, expression, **extra): - output_field = extra.pop('output_field', DecimalField()) - super().__init__(expression, output_field=output_field, **extra) + def __init__(self, expression, *, output_field=None, **extra): + super().__init__(expression, output_field=output_field or DecimalField(), **extra) class SecondsToInterval(Func): function = 'NUMTODSINTERVAL' template = "%(function)s(%(expressions)s, 'SECOND')" - def __init__(self, expression, **extra): - output_field = extra.pop('output_field', DurationField()) - super().__init__(expression, output_field=output_field, **extra) + def __init__(self, expression, *, output_field=None, **extra): + super().__init__(expression, output_field=output_field or DurationField(), **extra) diff --git a/django/db/models/expressions.py b/django/db/models/expressions.py index fcbf867b96..af8891da22 100644 --- a/django/db/models/expressions.py +++ b/django/db/models/expressions.py @@ -506,7 +506,7 @@ class Func(Expression): arg_joiner = ', ' arity = None # The number of arguments the function accepts. - def __init__(self, *expressions, **extra): + def __init__(self, *expressions, output_field=None, **extra): if self.arity is not None and len(expressions) != self.arity: raise TypeError( "'%s' takes exactly %s %s (%s given)" % ( @@ -516,7 +516,6 @@ class Func(Expression): len(expressions), ) ) - output_field = extra.pop('output_field', None) super().__init__(output_field=output_field) self.source_expressions = self._parse_expressions(*expressions) self.extra = extra @@ -828,11 +827,9 @@ class Case(Expression): template = 'CASE %(cases)s ELSE %(default)s END' case_joiner = ' ' - def __init__(self, *cases, **extra): + def __init__(self, *cases, default=None, output_field=None, **extra): if not all(isinstance(case, When) for case in cases): raise TypeError("Positional arguments must all be When objects.") - default = extra.pop('default', None) - output_field = extra.pop('output_field', None) super().__init__(output_field) self.cases = list(cases) self.default = self._parse_expressions(default)[0] @@ -983,8 +980,8 @@ class Subquery(Expression): class Exists(Subquery): template = 'EXISTS(%(subquery)s)' - def __init__(self, *args, **kwargs): - self.negated = kwargs.pop('negated', False) + def __init__(self, *args, negated=False, **kwargs): + self.negated = negated super().__init__(*args, **kwargs) def __invert__(self): diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 809a14277e..68a42f0236 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -2045,15 +2045,11 @@ class SlugField(CharField): default_validators = [validators.validate_slug] description = _("Slug (up to %(max_length)s)") - def __init__(self, *args, **kwargs): - kwargs['max_length'] = kwargs.get('max_length', 50) - # Set db_index=True unless it's been set manually. - if 'db_index' not in kwargs: - kwargs['db_index'] = True - self.allow_unicode = kwargs.pop('allow_unicode', False) + def __init__(self, *args, max_length=50, db_index=True, allow_unicode=False, **kwargs): + self.allow_unicode = allow_unicode if self.allow_unicode: self.default_validators = [validators.validate_unicode_slug] - super().__init__(*args, **kwargs) + super().__init__(*args, max_length=max_length, db_index=db_index, **kwargs) def deconstruct(self): name, path, args, kwargs = super().deconstruct() diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index dd84d780fb..cf39de7287 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -922,15 +922,14 @@ class ForeignKey(ForeignObject): if self.remote_field.field_name is None: self.remote_field.field_name = cls._meta.pk.name - def formfield(self, **kwargs): - db = kwargs.pop('using', None) + def formfield(self, *, using=None, **kwargs): if isinstance(self.remote_field.model, str): raise ValueError("Cannot create form field for %r yet, because " "its related model %r has not been loaded yet" % (self.name, self.remote_field.model)) defaults = { 'form_class': forms.ModelChoiceField, - 'queryset': self.remote_field.model._default_manager.using(db), + 'queryset': self.remote_field.model._default_manager.using(using), 'to_field_name': self.remote_field.field_name, } defaults.update(kwargs) @@ -1609,11 +1608,10 @@ class ManyToManyField(RelatedField): def save_form_data(self, instance, data): getattr(instance, self.attname).set(data) - def formfield(self, **kwargs): - db = kwargs.pop('using', None) + def formfield(self, *, using=None, **kwargs): defaults = { 'form_class': forms.ModelMultipleChoiceField, - 'queryset': self.remote_field.model._default_manager.using(db), + 'queryset': self.remote_field.model._default_manager.using(using), } defaults.update(kwargs) # If initial is passed in, it's a list of related objects, but the diff --git a/django/db/models/fields/related_descriptors.py b/django/db/models/fields/related_descriptors.py index b3e87cd482..5899a2324c 100644 --- a/django/db/models/fields/related_descriptors.py +++ b/django/db/models/fields/related_descriptors.py @@ -510,10 +510,8 @@ def create_reverse_many_to_one_manager(superclass, rel): self.core_filters = {self.field.name: instance} - def __call__(self, **kwargs): - # We use **kwargs rather than a kwarg argument to enforce the - # `manager='manager_name'` syntax. - manager = getattr(self.model, kwargs.pop('manager')) + def __call__(self, *, manager): + manager = getattr(self.model, manager) manager_class = create_reverse_many_to_one_manager(manager.__class__, rel) return manager_class(self.instance) do_not_call_in_templates = True @@ -569,9 +567,8 @@ def create_reverse_many_to_one_manager(superclass, rel): cache_name = self.field.related_query_name() return queryset, rel_obj_attr, instance_attr, False, cache_name - def add(self, *objs, **kwargs): + def add(self, *objs, bulk=True): self._remove_prefetched_objects() - bulk = kwargs.pop('bulk', True) objs = list(objs) db = router.db_for_write(self.model, instance=self.instance) @@ -622,10 +619,9 @@ def create_reverse_many_to_one_manager(superclass, rel): # remove() and clear() are only provided if the ForeignKey can have a value of null. if rel.field.null: - def remove(self, *objs, **kwargs): + def remove(self, *objs, bulk=True): if not objs: return - bulk = kwargs.pop('bulk', True) val = self.field.get_foreign_related_value(self.instance) old_ids = set() for obj in objs: @@ -639,8 +635,7 @@ def create_reverse_many_to_one_manager(superclass, rel): self._clear(self.filter(pk__in=old_ids), bulk) remove.alters_data = True - def clear(self, **kwargs): - bulk = kwargs.pop('bulk', True) + def clear(self, *, bulk=True): self._clear(self, bulk) clear.alters_data = True @@ -658,14 +653,11 @@ def create_reverse_many_to_one_manager(superclass, rel): obj.save(update_fields=[self.field.name]) _clear.alters_data = True - def set(self, objs, **kwargs): + def set(self, objs, *, bulk=True, clear=False): # Force evaluation of `objs` in case it's a queryset whose value # could be affected by `manager.clear()`. Refs #19816. objs = tuple(objs) - bulk = kwargs.pop('bulk', True) - clear = kwargs.pop('clear', False) - if self.field.null: db = router.db_for_write(self.model, instance=self.instance) with transaction.atomic(using=db, savepoint=False): @@ -791,10 +783,8 @@ def create_forward_many_to_many_manager(superclass, rel, reverse): "a many-to-many relationship can be used." % instance.__class__.__name__) - def __call__(self, **kwargs): - # We use **kwargs rather than a kwarg argument to enforce the - # `manager='manager_name'` syntax. - manager = getattr(self.model, kwargs.pop('manager')) + def __call__(self, *, manager): + manager = getattr(self.model, manager) manager_class = create_forward_many_to_many_manager(manager.__class__, rel, reverse) return manager_class(instance=self.instance) do_not_call_in_templates = True @@ -924,7 +914,7 @@ def create_forward_many_to_many_manager(superclass, rel, reverse): ) clear.alters_data = True - def set(self, objs, **kwargs): + def set(self, objs, *, clear=False): if not rel.through._meta.auto_created: opts = self.through._meta raise AttributeError( @@ -937,8 +927,6 @@ def create_forward_many_to_many_manager(superclass, rel, reverse): # could be affected by `manager.clear()`. Refs #19816. objs = tuple(objs) - clear = kwargs.pop('clear', False) - db = router.db_for_write(self.through, instance=self.instance) with transaction.atomic(using=db, savepoint=False): if clear: diff --git a/django/db/models/functions/base.py b/django/db/models/functions/base.py index e9bf01ff0d..905f740a6c 100644 --- a/django/db/models/functions/base.py +++ b/django/db/models/functions/base.py @@ -165,9 +165,8 @@ class Length(Transform): function = 'LENGTH' lookup_name = 'length' - def __init__(self, expression, **extra): - output_field = extra.pop('output_field', fields.IntegerField()) - super().__init__(expression, output_field=output_field, **extra) + def __init__(self, expression, *, output_field=None, **extra): + super().__init__(expression, output_field=output_field or fields.IntegerField(), **extra) def as_mysql(self, compiler, connection): return super().as_sql(compiler, connection, function='CHAR_LENGTH') diff --git a/django/db/models/query.py b/django/db/models/query.py index dcb1360054..244cfaf164 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -691,11 +691,7 @@ class QuerySet: clone._iterable_class = ValuesIterable return clone - def values_list(self, *fields, **kwargs): - flat = kwargs.pop('flat', False) - if kwargs: - raise TypeError('Unexpected keyword arguments to values_list: %s' % (list(kwargs),)) - + def values_list(self, *fields, flat=False): if flat and len(fields) > 1: raise TypeError("'flat' is not valid when values_list is called with more than one field.") @@ -812,7 +808,7 @@ class QuerySet: else: return self._filter_or_exclude(None, **filter_obj) - def _combinator_query(self, combinator, *other_qs, **kwargs): + def _combinator_query(self, combinator, *other_qs, all=False): # Clone the query to inherit the select list and everything clone = self._clone() # Clear limits and ordering so they can be reapplied @@ -820,18 +816,11 @@ class QuerySet: clone.query.clear_limits() clone.query.combined_queries = (self.query,) + tuple(qs.query for qs in other_qs) clone.query.combinator = combinator - clone.query.combinator_all = kwargs.pop('all', False) + clone.query.combinator_all = all return clone - def union(self, *other_qs, **kwargs): - if kwargs: - unexpected_kwarg = next((k for k in kwargs.keys() if k != 'all'), None) - if unexpected_kwarg: - raise TypeError( - "union() received an unexpected keyword argument '%s'" % - (unexpected_kwarg,) - ) - return self._combinator_query('union', *other_qs, **kwargs) + def union(self, *other_qs, all=False): + return self._combinator_query('union', *other_qs, all=all) def intersection(self, *other_qs): return self._combinator_query('intersection', *other_qs) |
