Skip to content

Commit

Permalink
Fixed #28370 -- Deprecated the context arg of Field.from_db_value() a…
Browse files Browse the repository at this point in the history
…nd Expression.convert_value().

Unused since a0d1663.
  • Loading branch information
timgraham committed Jul 20, 2017
1 parent 8d5095d commit 487362f
Show file tree
Hide file tree
Showing 28 changed files with 124 additions and 60 deletions.
2 changes: 1 addition & 1 deletion django/contrib/gis/db/backends/mysql/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def get_db_converters(self, expression):

# https://dev.mysql.com/doc/refman/en/spatial-function-argument-handling.html
# MySQL 5.7.5 adds support for the empty geometry collections, but they are represented with invalid WKT.
def convert_invalid_empty_geometry_collection(self, value, expression, connection, context):
def convert_invalid_empty_geometry_collection(self, value, expression, connection):
if value == b'GEOMETRYCOLLECTION()':
return b'GEOMETRYCOLLECTION EMPTY'
return value
4 changes: 2 additions & 2 deletions django/contrib/gis/db/models/aggregates.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Extent(GeoAggregate):
def __init__(self, expression, **extra):
super().__init__(expression, output_field=ExtentField(), **extra)

def convert_value(self, value, expression, connection, context):
def convert_value(self, value, expression, connection):
return connection.ops.convert_extent(value)


Expand All @@ -54,7 +54,7 @@ class Extent3D(GeoAggregate):
def __init__(self, expression, **extra):
super().__init__(expression, output_field=ExtentField(), **extra)

def convert_value(self, value, expression, connection, context):
def convert_value(self, value, expression, connection):
return connection.ops.convert_extent3d(value)


Expand Down
4 changes: 2 additions & 2 deletions django/contrib/gis/db/models/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def deconstruct(self):
kwargs['geography'] = self.geography
return name, path, args, kwargs

def from_db_value(self, value, expression, connection, context):
def from_db_value(self, value, expression, connection):
if value:
value = Geometry(value)
srid = value.srid
Expand Down Expand Up @@ -351,7 +351,7 @@ def db_type(self, connection):
self._check_connection(connection)
return super().db_type(connection)

def from_db_value(self, value, expression, connection, context):
def from_db_value(self, value, expression, connection):
return connection.ops.parse_raster(value)

