From 8f6d8f3f5a551eab7fe2001577032c418828bca3 Mon Sep 17 00:00:00 2001 From: dhairya05 Date: Fri, 6 Nov 2026 21:33:59 +0530 Subject: [PATCH] [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. --- stubs/geojson/geojson/utils.pyi | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/stubs/geojson/geojson/utils.pyi b/stubs/geojson/geojson/utils.pyi index 9e35c35bfc81..2a2da79d2701 100644 --- a/stubs/geojson/geojson/utils.pyi +++ b/stubs/geojson/geojson/utils.pyi @@ -1,14 +1,15 @@ -from _typeshed import Incomplete -from collections.abc import Callable, Generator +from collections.abc import Callable, Generator, Iterable, Sequence from typing import Any, Literal from geojson.base import GeoJSON from geojson.geometry import Geometry, LineString, Point, Polygon -def coords(obj: GeoJSON | dict[str, Any]) -> Generator[tuple[float]]: ... -def map_coords(func: Callable[[Incomplete], float | Geometry], obj: GeoJSON | dict[str, Any]) -> dict[str, Any]: ... -def map_tuples(func: Callable[[Incomplete], float | Geometry], obj: GeoJSON | dict[str, Any]) -> dict[str, Any]: ... -def map_geometries(func: Callable[[Incomplete], float | Geometry], obj: GeoJSON | dict[str, Any]) -> dict[str, Any]: ... +def coords(obj: GeoJSON | dict[str, Any]) -> Generator[tuple[float, ...]]: ... +def map_coords(func: Callable[[float], float], obj: GeoJSON | dict[str, Any]) -> dict[str, Any]: ... +def map_tuples(func: Callable[[Sequence[float]], Iterable[float]], obj: GeoJSON | dict[str, Any]) -> dict[str, Any]: ... +def map_geometries( + func: Callable[[Geometry | dict[str, Any]], Geometry | dict[str, Any]], obj: GeoJSON | dict[str, Any] +) -> dict[str, Any]: ... def generate_random( featureType: Literal["Point", "LineString", "Polygon"], numberVertices: int = 3,