Skip to content

Commit a53e15b

Browse files
committed
Type hint _from_geojson with more judicious uses of cast()
1 parent e6db5fd commit a53e15b

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/shapefile.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,7 @@ def __geo_interface__(self) -> GeoJSONHomogeneousGeometryObject:
989989
)
990990

991991
@staticmethod
992-
def _from_geojson(geoj) -> Shape:
992+
def _from_geojson(geoj: GeoJSONHomogeneousGeometryObject) -> Shape:
993993
# create empty shape
994994
# set shapeType
995995
geojType = geoj["type"] if geoj else "Null"
@@ -998,18 +998,26 @@ def _from_geojson(geoj) -> Shape:
998998
else:
999999
raise GeoJSON_Error(f"Cannot create Shape from GeoJSON type '{geojType}'")
10001000

1001+
coordinates = geoj["coordinates"]
1002+
1003+
if coordinates == ():
1004+
raise GeoJSON_Error(f"Cannot create non-Null Shape from: {coordinates=}")
1005+
1006+
points: PointsT
1007+
parts: list[int]
1008+
10011009
# set points and parts
10021010
if geojType == "Point":
1003-
points = [geoj["coordinates"]]
1011+
points = [cast(PointT, coordinates)]
10041012
parts = [0]
10051013
elif geojType in ("MultiPoint", "LineString"):
1006-
points = geoj["coordinates"]
1014+
points = cast(PointsT, coordinates)
10071015
parts = [0]
10081016
elif geojType == "Polygon":
10091017
points = []
10101018
parts = []
10111019
index = 0
1012-
for i, ext_or_hole in enumerate(geoj["coordinates"]):
1020+
for i, ext_or_hole in enumerate(cast(list[PointsT], coordinates)):
10131021
# although the latest GeoJSON spec states that exterior rings should have
10141022
# counter-clockwise orientation, we explicitly check orientation since older
10151023
# GeoJSONs might not enforce this.
@@ -1026,15 +1034,15 @@ def _from_geojson(geoj) -> Shape:
10261034
points = []
10271035
parts = []
10281036
index = 0
1029-
for linestring in geoj["coordinates"]:
1037+
for linestring in cast(list[PointsT], coordinates):
10301038
points.extend(linestring)
10311039
parts.append(index)
10321040
index += len(linestring)
10331041
elif geojType == "MultiPolygon":
10341042
points = []
10351043
parts = []
10361044
index = 0
1037-
for polygon in geoj["coordinates"]:
1045+
for polygon in cast(list[list[PointsT]], coordinates):
10381046
for i, ext_or_hole in enumerate(polygon):
10391047
# although the latest GeoJSON spec states that exterior rings should have
10401048
# counter-clockwise orientation, we explicitly check orientation since older
@@ -3591,9 +3599,9 @@ def shape(
35913599
# Check is shape or import from geojson
35923600
if not isinstance(s, Shape):
35933601
if hasattr(s, "__geo_interface__"):
3594-
shape_dict = cast(dict, s.__geo_interface__)
3602+
shape_dict = cast(GeoJSONHomogeneousGeometryObject, s.__geo_interface__)
35953603
if isinstance(s, dict):
3596-
shape_dict = s
3604+
shape_dict = cast(GeoJSONHomogeneousGeometryObject, s)
35973605
else:
35983606
raise TypeError(
35993607
"Can only write Shape objects, GeoJSON dictionaries, "

0 commit comments

Comments
 (0)