Fixed #25611 -- Standardized descriptor signatures.

This commit is contained in:
Tim Graham 2015-10-26 11:31:16 -04:00
parent 34af2bc523
commit 0b5d32faca
11 changed files with 28 additions and 28 deletions

View File

@ -235,7 +235,7 @@ class GenericForeignKey(object):
def is_cached(self, instance): def is_cached(self, instance):
return hasattr(instance, self.cache_attr) return hasattr(instance, self.cache_attr)
def __get__(self, instance, instance_type=None): def __get__(self, instance, cls=None):
if instance is None: if instance is None:
return self return self

View File

@ -17,19 +17,19 @@ class SpatialProxy(object):
self._field = field self._field = field
self._klass = klass self._klass = klass
def __get__(self, obj, type=None): def __get__(self, instance, cls=None):
""" """
This accessor retrieves the geometry or raster, initializing it using This accessor retrieves the geometry or raster, initializing it using
the corresponding class specified during initialization and the value the corresponding class specified during initialization and the value
of the field. Currently, GEOS or OGR geometries as well as GDALRasters of the field. Currently, GEOS or OGR geometries as well as GDALRasters
are supported. are supported.
""" """
if obj is None: if instance is None:
# Accessed on a class, not an instance # Accessed on a class, not an instance
return self return self
# Getting the value of the field. # Getting the value of the field.
geo_value = obj.__dict__[self._field.attname] geo_value = instance.__dict__[self._field.attname]
if isinstance(geo_value, self._klass): if isinstance(geo_value, self._klass):
geo_obj = geo_value geo_obj = geo_value
@ -39,10 +39,10 @@ class SpatialProxy(object):
# Otherwise, a geometry or raster object is built using the field's # Otherwise, a geometry or raster object is built using the field's
# contents, and the model's corresponding attribute is set. # contents, and the model's corresponding attribute is set.
geo_obj = self._klass(geo_value) geo_obj = self._klass(geo_value)
setattr(obj, self._field.attname, geo_obj) setattr(instance, self._field.attname, geo_obj)
return geo_obj return geo_obj
def __set__(self, obj, value): def __set__(self, instance, value):
""" """
This accessor sets the proxied geometry or raster with the This accessor sets the proxied geometry or raster with the
corresponding class specified during initialization. corresponding class specified during initialization.
@ -68,8 +68,8 @@ class SpatialProxy(object):
pass pass
else: else:
raise TypeError('Cannot set %s SpatialProxy (%s) with value of type: %s' % ( raise TypeError('Cannot set %s SpatialProxy (%s) with value of type: %s' % (
obj.__class__.__name__, gtype, type(value))) instance.__class__.__name__, gtype, type(value)))
# Setting the objects dictionary with the value, and returning. # Setting the objects dictionary with the value, and returning.
obj.__dict__[self._field.attname] = value instance.__dict__[self._field.attname] = value
return value return value

View File

@ -70,7 +70,7 @@ IntegrityError = Database.IntegrityError
class _UninitializedOperatorsDescriptor(object): class _UninitializedOperatorsDescriptor(object):
def __get__(self, instance, owner): def __get__(self, instance, cls=None):
# If connection.operators is looked up before a connection has been # If connection.operators is looked up before a connection has been
# created, transparently initialize connection.operators to avert an # created, transparently initialize connection.operators to avert an
# AttributeError. # AttributeError.

View File

@ -155,7 +155,7 @@ class FileDescriptor(object):
def __init__(self, field): def __init__(self, field):
self.field = field self.field = field
def __get__(self, instance=None, owner=None): def __get__(self, instance, cls=None):
if instance is None: if instance is None:
return self return self

View File

@ -140,7 +140,7 @@ class ForwardManyToOneDescriptor(object):
setattr(rel_obj, rel_obj_cache_name, instance) setattr(rel_obj, rel_obj_cache_name, instance)
return queryset, rel_obj_attr, instance_attr, True, self.cache_name return queryset, rel_obj_attr, instance_attr, True, self.cache_name
def __get__(self, instance, instance_type=None): def __get__(self, instance, cls=None):
""" """
Get the related instance through the forward relation. Get the related instance through the forward relation.
@ -148,7 +148,7 @@ class ForwardManyToOneDescriptor(object):
- ``self`` is the descriptor managing the ``parent`` attribute - ``self`` is the descriptor managing the ``parent`` attribute
- ``instance`` is the ``child`` instance - ``instance`` is the ``child`` instance
- ``instance_type`` in the ``Child`` class (we don't need it) - ``cls`` is the ``Child`` class (we don't need it)
""" """
if instance is None: if instance is None:
return self return self
@ -311,7 +311,7 @@ class ReverseOneToOneDescriptor(object):
setattr(rel_obj, rel_obj_cache_name, instance) setattr(rel_obj, rel_obj_cache_name, instance)
return queryset, rel_obj_attr, instance_attr, True, self.cache_name return queryset, rel_obj_attr, instance_attr, True, self.cache_name
def __get__(self, instance, instance_type=None): def __get__(self, instance, cls=None):
""" """
Get the related instance through the reverse relation. Get the related instance through the reverse relation.
@ -452,7 +452,7 @@ class ReverseManyToOneDescriptor(object):
self.rel, self.rel,
) )
def __get__(self, instance, instance_type=None): def __get__(self, instance, cls=None):
""" """
Get the related objects through the reverse relation. Get the related objects through the reverse relation.

