Skip to content

Commit 13975d6

Browse files
author
Arthur Dujardin
committed
refactor: 🎨 Modify FeatureCollection generic
Modify the FeatureCollection generic to depends only on a generic Feature instead of a Geoemtry and Properties The FeatureCollection generic instantiation is changed from FeatureCollection[MyGeometry, MyProperties] to FeatureCollection[MyFeature]
1 parent d78b841 commit 13975d6

File tree

2 files changed

+16
-29
lines changed

2 files changed

+16
-29
lines changed

geojson_pydantic/features.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,25 @@ def set_geometry(cls, geometry: Any) -> Any:
3232
return geometry
3333

3434

35-
class FeatureCollection(BaseModel, Generic[Geom, Props], GeoInterfaceMixin):
35+
Feat = TypeVar("Feat", bound=Feature)
36+
37+
38+
class FeatureCollection(BaseModel, Generic[Feat], GeoInterfaceMixin):
3639
"""FeatureCollection Model"""
3740

3841
type: Literal["FeatureCollection"]
39-
features: List[Feature[Geom, Props]]
42+
features: List[Feat]
4043
bbox: Optional[BBox] = None
4144

42-
def __iter__(self) -> Iterator[Feature]: # type: ignore [override]
45+
def __iter__(self) -> Iterator[Feat]: # type: ignore [override]
4346
"""iterate over features"""
4447
return iter(self.features)
4548

4649
def __len__(self) -> int:
4750
"""return features length"""
4851
return len(self.features)
4952

50-
def __getitem__(self, index: int) -> Feature:
53+
def __getitem__(self, index: int) -> Feat:
5154
"""get feature at a given index"""
5255
return self.features[index]
5356

tests/test_features.py

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,14 @@ class GenericProperties(BaseModel):
7373

7474
def test_feature_collection_iteration():
7575
"""test if feature collection is iterable"""
76-
gc = FeatureCollection(
77-
type="FeatureCollection", features=[test_feature, test_feature]
78-
)
76+
gc = FeatureCollection(type="FeatureCollection", features=[test_feature, test_feature])
7977
assert hasattr(gc, "__geo_interface__")
8078
iter(gc)
8179

8280

8381
def test_geometry_collection_iteration():
8482
"""test if feature collection is iterable"""
85-
gc = FeatureCollection(
86-
type="FeatureCollection", features=[test_feature_geometry_collection]
87-
)
83+
gc = FeatureCollection(type="FeatureCollection", features=[test_feature_geometry_collection])
8884
assert hasattr(gc, "__geo_interface__")
8985
iter(gc)
9086

@@ -100,9 +96,7 @@ def test_generic_properties_is_dict():
10096
def test_generic_properties_is_dict_collection():
10197
feature = Feature(**test_feature_geometry_collection)
10298
assert hasattr(feature, "__geo_interface__")
103-
assert (
104-
feature.properties["id"] == test_feature_geometry_collection["properties"]["id"]
105-
)
99+
assert feature.properties["id"] == test_feature_geometry_collection["properties"]["id"]
106100
assert type(feature.properties) == dict
107101
assert not hasattr(feature.properties, "id")
108102

@@ -132,9 +126,7 @@ def test_generic_geometry():
132126

133127

134128
def test_generic_geometry_collection():
135-
feature = Feature[GeometryCollection, GenericProperties](
136-
**test_feature_geometry_collection
137-
)
129+
feature = Feature[GeometryCollection, GenericProperties](**test_feature_geometry_collection)
138130
assert feature.properties.id == test_feature_geometry_collection["properties"]["id"]
139131
assert type(feature.geometry) == GeometryCollection
140132
assert feature.geometry.wkt.startswith("GEOMETRYCOLLECTION (POLYGON ")
@@ -143,9 +135,7 @@ def test_generic_geometry_collection():
143135

144136
feature = Feature[GeometryCollection, Dict](**test_feature_geometry_collection)
145137
assert type(feature.geometry) == GeometryCollection
146-
assert (
147-
feature.properties["id"] == test_feature_geometry_collection["properties"]["id"]
148-
)
138+
assert feature.properties["id"] == test_feature_geometry_collection["properties"]["id"]
149139
assert type(feature.properties) == dict
150140
assert not hasattr(feature.properties, "id")
151141

@@ -155,13 +145,11 @@ def test_generic_geometry_collection():
155145

156146
def test_generic_properties_should_raise_for_string():
157147
with pytest.raises(ValidationError):
158-
Feature(
159-
**({"type": "Feature", "geometry": polygon, "properties": "should raise"})
160-
)
148+
Feature(**({"type": "Feature", "geometry": polygon, "properties": "should raise"}))
161149

162150

163151
def test_feature_collection_generic():
164-
fc = FeatureCollection[Polygon, GenericProperties](
152+
fc = FeatureCollection[Feature[Polygon, GenericProperties]](
165153
type="FeatureCollection", features=[test_feature, test_feature]
166154
)
167155
assert len(fc) == 2
@@ -231,12 +219,8 @@ def test_feature_validation():
231219
# missing geometry
232220
Feature(type="Feature", properties=None)
233221

234-
assert Feature(
235-
type="Feature", properties=None, bbox=(0, 0, 100, 100), geometry=None
236-
)
237-
assert Feature(
238-
type="Feature", properties=None, bbox=(0, 0, 0, 100, 100, 100), geometry=None
239-
)
222+
assert Feature(type="Feature", properties=None, bbox=(0, 0, 100, 100), geometry=None)
223+
assert Feature(type="Feature", properties=None, bbox=(0, 0, 0, 100, 100, 100), geometry=None)
240224

241225
with pytest.raises(ValidationError):
242226
# bad bbox2d

0 commit comments

Comments
 (0)