Skip to content

Commit fd2b2ea

Browse files
committed
[geojson] Fix coords return type and annotate map_* callables
`coords` was annotated `Generator[tuple[float]]`, a one-element tuple, but it yields whole coordinate tuples: >>> list(coords(LineString([(-115.81, 37.24), (-120.5, 40.1)]))) [(-115.81, 37.24), (-120.5, 40.1)] >>> list(coords(Point((1.0, 2.0, 3.0)))) [(1.0, 2.0, 3.0)] Reading the second element of a 2D coordinate therefore failed with "Tuple index out of range". Changed to `tuple[float, ...]`. The three `map_*` functions took `Callable[[Incomplete], float | Geometry]`, which is wrong for `map_tuples`: the implementation calls `tuple(func(c))`, so the callable must return an iterable of floats, and a coordinate-swapping function was rejected. Replaced the `Incomplete` callables with the types the implementations actually use: - `map_coords` applies `func` to individual coordinate values (`tuple_func` calls `func(coord[0])`, `func(coord[1])`) - `map_tuples` applies `func` to a whole coordinate sequence and wraps the result with `tuple(...)` - `map_geometries` passes each geometry through `func` Verified against geojson 3.3.0 at runtime, and with mypy --strict.
1 parent ffcf406 commit fd2b2ea

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

‎stubs/geojson/geojson/utils.pyi‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
from _typeshed import Incomplete
2-
from collections.abc import Callable, Generator
1+
from collections.abc import Callable, Generator, Iterable, Sequence
32
from typing import Any, Literal
43

54
from geojson.base import GeoJSON
65
from geojson.geometry import Geometry, LineString, Point, Polygon
76

8-
def coords(obj: GeoJSON | dict[str, Any]) -> Generator[tuple[float]]: ...
9-
def map_coords(func: Callable[[Incomplete], float | Geometry], obj: GeoJSON | dict[str, Any]) -> dict[str, Any]: ...
10-
def map_tuples(func: Callable[[Incomplete], float | Geometry], obj: GeoJSON | dict[str, Any]) -> dict[str, Any]: ...
11-
def map_geometries(func: Callable[[Incomplete], float | Geometry], obj: GeoJSON | dict[str, Any]) -> dict[str, Any]: ...
7+
def coords(obj: GeoJSON | dict[str, Any]) -> Generator[tuple[float, ...]]: ...
8+
def map_coords(func: Callable[[float], float], obj: GeoJSON | dict[str, Any]) -> dict[str, Any]: ...
9+
def map_tuples(func: Callable[[Sequence[float]], Iterable[float]], obj: GeoJSON | dict[str, Any]) -> dict[str, Any]: ...
10+
def map_geometries(
11+
func: Callable[[Geometry | dict[str, Any]], Geometry | dict[str, Any]], obj: GeoJSON | dict[str, Any]
12+
) -> dict[str, Any]: ...
1213
def generate_random(
1314
featureType: Literal["Point", "LineString", "Polygon"],
1415
numberVertices: int = 3,

0 commit comments

Comments
 (0)