From 965f115bb139270dfbf40a0d7865d21ca3e66bd3 Mon Sep 17 00:00:00 2001 From: Christian Ledermann Date: Sun, 26 Nov 2023 13:38:08 +0000 Subject: [PATCH] Remove hash method. it cannot be guaranteed that objects which compare equal have the same hash value --- .pre-commit-config.yaml | 5 ----- docs/HISTORY.rst | 2 +- pygeoif/geometry.py | 7 ------ tests/test_geometrycollection.py | 36 ------------------------------ tests/test_line.py | 6 ----- tests/test_linear_ring.py | 6 ----- tests/test_multiline.py | 8 ------- tests/test_multipoint.py | 6 ----- tests/test_multipolygon.py | 38 -------------------------------- tests/test_point.py | 12 ---------- tests/test_polygon.py | 6 ----- 11 files changed, 1 insertion(+), 131 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e822e498..b5a3bbcd 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -31,11 +31,6 @@ repos: rev: v0.3.1 hooks: - id: absolufy-imports - - repo: https://github.com/hakancelikdev/unimport - rev: 1.1.0 - hooks: - - id: unimport - args: [--remove, --include-star-import, --ignore-init, --gitignore] - repo: https://github.com/pycqa/isort rev: 5.12.0 hooks: diff --git a/docs/HISTORY.rst b/docs/HISTORY.rst index 3cec4bf8..2ccdf5fd 100644 --- a/docs/HISTORY.rst +++ b/docs/HISTORY.rst @@ -5,7 +5,7 @@ Changelog ------------------ - remove Python 3.7 support - - Geometries are now immutable and hashable + - Geometries are now immutable (but not hashable) - add ``force_2d`` and ``force_3d`` factories [Alex Svetkin] 1.1.1 (2023/10/27) diff --git a/pygeoif/geometry.py b/pygeoif/geometry.py index fdbe7164..9b862777 100644 --- a/pygeoif/geometry.py +++ b/pygeoif/geometry.py @@ -67,9 +67,6 @@ def __delattr__(self, *args: Any) -> NoReturn: # noqa: ANN401 msg, ) - def __hash__(self) -> int: - return hash(self._geoms) - def __str__(self) -> str: return self.wkt @@ -1095,10 +1092,6 @@ def __eq__(self, other: object) -> bool: second=other.__geo_interface__, # type: ignore [attr-defined] ) - def __hash__(self) -> int: - """Return the hash of the collection.""" - return hash(self.wkt) - def __len__(self) -> int: """ Length of the collection. diff --git a/tests/test_geometrycollection.py b/tests/test_geometrycollection.py index 59065667..51b016cc 100644 --- a/tests/test_geometrycollection.py +++ b/tests/test_geometrycollection.py @@ -425,39 +425,3 @@ def test_nested_geometry_collection_repr_eval() -> None: ).__geo_interface__ == gc.__geo_interface__ ) - - -def test_nested_geometry_collection_hash() -> None: - multipoint = geometry.MultiPoint([(0, 0), (1, 1), (1, 2), (2, 2)]) - gc1 = geometry.GeometryCollection([geometry.Point(0, 0), multipoint]) - line1 = geometry.LineString([(0, 0), (3, 1)]) - gc2 = geometry.GeometryCollection([gc1, line1]) - poly1 = geometry.Polygon([(0, 0), (1, 1), (1, 0), (0, 0)]) - e = [(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)] - i = [(1, 0), (0.5, 0.5), (1, 1), (1.5, 0.5), (1, 0)] - poly2 = geometry.Polygon(e, [i]) - p0 = geometry.Point(0, 0) - p1 = geometry.Point(-1, -1) - ring = geometry.LinearRing([(0, 0), (1, 1), (1, 0), (0, 0)]) - line = geometry.LineString([(0, 0), (1, 1)]) - gc = geometry.GeometryCollection([gc2, poly1, poly2, p0, p1, ring, line]) - - assert hash(gc) == hash( - geometry.GeometryCollection( - [ - gc2, - poly1, - poly2, - p0, - p1, - ring, - line, - ], - ), - ) - - -def test_hash_empty() -> None: - gc = geometry.GeometryCollection([]) - - assert hash(gc) == hash(geometry.GeometryCollection([])) diff --git a/tests/test_line.py b/tests/test_line.py index 2d6fd5bf..b0b4df8a 100644 --- a/tests/test_line.py +++ b/tests/test_line.py @@ -19,12 +19,6 @@ def test_coords_get_3d() -> None: assert line.coords == ((0.0, 0.0, 0), (1.0, 1.0, 1)) -def test_hash() -> None: - line = geometry.LineString([(0, 0, 0), (1, 1, 1)]) - - assert hash(line) == hash(geometry.LineString([(0, 0, 0), (1, 1, 1)])) - - def test_set_geoms_raises() -> None: line = geometry.LineString([(0, 0), (1, 0)]) # pragma: no mutate diff --git a/tests/test_linear_ring.py b/tests/test_linear_ring.py index 3a67a879..8e4d3c9b 100644 --- a/tests/test_linear_ring.py +++ b/tests/test_linear_ring.py @@ -240,9 +240,3 @@ def test_empty_bounds() -> None: ring = geometry.LinearRing([]) assert ring.bounds == () - - -def test_hash() -> None: - ring = geometry.LinearRing([(0, 0), (4, 0), (4, 2), (0, 2)]) - - assert hash(ring) == hash(((0, 0), (4, 0), (4, 2), (0, 2), (0, 0))) diff --git a/tests/test_multiline.py b/tests/test_multiline.py index 0aadecae..40d14280 100644 --- a/tests/test_multiline.py +++ b/tests/test_multiline.py @@ -200,11 +200,3 @@ def test_empty_bounds() -> None: lines = geometry.MultiLineString([]) assert lines.bounds == () - - -def test_hash() -> None: - lines = geometry.MultiLineString(([(0, 0), (1, 1)], [[0.0, 0.0], [1.0, 2.0]])) - - assert hash(lines) == hash( - geometry.MultiLineString(([(0, 0), (1, 1)], [[0.0, 0.0], [1.0, 2.0]])), - ) diff --git a/tests/test_multipoint.py b/tests/test_multipoint.py index 9b64bef3..693bad36 100644 --- a/tests/test_multipoint.py +++ b/tests/test_multipoint.py @@ -148,12 +148,6 @@ def test_from_points_unique() -> None: ) -def test_hash() -> None: - multipoint = geometry.MultiPoint([(0, 0), (1, 0), (2, 2)]) - - assert hash(multipoint) == hash(geometry.MultiPoint([(0, 0), (1, 0), (2, 2)])) - - def test_empty() -> None: multipoint = geometry.MultiPoint([(1, None)]) diff --git a/tests/test_multipolygon.py b/tests/test_multipolygon.py index 4ae637ba..967b8fee 100644 --- a/tests/test_multipolygon.py +++ b/tests/test_multipolygon.py @@ -284,41 +284,3 @@ def test_empty_bounds() -> None: polys = geometry.MultiPolygon([]) assert polys.bounds == () - - -def test_hash() -> None: - polys = geometry.MultiPolygon( - ( - ( - ((0, 0), (0, 2), (2, 2), (2, 0), (0, 0)), - ( - ((1, 0), (0.5, 0.5), (1, 1), (1.5, 0.5), (1, 0)), - ((1, 0), (0.5, 0.5), (1, 1), (1.5, 0.5), (1, 0)), - ), - ), - (((0, 0, 0), (1, 1, 0), (1, 0, 0), (0, 0, 0)),), - ( - ((0, 0), (0, 2), (2, 2), (2, 0), (0, 0)), - (((1, 0), (0.5, 0.5), (1, 1), (1.5, 0.5), (1, 0)),), - ), - ), - ) - - assert hash(polys) == hash( - geometry.MultiPolygon( - ( - ( - ((0, 0), (0, 2), (2, 2), (2, 0), (0, 0)), - ( - ((1, 0), (0.5, 0.5), (1, 1), (1.5, 0.5), (1, 0)), - ((1, 0), (0.5, 0.5), (1, 1), (1.5, 0.5), (1, 0)), - ), - ), - (((0, 0, 0), (1, 1, 0), (1, 0, 0), (0, 0, 0)),), - ( - ((0, 0), (0, 2), (2, 2), (2, 0), (0, 0)), - (((1, 0), (0.5, 0.5), (1, 1), (1.5, 0.5), (1, 0)),), - ), - ), - ), - ) diff --git a/tests/test_point.py b/tests/test_point.py index 37ad8803..33a3fb93 100644 --- a/tests/test_point.py +++ b/tests/test_point.py @@ -57,12 +57,6 @@ def test_xy() -> None: assert point.y == 0 -def test_hash() -> None: - point = geometry.Point(1.0, 2.0, 3.0) - - assert hash(point) == hash((1.0, 2.0, 3.0)) - - def test_xy_raises_error_accessing_z() -> None: point = geometry.Point(1, 0) @@ -244,9 +238,3 @@ def test_empty_bounds() -> None: point = geometry.Point(None, None) assert point.bounds == () - - -def test_hash_empty() -> None: - point = geometry.Point(None, None) - - assert hash(point) == hash(()) diff --git a/tests/test_polygon.py b/tests/test_polygon.py index 07c8b056..4ef462d3 100644 --- a/tests/test_polygon.py +++ b/tests/test_polygon.py @@ -20,12 +20,6 @@ def test_coords_with_holes() -> None: ) -def test_hash() -> None: - polygon = geometry.Polygon([(0, 0), (1, 1), (1, 0), (0, 0)]) - - assert hash(polygon) == hash(geometry.Polygon([(0, 0), (1, 1), (1, 0), (0, 0)])) - - def test_geo_interface_shell_only() -> None: polygon = geometry.Polygon([(0, 0), (1, 1), (1, 0), (0, 0)])