def contribute_to_class(self, cls, name, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions django/contrib/gis/db/models/sql/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def get_db_prep_value(self, value, connection, prepared=False):
return value
return getattr(value, self.area_att)

def from_db_value(self, value, expression, connection, context):
def from_db_value(self, value, expression, connection):
# If the database returns a Decimal, convert it to a float as expected
# by the Python geometric objects.
if isinstance(value, Decimal):
Expand Down Expand Up @@ -54,7 +54,7 @@ def get_db_prep_value(self, value, connection, prepared=False):
raise ValueError('Distance measure is supplied, but units are unknown for result.')
return getattr(value, self.distance_att)

def from_db_value(self, value, expression, connection, context):
def from_db_value(self, value, expression, connection):
if value is None or not self.distance_att:
return value
return Distance(**{self.distance_att: value})
Expand Down
6 changes: 3 additions & 3 deletions django/contrib/postgres/aggregates/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ArrayAgg(Aggregate):
def __init__(self, expression, distinct=False, **extra):
super().__init__(expression, distinct='DISTINCT ' if distinct else '', **extra)

def convert_value(self, value, expression, connection, context):
def convert_value(self, value, expression, connection):
if not value:
return []
return value
Expand All @@ -39,7 +39,7 @@ class JSONBAgg(Aggregate):
function = 'JSONB_AGG'
output_field = JSONField()

def convert_value(self, value, expression, connection, context):
def convert_value(self, value, expression, connection):
if not value:
return []
return value
Expand All @@ -53,7 +53,7 @@ def __init__(self, expression, delimiter, distinct=False, **extra):
distinct = 'DISTINCT ' if distinct else ''
super().__init__(expression, delimiter=delimiter, distinct=distinct, **extra)

def convert_value(self, value, expression, connection, context):
def convert_value(self, value, expression, connection):
if not value:
return ''
return value
2 changes: 1 addition & 1 deletion django/contrib/postgres/aggregates/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class RegrCount(StatAggregate):
def __init__(self, y, x):
super().__init__(y=y, x=x, output_field=IntegerField())

def convert_value(self, value, expression, connection, context):
def convert_value(self, value, expression, connection):
if value is None:
return 0
return int(value)
Expand Down
7 changes: 5 additions & 2 deletions django/contrib/postgres/fields/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.core import checks, exceptions
from django.db.models import Field, IntegerField, Transform
from django.db.models.lookups import Exact, In
from django.utils.inspect import func_supports_parameter
from django.utils.translation import gettext_lazy as _

from ..utils import prefix_validation_error
Expand Down Expand Up @@ -103,11 +104,13 @@ def to_python(self, value):
value = [self.base_field.to_python(val) for val in vals]
return value

def _from_db_value(self, value, expression, connection, context):
def _from_db_value(self, value, expression, connection):
if value is None:
return value
return [
self.base_field.from_db_value(item, expression, connection, context)
self.base_field.from_db_value(item, expression, connection, {})
if func_supports_parameter(self.base_field.from_db_value, 'context') # RemovedInDjango30Warning
else self.base_field.from_db_value(item, expression, connection)
for item in value
]

Expand Down
11 changes: 9 additions & 2 deletions django/core/cache/backends/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.db import DatabaseError, connections, models, router, transaction
from django.utils import timezone
from django.utils.encoding import force_bytes
from django.utils.inspect import func_supports_parameter


class Options:
Expand Down Expand Up @@ -64,7 +65,10 @@ def get(self, key, default=None, version=None):
expression = models.Expression(output_field=models.DateTimeField())
for converter in (connection.ops.get_db_converters(expression) +
expression.get_db_converters(connection)):
expires = converter(expires, expression, connection, {})
if func_supports_parameter(converter, 'context'): # RemovedInDjango30Warning
expires = converter(expires, expression, connection, {})
else:
expires = converter(expires, expression, connection)

if expires < timezone.now():
db = router.db_for_write(self.cache_model_class)
Expand Down Expand Up @@ -126,7 +130,10 @@ def _base_set(self, mode, key, value, timeout=DEFAULT_TIMEOUT):
expression = models.Expression(output_field=models.DateTimeField())
for converter in (connection.ops.get_db_converters(expression) +
expression.get_db_converters(connection)):
current_expires = converter(current_expires, expression, connection, {})
if func_supports_parameter(converter, 'context'): # RemovedInDjango30Warning
current_expires = converter(current_expires, expression, connection, {})
else:
current_expires = converter(current_expires, expression, connection)

exp = connection.ops.adapt_datetimefield_value(exp)
if result and (mode == 'set' or (mode == 'add' and current_expires < now)):
Expand Down
2 changes: 1 addition & 1 deletion django/db/backends/base/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ def get_db_converters(self, expression):
"""
return []

def convert_durationfield_value(self, value, expression, connection, context):
def convert_durationfield_value(self, value, expression, connection):
if value is not None:
value = str(decimal.Decimal(value) / decimal.Decimal(1000000))
value = parse_duration(value)
Expand Down
8 changes: 4 additions & 4 deletions django/db/backends/mysql/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,23 +221,23 @@ def get_db_converters(self, expression):
converters.append(self.convert_uuidfield_value)
return converters

def convert_textfield_value(self, value, expression, connection, context):
def convert_textfield_value(self, value, expression, connection):
if value is not None:
value = force_text(value)
return value

def convert_booleanfield_value(self, value, expression, connection, context):
def convert_booleanfield_value(self, value, expression, connection):
if value in (0, 1):
value = bool(value)
return value

def convert_datetimefield_value(self, value, expression, connection, context):
def convert_datetimefield_value(self, value, expression, connection):
if value is not None:
if settings.USE_TZ:
value = timezone.make_aware(value, self.connection.timezone)
return value

def convert_uuidfield_value(self, value, expression, connection, context):
def convert_uuidfield_value(self, value, expression, connection):
if value is not None:
value = uuid.UUID(value)
return value
Expand Down
16 changes: 8 additions & 8 deletions django/db/backends/oracle/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,17 +165,17 @@ def get_db_converters(self, expression):
converters.append(self.convert_empty_values)
return converters

def convert_textfield_value(self, value, expression, connection, context):
def convert_textfield_value(self, value, expression, connection):
if isinstance(value, Database.LOB):
value = value.read()
return value

def convert_binaryfield_value(self, value, expression, connection, context):
def convert_binaryfield_value(self, value, expression, connection):
if isinstance(value, Database.LOB):
value = force_bytes(value.read())
return value

def convert_booleanfield_value(self, value, expression, connection, context):
def convert_booleanfield_value(self, value, expression, connection):
if value in (0, 1):
value = bool(value)
return value
Expand All @@ -184,28 +184,28 @@ def convert_booleanfield_value(self, value, expression, connection, context):
# DATE and TIMESTAMP columns, but Django wants to see a
# python datetime.date, .time, or .datetime.

def convert_datetimefield_value(self, value, expression, connection, context):
def convert_datetimefield_value(self, value, expression, connection):
if value is not None:
if settings.USE_TZ:
value = timezone.make_aware(value, self.connection.timezone)
return value

def convert_datefield_value(self, value, expression, connection, context):
def convert_datefield_value(self, value, expression, connection):
if isinstance(value, Database.Timestamp):
value = value.date()
return value

def convert_timefield_value(self, value, expression, connection, context):
def convert_timefield_value(self, value, expression, connection):
if isinstance(value, Database.Timestamp):
value = value.time()
return value

def convert_uuidfield_value(self, value, expression, connection, context):
def convert_uuidfield_value(self, value, expression, connection):
if value is not None:
value = uuid.UUID(value)
return value

def convert_empty_values(self, value, expression, connection, context):
def convert_empty_values(self, value, expression, connection):
# Oracle stores empty strings as null. We need to undo this in
# order to adhere to the Django convention of using the empty
# string instead of null, but only if the field accepts the
Expand Down
12 changes: 6 additions & 6 deletions django/db/backends/sqlite3/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,38 +212,38 @@ def get_db_converters(self, expression):
converters.append(self.convert_booleanfield_value)
return converters

def convert_datetimefield_value(self, value, expression, connection, context):
def convert_datetimefield_value(self, value, expression, connection):
if value is not None:
if not isinstance(value, datetime.datetime):
value = parse_datetime(value)
if settings.USE_TZ and not timezone.is_aware(value):
value = timezone.make_aware(value, self.connection.timezone)
return value

def convert_datefield_value(self, value, expression, connection, context):
def convert_datefield_value(self, value, expression, connection):
if value is not None:
if not isinstance(value, datetime.date):
value = parse_date(value)
return value

def convert_timefield_value(self, value, expression, connection, context):
def convert_timefield_value(self, value, expression, connection):
if value is not None:
if not isinstance(value, datetime.time):
value = parse_time(value)
return value

def convert_decimalfield_value(self, value, expression, connection, context):
def convert_decimalfield_value(self, value, expression, connection):
if value is not None:
value = expression.output_field.format_number(value)
value = backend_utils.typecast_decimal(value)
return value

def convert_uuidfield_value(self, value, expression, connection, context):
def convert_uuidfield_value(self, value, expression, connection):
if value is not None:
value = uuid.UUID(value)
return value

def convert_booleanfield_value(self, value, expression, connection, context):
def convert_booleanfield_value(self, value, expression, connection):
return bool(value) if value in (1, 0) else value

def bulk_insert_sql(self, fields, placeholder_rows):
Expand Down
6 changes: 3 additions & 3 deletions django/db/models/aggregates.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def __init__(self, expression, distinct=False, **extra):
def _get_repr_options(self):
return {'distinct': self.extra['distinct'] != ''}

def convert_value(self, value, expression, connection, context):
def convert_value(self, value, expression, connection):
if value is None:
return 0
return int(value)
Expand All @@ -99,7 +99,7 @@ def __init__(self, expression, sample=False, **extra):
def _get_repr_options(self):
return {'sample': self.function == 'STDDEV_SAMP'}

def convert_value(self, value, expression, connection, context):
def convert_value(self, value, expression, connection):
if value is None:
return value
return float(value)
Expand Down Expand Up @@ -129,7 +129,7 @@ def __init__(self, expression, sample=False, **extra):
def _get_repr_options(self):
return {'sample': self.function == 'VAR_SAMP'}

def convert_value(self, value, expression, connection, context):
def convert_value(self, value, expression, connection):
if value is None:
return value
return float(value)
2 changes: 1 addition & 1 deletion django/db/models/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def _resolve_output_field(self):
raise FieldError('Expression contains mixed types. You must set output_field.')
return output_field

def convert_value(self, value, expression, connection, context):
def convert_value(self, value, expression, connection):
"""
Expressions provide their own converters because users have the option
of manually specifying the output_field which may be a different type
Expand Down
2 changes: 1 addition & 1 deletion django/db/models/fields/related.py
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ def db_type(self, connection):
def db_parameters(self, connection):
return {"type": self.db_type(connection), "check": self.db_check(connection)}

def convert_empty_strings(self, value, expression, connection, context):
def convert_empty_strings(self, value, expression, connection):
if (not value) and isinstance(value, str):
return None
return value
Expand Down
2 changes: 1 addition & 1 deletion django/db/models/functions/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def resolve_expression(self, query=None, allow_joins=True, reuse=None, summarize
))
return copy

