Skip to content

Commit

Permalink
Remove hash method.
Browse files Browse the repository at this point in the history
it cannot be guaranteed that objects which compare equal have the same hash value
  • Loading branch information
cleder committed Nov 26, 2023
1 parent d36f958 commit 965f115
Show file tree
Hide file tree
Showing 11 changed files with 1 addition and 131 deletions.
5 changes: 0 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion docs/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 0 additions & 7 deletions pygeoif/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
36 changes: 0 additions & 36 deletions tests/test_geometrycollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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([]))
6 changes: 0 additions & 6 deletions tests/test_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 0 additions & 6 deletions tests/test_linear_ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
8 changes: 0 additions & 8 deletions tests/test_multiline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]])),
)
6 changes: 0 additions & 6 deletions tests/test_multipoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)])

Expand Down
38 changes: 0 additions & 38 deletions tests/test_multipolygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)),),
),
),
),
)
12 changes: 0 additions & 12 deletions tests/test_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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(())
6 changes: 0 additions & 6 deletions tests/test_polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)])

Expand Down

0 comments on commit 965f115

Please sign in to comment.