Skip to content

Commit f89b588

Browse files
Merge pull request #138 from arthurdjn/featureCollectionGeneric
Feature collection generic
2 parents 210f54b + b74392f commit f89b588

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ Note: Minor version `0.X.0` update might break the API, It's recommanded to pin
1818

1919
### Changed
2020

21+
* update pydantic `FeatureCollection` generic model to allow named features in the generated schemas.
22+
23+
```python
24+
# before
25+
FeatureCollection[Geometry, Properties]
26+
27+
# now
28+
FeatureCollection[Feature[Geometry, Properties]]
29+
```
30+
2131
* update pydantic requirement to `~=2.0`
2232

2333
* raise `ValueError` in `geomtries.parse_geometry_obj` instead of `ValidationError`

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def test_generic_properties_should_raise_for_string():
173173

174174

175175
def test_feature_collection_generic():
176-
fc = FeatureCollection[Polygon, GenericProperties](
176+
fc = FeatureCollection[Feature[Polygon, GenericProperties]](
177177
type="FeatureCollection", features=[test_feature, test_feature]
178178
)
179179
assert len(fc) == 2

0 commit comments

Comments
 (0)