def convert_value(self, value, expression, connection, context):
def convert_value(self, value, expression, connection):
if isinstance(self.output_field, DateTimeField):
if settings.USE_TZ:
if value is None:
Expand Down
16 changes: 15 additions & 1 deletion django/db/models/sql/compiler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import collections
import re
import warnings
from itertools import chain

from django.core.exceptions import EmptyResultSet, FieldError
Expand All @@ -12,6 +13,8 @@
from django.db.models.sql.query import Query, get_order_dir
from django.db.transaction import TransactionManagementError
from django.db.utils import DatabaseError, NotSupportedError
from django.utils.deprecation import RemovedInDjango30Warning
from django.utils.inspect import func_supports_parameter

FORCE = object()

Expand Down Expand Up @@ -926,7 +929,18 @@ def apply_converters(self, row, converters):
for pos, (convs, expression) in converters.items():
value = row[pos]
for converter in convs:
value = converter(value, expression, self.connection, self.query.context)
if func_supports_parameter(converter, 'context'):
warnings.warn(
'Remove the context parameter from %s.%s(). Support for it '
'will be removed in Django 3.0.' % (
converter.__self__.__class__.__name__,
converter.__name__,
),
RemovedInDjango30Warning,
)
value = converter(value, expression, self.connection, {})
else:
value = converter(value, expression, self.connection)
row[pos] = value
return tuple(row)

Expand Down
Loading

0 comments on commit 487362f

Please sign in to comment.