Repository navigation
[geojson] Fix coords return type and annotate the map_* callables - #16717
Open
Dhairya-007 wants to merge 1 commit into
Open
Dhairya-007 wants to merge 1 commit into
Dhairya-007 wants to merge 1 commit into
Conversation
`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.
Dhairya-007
force-pushed
the
geojson-utils-types
branch
from
November 6, 2026 16:06
fd2b2ea to
8f6d8f3
Compare
Contributor
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two problems in
geojson.utils.coordsyields whole coordinate tuples, not one-element onesIt was annotated
Generator[tuple[float]], but it yields each complete coordinate:So reading the latitude of a 2D coordinate was a type error:
Changed to
tuple[float, ...], since 2D and 3D coordinates are both possible.The
map_*callables wereIncompleteand partly wrongAll three took
Callable[[Incomplete], float | Geometry]. That return type is wrong formap_tuples, whose implementation callstuple(func(c))— so the callable has to return an iterable of floats. A coordinate-swapping function was rejected:Replaced the
Incompletecallables with what the implementations actually do:map_coordsappliesfuncto individual coordinate values —tuple_funccallsfunc(coord[0])andfunc(coord[1])— soCallable[[float], float]map_tuplesappliesfuncto a whole coordinate sequence and wraps the result intuple(...), soCallable[[Sequence[float]], Iterable[float]]map_geometriespasses each geometry throughfuncVerified against geojson 3.3.0 by introspecting the runtime behaviour, and the four errors above are gone under
mypy --strict.