Skip to content

Commit

Permalink
Issue 1487 - Suggested Fix - Return false when comparing Object again…
Browse files Browse the repository at this point in the history
…st values that can't be unpacked (#1488)

* Add test coverage to show intended behaviour of Object eq, with xfail for situations throwing TypeErrors

* Add check to Object eq to ensure only objects that can be unpacked are explicitly checked for equality, otherwise assume not equal

---------

Co-authored-by: Alex Ware <[email protected]>
  • Loading branch information
Alex and Alex Ware authored Feb 20, 2025
1 parent 74936d0 commit ec9768a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
5 changes: 4 additions & 1 deletion fiona/model.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Fiona data model"""

from binascii import hexlify
from collections.abc import MutableMapping
from collections.abc import Mapping, MutableMapping
from enum import Enum
import itertools
from json import JSONEncoder
Expand Down Expand Up @@ -187,6 +187,9 @@ def __delitem__(self, key):
del self._data[key]

def __eq__(self, other):
if not isinstance(other, Mapping):
return False

return dict(**self) == dict(**other)


Expand Down
21 changes: 21 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test of deprecations following RFC 1"""
from collections import OrderedDict

import pytest

Expand Down Expand Up @@ -102,6 +103,26 @@ def __init__(self, value=None, **data):
assert thing["value"] is None


@pytest.mark.parametrize(
"other",
({"g": 1}, Object(g=1), OrderedDict(g=1)),
)
def test_object_eq(other):
"""Object eq identifies a match"""
obj = Object(g=1)
assert obj == other


@pytest.mark.parametrize(
"other",
("", 1, [1, 2], {"g": 5}, Object(g=10), OrderedDict(a=1)),
)
def test_object_eq_not(other):
"""Object eq identifies not a match"""
obj = Object(g=1)
assert obj != other


def test__geometry_ctor():
"""Construction of a _Geometry works"""
geom = _Geometry(type="Point", coordinates=(0, 0))
Expand Down

0 comments on commit ec9768a

Please sign in to comment.