Skip to content

Commit f083ee8

Browse files
authored
Merge pull request #359 from JamesParrott/pyshp-avoid-casting-to-geojson-TypedDict
Avoid one of the casts in Writer.shape coercion code
2 parents 19010df + aa8281d commit f083ee8

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/shapefile.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ def __repr__(self) -> str:
240240

241241
class HasGeoInterface(Protocol):
242242
@property
243-
def __geo_interface__(self) -> Any: ...
243+
def __geo_interface__(self) -> GeoJSONHomogeneousGeometryObject: ...
244244

245245

246246
class GeoJSONPoint(TypedDict):
@@ -3591,17 +3591,18 @@ def __dbfHeader(self) -> None:
35913591

35923592
def shape(
35933593
self,
3594-
s: Union[Shape, HasGeoInterface, dict[str, PointsT]],
3594+
s: Union[Shape, HasGeoInterface, GeoJSONHomogeneousGeometryObject],
35953595
) -> None:
35963596
# Balance if already not balanced
35973597
if self.autoBalance and self.recNum < self.shpNum:
35983598
self.balance()
35993599
# Check is shape or import from geojson
36003600
if not isinstance(s, Shape):
36013601
if hasattr(s, "__geo_interface__"):
3602-
shape_dict = cast(GeoJSONHomogeneousGeometryObject, s.__geo_interface__)
3603-
if isinstance(s, dict):
3604-
shape_dict = cast(GeoJSONHomogeneousGeometryObject, s)
3602+
s = cast(HasGeoInterface, s)
3603+
shape_dict = s.__geo_interface__
3604+
elif isinstance(s, dict): # TypedDict is a dict at runtime
3605+
shape_dict = s
36053606
else:
36063607
raise TypeError(
36073608
"Can only write Shape objects, GeoJSON dictionaries, "

0 commit comments

Comments
 (0)