nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: Toblerity/Fiona
Pull request #1488 (2025-02-20 04:53:54)

Issue 1487 - Suggested Fix - Return false when comparing Object against values that can't be unpacked 
This is a suggested fix for [Issue 1847](https://github.com/Toblerity/Fiona/issues/1487)

In cases where eq is used for an object that cannot be unpacked (i.e. is not an instance of Mapping) simply default to returning false.

Depending on style/preference, another way this could be implemented is:
```python
try:
    return dict(**self) == dict(**other)
except TypeError:
    return False
```
I went with the Mapping approach as it seemed to tie in more nicely with the inheritance from MutableMapping for Object, but both are pythonic and I'm happy to switch the fix over if you have a preference.

I made the assumption that the decision to do equality explicitly against the unpacked versions of the objects was in part to maintain backwards compatibility with old style Fiona representations of geometries (or similar). So I tried to stay away from anything that would create more direct comparisons between object types (e.g. return false if it's not a dict/Object), leaving the option open for other Mapping based objects to also resolve to True when compared.


Patch:
diff --git a/fiona/model.py b/fiona/model.py
index 30f4f18..0bcca78 100644
--- a/fiona/model.py
+++ b/fiona/model.py
@@ -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
@@ -187,6 +187,9 @@ class Object(MutableMapping):
             del self._data[key]
 
     def __eq__(self, other):
+        if not isinstance(other, Mapping):
+            return False
+
         return dict(**self) == dict(**other)
 
 


Test patch:
diff --git a/tests/test_model.py b/tests/test_model.py
index 2228cc4..dd290d4 100644
--- a/tests/test_model.py
+++ b/tests/test_model.py
@@ -1,4 +1,5 @@
 """Test of deprecations following RFC 1"""
+from collections import OrderedDict
 
 import pytest
 
@@ -102,6 +103,26 @@ def test_object_delitem_delegated():
     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))
Repository: Toblerity/Shapely
Pull request #355 (2016-01-11 23:48:40)

Fix for equals comparison with empty polygons
This PR fixes a bug triggered when comparing the equality of empty Polygons. The accompanying test illustrates the issue. The problem is that an empty polygon doesn't have any coordinates, so `self.exterior.coords` in the existing algorithm falls over.


Patch:
diff --git a/shapely/geometry/polygon.py b/shapely/geometry/polygon.py
index 945a108..91f33fc 100644
--- a/shapely/geometry/polygon.py
+++ b/shapely/geometry/polygon.py
@@ -251,6 +251,11 @@ class Polygon(BaseGeometry):
     def __eq__(self, other):
         if not isinstance(other, Polygon):
             return False
+        check_empty = (self.is_empty, other.is_empty)
+        if all(check_empty):
+            return True
+        elif any(check_empty):
+            return False
         my_coords = [
             tuple(self.exterior.coords),
             [tuple(interior.coords) for interior in self.interiors]


Test patch:
diff --git a/tests/test_polygon.py b/tests/test_polygon.py
index 2d07a3a..84532b1 100644
--- a/tests/test_polygon.py
+++ b/tests/test_polygon.py
@@ -210,6 +210,21 @@ class PolygonTestCase(unittest.TestCase):
         ec = list(p.buffer(1).boundary.coords)
         self.assertIsInstance(ec, list)  # TODO: this is a poor test
 
+    def test_empty_equality(self):
+        # Test equals operator, including empty geometries
+        # see issue #338
+
+        point1 = Point(0, 0)
+        polygon1 = Polygon(((0.0, 0.0), (0.0, 1.0), (-1.0, 1.0), (-1.0, 0.0)))
+        polygon2 = Polygon(((0.0, 0.0), (0.0, 1.0), (-1.0, 1.0), (-1.0, 0.0)))
+        polygon_empty1 = Polygon()
+        polygon_empty2 = Polygon()
+
+        self.assertNotEqual(point1, polygon1)
+        self.assertEqual(polygon_empty1, polygon_empty2)
+        self.assertNotEqual(polygon1, polygon_empty1)
+        self.assertEqual(polygon1, polygon2)
+        self.assertNotEqual(None, polygon_empty1)
 
 def test_suite():
     return unittest.TestLoader().loadTestsFromTestCase(PolygonTestCase)
Repository: Toblerity/Shapely
Pull request #392 (2016-07-18 21:36:06)

Polygon.from_bounds()
Ready for review.

To support patterns like:

```python
import fiona
from shapely.geometry import Polygon

with fiona.open('input.shp') as src:
    bounds = Polygon.from_bounds(*src.bounds)
```


Patch:
diff --git a/shapely/geometry/polygon.py b/shapely/geometry/polygon.py
index 91f33fc..6e99bab 100644
--- a/shapely/geometry/polygon.py
+++ b/shapely/geometry/polygon.py
@@ -333,6 +333,15 @@ class Polygon(BaseGeometry):
             'stroke-width="{0}" opacity="0.6" d="{1}" />'
             ).format(2. * scale_factor, path, fill_color)
 
+    @classmethod
+    def from_bounds(cls, xmin, ymin, xmax, ymax):
+        """Construct a `Polygon()` from spatial bounds."""
+        return cls([
+            (xmin, ymin),
+            (xmin, ymax),
+            (xmax, ymax),
+            (xmax, ymin)])
+
 
 class PolygonAdapter(PolygonProxy, Polygon):
 


Test patch:
diff --git a/tests/test_polygon.py b/tests/test_polygon.py
index 84532b1..cecb616 100644
--- a/tests/test_polygon.py
+++ b/tests/test_polygon.py
@@ -226,5 +226,17 @@ class PolygonTestCase(unittest.TestCase):
         self.assertEqual(polygon1, polygon2)
         self.assertNotEqual(None, polygon_empty1)
 
+    def test_from_bounds(self):
+        xmin, ymin, xmax, ymax = -180, -90, 180, 90
+        coords = [
+            (xmin, ymin),
+            (xmin, ymax),
+            (xmax, ymax),
+            (xmax, ymin)]
+        self.assertEqual(
+            Polygon(coords),
+            Polygon.from_bounds(xmin, ymin, xmax, ymax))
+
+
 def test_suite():
     return unittest.TestLoader().loadTestsFromTestCase(PolygonTestCase)
Repository: Toblerity/Shapely
Pull request #514 (2017-08-29 04:51:37)

Add EmptyGeometry class
This provides an explicit means of creating an empty geometry object. Motivated by desire for a clear empty geometry type to use in na operations in geopandas. Thanks @sgillies for laying this out in #513, and @jorisvandenbossche for motivation in #510.

I went with `EmptyGeometry`, rather than `Empty`, thinking it made the context slightly clearer when used outside of `shapely`. I created an `__init__` mainly for the docstring, while retaining the `BaseGeometry` docstring for the class; maybe that's not necessary though?


Patch:
diff --git a/shapely/geometry/base.py b/shapely/geometry/base.py
index 7f64c78..202d923 100644
--- a/shapely/geometry/base.py
+++ b/shapely/geometry/base.py
@@ -943,6 +943,12 @@ class HeterogeneousGeometrySequence(GeometrySequence):
         return g
 
 
+class EmptyGeometry(BaseGeometry):
+    def __init__(self):
+        """Create an empty geometry."""
+        BaseGeometry.__init__(self)
+
+
 def _test():
     """Test runner"""
     import doctest


Test patch:
diff --git a/tests/test_emptiness.py b/tests/test_emptiness.py
index 541224e..91d5ec2 100644
--- a/tests/test_emptiness.py
+++ b/tests/test_emptiness.py
@@ -1,5 +1,5 @@
 from . import unittest
-from shapely.geometry.base import BaseGeometry
+from shapely.geometry.base import BaseGeometry, EmptyGeometry
 import shapely.geometry as sgeom
 from shapely.geometry.polygon import LinearRing
 
@@ -7,6 +7,10 @@ empty_generator = lambda: iter([])
 
 class EmptinessTestCase(unittest.TestCase):
 
+    def test_empty_class(self):
+        g = EmptyGeometry()
+        self.assertTrue(g._is_empty)
+
     def test_empty_base(self):
         g = BaseGeometry()
         self.assertTrue(g._is_empty)