From aa8281d56bc3f029f21a6a386cf905f3c054ab89 Mon Sep 17 00:00:00 2001 From: James Parrott <80779630+JamesParrott@users.noreply.github.com> Date: Tue, 5 Aug 2025 13:09:19 +0100 Subject: [PATCH] Avoid one of the casts in Writer.shape coercion code --- src/shapefile.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/shapefile.py b/src/shapefile.py index 00578bb..5ec1077 100644 --- a/src/shapefile.py +++ b/src/shapefile.py @@ -240,7 +240,7 @@ def __repr__(self) -> str: class HasGeoInterface(Protocol): @property - def __geo_interface__(self) -> Any: ... + def __geo_interface__(self) -> GeoJSONHomogeneousGeometryObject: ... class GeoJSONPoint(TypedDict): @@ -3591,7 +3591,7 @@ def __dbfHeader(self) -> None: def shape( self, - s: Union[Shape, HasGeoInterface, dict[str, PointsT]], + s: Union[Shape, HasGeoInterface, GeoJSONHomogeneousGeometryObject], ) -> None: # Balance if already not balanced if self.autoBalance and self.recNum < self.shpNum: @@ -3599,9 +3599,10 @@ def shape( # Check is shape or import from geojson if not isinstance(s, Shape): if hasattr(s, "__geo_interface__"): - shape_dict = cast(GeoJSONHomogeneousGeometryObject, s.__geo_interface__) - if isinstance(s, dict): - shape_dict = cast(GeoJSONHomogeneousGeometryObject, s) + s = cast(HasGeoInterface, s) + shape_dict = s.__geo_interface__ + elif isinstance(s, dict): # TypedDict is a dict at runtime + shape_dict = s else: raise TypeError( "Can only write Shape objects, GeoJSON dictionaries, "