View File

@ -245,9 +245,9 @@ class ManagerDescriptor(object):
def __init__(self, manager): def __init__(self, manager):
self.manager = manager self.manager = manager
def __get__(self, instance, type=None): def __get__(self, instance, cls=None):
if instance is not None: if instance is not None:
raise AttributeError("Manager isn't accessible via %s instances" % type.__name__) raise AttributeError("Manager isn't accessible via %s instances" % cls.__name__)
return self.manager return self.manager
@ -257,7 +257,7 @@ class AbstractManagerDescriptor(object):
def __init__(self, model): def __init__(self, model):
self.model = model self.model = model
def __get__(self, instance, type=None): def __get__(self, instance, cls=None):
raise AttributeError("Manager isn't available; %s is abstract" % ( raise AttributeError("Manager isn't available; %s is abstract" % (
self.model._meta.object_name, self.model._meta.object_name,
)) ))
@ -269,7 +269,7 @@ class SwappedManagerDescriptor(object):
def __init__(self, model): def __init__(self, model):
self.model = model self.model = model
def __get__(self, instance, type=None): def __get__(self, instance, cls=None):
raise AttributeError( raise AttributeError(
"Manager isn't available; '%s.%s' has been swapped for '%s'" % ( "Manager isn't available; '%s.%s' has been swapped for '%s'" % (
self.model._meta.app_label, self.model._meta.app_label,

View File

@ -121,7 +121,7 @@ class DeferredAttribute(object):
def __init__(self, field_name, model): def __init__(self, field_name, model):
self.field_name = field_name self.field_name = field_name
def __get__(self, instance, owner): def __get__(self, instance, cls=None):
""" """
Retrieves and caches the value from the datastore on the first lookup. Retrieves and caches the value from the datastore on the first lookup.
Returns the cached value. Returns the cached value.

View File

@ -1053,7 +1053,7 @@ class CheckCondition(object):
def __init__(self, cond_func): def __init__(self, cond_func):
self.cond_func = cond_func self.cond_func = cond_func
def __get__(self, obj, objtype): def __get__(self, instance, cls=None):
return self.cond_func() return self.cond_func()

View File

@ -11,10 +11,10 @@ from django.utils import six
class classonlymethod(classmethod): class classonlymethod(classmethod):
def __get__(self, instance, owner): def __get__(self, instance, cls=None):
if instance is not None: if instance is not None:
raise AttributeError("This method is available only on the class, not on instances.") raise AttributeError("This method is available only on the class, not on instances.")
return super(classonlymethod, self).__get__(instance, owner) return super(classonlymethod, self).__get__(instance, cls)
def method_decorator(decorator, name=''): def method_decorator(decorator, name=''):
@ -189,8 +189,8 @@ class classproperty(object):
def __init__(self, method=None): def __init__(self, method=None):
self.fget = method self.fget = method
def __get__(self, instance, owner): def __get__(self, instance, cls=None):
return self.fget(owner) return self.fget(cls)
def getter(self, method): def getter(self, method):
self.fget = method self.fget = method

View File

@ -27,7 +27,7 @@ class cached_property(object):
self.__doc__ = getattr(func, '__doc__') self.__doc__ = getattr(func, '__doc__')
self.name = name or func.__name__ self.name = name or func.__name__
def __get__(self, instance, type=None): def __get__(self, instance, cls=None):
if instance is None: if instance is None:
return self return self
res = instance.__dict__[self.name] = self.func(instance) res = instance.__dict__[self.name] = self.func(instance)

View File

@ -286,7 +286,7 @@ class MethodDecoratorTests(SimpleTestCase):
def __call__(self, arg): def __call__(self, arg):
return self.wrapped(arg) return self.wrapped(arg)
def __get__(self, instance, owner): def __get__(self, instance, cls=None):
return self return self
class descriptor_wrapper(object): class descriptor_wrapper(object):
@ -294,8 +294,8 @@ class MethodDecoratorTests(SimpleTestCase):
self.wrapped = wrapped self.wrapped = wrapped
self.__name__ = wrapped.__name__ self.__name__ = wrapped.__name__
def __get__(self, instance, owner): def __get__(self, instance, cls=None):
return bound_wrapper(self.wrapped.__get__(instance, owner)) return bound_wrapper(self.wrapped.__get__(instance, cls))
class Test(object): class Test(object):
@method_dec @method_dec