From 7803f429a4d435623cb7b91dd324d3aadad87380 Mon Sep 17 00:00:00 2001 From: Sergey Fedoseev Date: Thu, 5 Nov 2015 08:45:42 +0500 Subject: [PATCH] Refs #25665 -- Deprecated getters/setters of Point coordinate properties. --- django/contrib/gis/geos/point.py | 79 +++++++++++++++++++------ docs/internals/deprecation.txt | 3 + docs/releases/1.10.txt | 4 ++ tests/gis_tests/geos_tests/test_geos.py | 13 +++- 4 files changed, 79 insertions(+), 20 deletions(-) diff --git a/django/contrib/gis/geos/point.py b/django/contrib/gis/geos/point.py index 65cf1344c9..beccfb0af5 100644 --- a/django/contrib/gis/geos/point.py +++ b/django/contrib/gis/geos/point.py @@ -1,9 +1,11 @@ +import warnings from ctypes import c_uint from django.contrib.gis.geos import prototypes as capi from django.contrib.gis.geos.error import GEOSException from django.contrib.gis.geos.geometry import GEOSGeometry from django.utils import six +from django.utils.deprecation import RemovedInDjango20Warning from django.utils.six.moves import range @@ -95,40 +97,79 @@ class Point(GEOSGeometry): _get_single_internal = _get_single_external - def get_x(self): + @property + def x(self): "Returns the X component of the Point." return self._cs.getOrdinate(0, 0) - def set_x(self, value): + @x.setter + def x(self, value): "Sets the X component of the Point." self._cs.setOrdinate(0, 0, value) - def get_y(self): + @property + def y(self): "Returns the Y component of the Point." return self._cs.getOrdinate(1, 0) - def set_y(self, value): + @y.setter + def y(self, value): "Sets the Y component of the Point." self._cs.setOrdinate(1, 0, value) - def get_z(self): + @property + def z(self): "Returns the Z component of the Point." - if self.hasz: - return self._cs.getOrdinate(2, 0) - else: - return None + return self._cs.getOrdinate(2, 0) if self.hasz else None + + @z.setter + def z(self, value): + "Sets the Z component of the Point." + if not self.hasz: + raise GEOSException('Cannot set Z on 2D Point.') + self._cs.setOrdinate(2, 0, value) + + def get_x(self): + warnings.warn( + "`get_x()` is deprecated, use the `x` property instead.", + RemovedInDjango20Warning, 2 + ) + return self.x + + def set_x(self, value): + warnings.warn( + "`set_x()` is deprecated, use the `x` property instead.", + RemovedInDjango20Warning, 2 + ) + self.x = value + + def get_y(self): + warnings.warn( + "`get_y()` is deprecated, use the `y` property instead.", + RemovedInDjango20Warning, 2 + ) + return self.y + + def set_y(self, value): + warnings.warn( + "`set_y()` is deprecated, use the `y` property instead.", + RemovedInDjango20Warning, 2 + ) + self.y = value + + def get_z(self): + warnings.warn( + "`get_z()` is deprecated, use the `z` property instead.", + RemovedInDjango20Warning, 2 + ) + return self.z def set_z(self, value): - "Sets the Z component of the Point." - if self.hasz: - self._cs.setOrdinate(2, 0, value) - else: - raise GEOSException('Cannot set Z on 2D Point.') - - # X, Y, Z properties - x = property(get_x, set_x) - y = property(get_y, set_y) - z = property(get_z, set_z) + warnings.warn( + "`set_z()` is deprecated, use the `z` property instead.", + RemovedInDjango20Warning, 2 + ) + self.z = value # ### Tuple setting and retrieval routines. ### def get_coords(self): diff --git a/docs/internals/deprecation.txt b/docs/internals/deprecation.txt index db2bf02af2..77ab15eb3c 100644 --- a/docs/internals/deprecation.txt +++ b/docs/internals/deprecation.txt @@ -115,6 +115,9 @@ details on these changes. * The ``get_srid()`` and ``set_srid()`` methods of ``django.contrib.gis.geos.GEOSGeometry`` will be removed. +* The ``get_x()``, ``set_x()``, ``get_y()``, ``set_y()``, ``get_z()``, and + ``set_z()`` methods of ``django.contrib.gis.geos.Point`` will be removed. + .. _deprecation-removed-in-1.10: 1.10 diff --git a/docs/releases/1.10.txt b/docs/releases/1.10.txt index d4a0e63094..0329456be0 100644 --- a/docs/releases/1.10.txt +++ b/docs/releases/1.10.txt @@ -335,6 +335,10 @@ This prevents confusion about an assignment resulting in an implicit save. :class:`~django.contrib.gis.geos.GEOSGeometry` are deprecated in favor of the :attr:`~django.contrib.gis.geos.GEOSGeometry.srid` property. +* The ``get_x()``, ``set_x()``, ``get_y()``, ``set_y()``, ``get_z()``, and + ``set_z()`` methods of :class:`~django.contrib.gis.geos.Point` are deprecated + in favor of the ``x``, ``y``, and ``z`` properties. + Miscellaneous ~~~~~~~~~~~~~ diff --git a/tests/gis_tests/geos_tests/test_geos.py b/tests/gis_tests/geos_tests/test_geos.py index 504676d4c6..9b1a7e384f 100644 --- a/tests/gis_tests/geos_tests/test_geos.py +++ b/tests/gis_tests/geos_tests/test_geos.py @@ -862,7 +862,8 @@ class GEOSTest(unittest.TestCase, TestDataMixin): # Testing __getitem__ (doesn't work on Point or Polygon) if isinstance(g, Point): - self.assertRaises(IndexError, g.get_x) + with self.assertRaises(IndexError): + g.x elif isinstance(g, Polygon): lr = g.shell self.assertEqual('LINEARRING EMPTY', lr.wkt) @@ -1148,3 +1149,13 @@ class GEOSTest(unittest.TestCase, TestDataMixin): p.set_srid(321) self.assertEqual(p.srid, 321) + + @ignore_warnings(category=RemovedInDjango20Warning) + def test_deprecated_point_coordinate_getters_setters(self): + p = Point(1, 2, 3) + self.assertEqual((p.get_x(), p.get_y(), p.get_z()), (p.x, p.y, p.z)) + + p.set_x(3) + p.set_y(1) + p.set_z(2) + self.assertEqual((p.x, p.y, p.z), (3, 1, 2))