Skip to content

[geojson] Fix coords return type and annotate the map_* callables - #16717

Open
Dhairya-007 wants to merge 1 commit into
python:mainfrom
Dhairya-007:geojson-utils-types
Open

Dhairya-007 wants to merge 1 commit into
python:mainfrom
Dhairya-007:geojson-utils-types

Conversation

@Dhairya-007

Copy link
Copy Markdown
Contributor

Two problems in geojson.utils.

coords yields whole coordinate tuples, not one-element ones

It was annotated Generator[tuple[float]], but it yields each complete coordinate:

>>> 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)]

So reading the latitude of a 2D coordinate was a type error:

for c in coords(line_string):
    x, y = c[0], c[1]   # error: Tuple index out of range  [misc]

Changed to tuple[float, ...], since 2D and 3D coordinates are both possible.

The map_* callables were Incomplete and partly wrong

All three took Callable[[Incomplete], float | Geometry]. That return type is wrong for map_tuples, whose implementation calls tuple(func(c)) — so the callable has to return an iterable of floats. A coordinate-swapping function was rejected:

map_tuples(lambda t: [t[1], t[0]], line_string)
# error: Argument 1 to "map_tuples" has incompatible type ...; expected "Callable[[Any], float | Geometry]"

Replaced the Incomplete callables with what the implementations actually do:

  • map_coords applies func to individual coordinate values — tuple_func calls func(coord[0]) and func(coord[1]) — so Callable[[float], float]
  • map_tuples applies func to a whole coordinate sequence and wraps the result in tuple(...), so Callable[[Sequence[float]], Iterable[float]]
  • map_geometries passes each geometry through func

Verified against geojson 3.3.0 by introspecting the runtime behaviour, and the four errors above are gone under mypy --strict.

`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.
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant