Skip to content

Commit d17cd18

Browse files
committed
remove conflicting iter len getitem methods
1 parent 11ff35b commit d17cd18

File tree

5 files changed

+92
-22
lines changed

5 files changed

+92
-22
lines changed

CHANGELOG.md

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,71 @@ Note: Minor version `0.X.0` update might break the API, It's recommended to pin
88

99
## [unreleased]
1010

11+
## [2.0.0] - TBD
12+
13+
* remove custom `__iter__`, `__getitem__` and `__len__` methods from `GeometryCollection` class **breaking change**
14+
15+
```python
16+
from geojson_pydantic.geometries import GeometryCollection, Point, MultiPoint
17+
18+
geoms = GeometryCollection(
19+
type="GeometryCollection",
20+
geometries=[
21+
Point(type="Point", coordinates=(102.0, 0.5)),
22+
MultiPoint(type="MultiPoint", coordinates=[(100.0, 0.0), (101.0, 1.0)]),
23+
],
24+
)
25+
26+
########
27+
# Before
28+
for geom in geom: # __iter__
29+
pass
30+
31+
assert len(geoms) == 2 # __len__
32+
33+
_ = geoms[0] # __getitem__
34+
35+
#####
36+
# Now
37+
for geom in geom.iter(): # __iter__
38+
pass
39+
40+
assert geoms.length == 2 # __len__
41+
42+
_ = geoms.geometries[0] # __getitem__
43+
```
44+
45+
* remove custom `__iter__`, `__getitem__` and `__len__` methods from `FeatureCollection` class **breaking change**
46+
47+
```python
48+
from geojson_pydantic import FeatureCollection, Feature, Point
49+
50+
fc = FeatureCollection(
51+
type="FeatureCollection", features=[
52+
Feature(type="Feature", geometry=Point(type="Point", coordinates=(102.0, 0.5)), properties={"name": "point1"}),
53+
Feature(type="Feature", geometry=Point(type="Point", coordinates=(102.0, 1.5)), properties={"name": "point2"}),
54+
]
55+
)
56+
57+
########
58+
# Before
59+
for feat in fc: # __iter__
60+
pass
61+
62+
assert len(fc) == 2 # __len__
63+
64+
_ = fc[0] # __getitem__
65+
66+
#####
67+
# Now
68+
for feat in fc.iter(): # __iter__
69+
pass
70+
71+
assert fc.length == 2 # __len__
72+
73+
_ = fe.features[0] # __getitem__
74+
```
75+
1176
## [1.2.0] - 2024-12-19
1277

1378
* drop python 3.8 support
@@ -376,7 +441,8 @@ Although the type file was added in `0.2.0` it wasn't included in the distribute
376441
### Added
377442
- Initial Release
378443

379-
[unreleased]: https://github.com/developmentseed/geojson-pydantic/compare/1.2.0...HEAD
444+
[unreleased]: https://github.com/developmentseed/geojson-pydantic/compare/2.0.0...HEAD
445+
[2.0.0]: https://github.com/developmentseed/geojson-pydantic/compare/1.2.0...2.0.0
380446
[1.2.0]: https://github.com/developmentseed/geojson-pydantic/compare/1.1.2...1.2.0
381447
[1.1.2]: https://github.com/developmentseed/geojson-pydantic/compare/1.1.1...1.1.2
382448
[1.1.1]: https://github.com/developmentseed/geojson-pydantic/compare/1.1.0...1.1.1

geojson_pydantic/features.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,11 @@ class FeatureCollection(_GeoJsonBase, Generic[Feat]):
3939
type: Literal["FeatureCollection"]
4040
features: List[Feat]
4141

42-
def __iter__(self) -> Iterator[Feat]: # type: ignore [override]
42+
def iter(self) -> Iterator[Feat]:
4343
"""iterate over features"""
4444
return iter(self.features)
4545

46-
def __len__(self) -> int:
46+
@property
47+
def length(self) -> int:
4748
"""return features length"""
4849
return len(self.features)
49-
50-
def __getitem__(self, index: int) -> Feat:
51-
"""get feature at a given index"""
52-
return self.features[index]

geojson_pydantic/geometries.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,18 +251,15 @@ class GeometryCollection(_GeoJsonBase):
251251
type: Literal["GeometryCollection"]
252252
geometries: List[Geometry]
253253

254-
def __iter__(self) -> Iterator[Geometry]: # type: ignore [override]
254+
def iter(self) -> Iterator[Geometry]:
255255
"""iterate over geometries"""
256256
return iter(self.geometries)
257257

258-
def __len__(self) -> int:
258+
@property
259+
def length(self) -> int:
259260
"""return geometries length"""
260261
return len(self.geometries)
261262

262-
def __getitem__(self, index: int) -> Geometry:
263-
"""get geometry at a given index"""
264-
return self.geometries[index]
265-
266263
@property
267264
def wkt(self) -> str:
268265
"""Return the Well Known Text representation."""

tests/test_features.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ def test_feature_collection_iteration():
9090
type="FeatureCollection", features=[test_feature, test_feature]
9191
)
9292
assert hasattr(gc, "__geo_interface__")
93-
iter(gc)
93+
assert list(iter(gc))
94+
assert len(list(gc.iter())) == 2
95+
assert dict(gc)
9496

9597

9698
def test_geometry_collection_iteration():
@@ -99,7 +101,9 @@ def test_geometry_collection_iteration():
99101
type="FeatureCollection", features=[test_feature_geometry_collection]
100102
)
101103
assert hasattr(gc, "__geo_interface__")
102-
iter(gc)
104+
assert list(iter(gc))
105+
assert len(list(gc.iter())) == 1
106+
assert dict(gc)
103107

104108

105109
def test_generic_properties_is_dict():
@@ -177,9 +181,11 @@ def test_feature_collection_generic():
177181
fc = FeatureCollection[Feature[Polygon, GenericProperties]](
178182
type="FeatureCollection", features=[test_feature, test_feature]
179183
)
180-
assert len(fc) == 2
181-
assert type(fc[0].properties) == GenericProperties
182-
assert type(fc[0].geometry) == Polygon
184+
assert fc.length == 2
185+
assert len(list(fc.iter())) == 2
186+
assert type(fc.features[0].properties) == GenericProperties
187+
assert type(fc.features[0].geometry) == Polygon
188+
assert dict(fc)
183189

184190

185191
def test_geo_interface_protocol():

tests/test_geometries.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,8 @@ def test_geometry_collection_iteration(coordinates):
498498
type="GeometryCollection", geometries=[polygon, multipolygon]
499499
)
500500
assert hasattr(gc, "__geo_interface__")
501-
iter(gc)
501+
assert len(list(gc.iter())) == 2
502+
assert list(iter(gc))
502503

503504

504505
@pytest.mark.parametrize(
@@ -511,7 +512,10 @@ def test_len_geometry_collection(coordinates):
511512
gc = GeometryCollection(
512513
type="GeometryCollection", geometries=[polygon, multipolygon]
513514
)
514-
assert len(gc) == 2
515+
assert gc.length == 2
516+
assert len(list(gc.iter())) == 2
517+
assert dict(gc)
518+
assert list(iter(gc))
515519

516520

517521
@pytest.mark.parametrize(
@@ -524,8 +528,8 @@ def test_getitem_geometry_collection(coordinates):
524528
gc = GeometryCollection(
525529
type="GeometryCollection", geometries=[polygon, multipolygon]
526530
)
527-
assert polygon == gc[0]
528-
assert multipolygon == gc[1]
531+
assert polygon == gc.geometries[0]
532+
assert multipolygon == gc.geometries[1]
529533

530534

531535
def test_wkt_mixed_geometry_collection():

0 commit comments

Comments
 (0)