-
-
Notifications
You must be signed in to change notification settings - Fork 2k
[rasterio] Add stubs for rasterio 1.5 #15884
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
thomas-maschler
wants to merge
13
commits into
python:main
Choose a base branch
from
thomas-maschler:add-rasterio-stubs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,222
−0
Open
Changes from 7 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0531100
[rasterio] Add stubs for rasterio 1.5
thomas-maschler e703f5d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7d26558
[rasterio] Drop apt/brew dependencies and prune comments
thomas-maschler 50acf5d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 46cb665
[rasterio] Fix pyright testcase failures
thomas-maschler 0f35ce5
[rasterio] Annotate _parse_path return for stricter pyright
thomas-maschler 2cc3635
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 240747c
[rasterio] Remove @tests/test_cases
thomas-maschler a4a4d06
[rasterio] Drop partial-stub flags, comment allowlist, fill missing API
thomas-maschler 27178f7
[rasterio] Narrow Any usages with TypeAliases, TypeVars, and object
thomas-maschler 0aea5e0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f2c60bb
Merge branch 'main' into add-rasterio-stubs
thomas-maschler 89e8c3f
Merge branch 'main' into add-rasterio-stubs
thomas-maschler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| rasterio\._typing | ||
| rasterio\._affine_types | ||
| rasterio\.features\.Geometry | ||
| rasterio\.merge\.MethodFunction | ||
|
|
||
| rasterio\._base.* | ||
| rasterio\._io.* | ||
| rasterio\._env.* | ||
| rasterio\._err.* | ||
| rasterio\._features.* | ||
| rasterio\._transform.* | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| """Verify `rasterio.crs.CRS` factory return types and arg validation.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing_extensions import assert_type | ||
|
|
||
| from rasterio.crs import CRS | ||
|
|
||
| # All factory staticmethods return `CRS`. | ||
| assert_type(CRS.from_epsg(4326), CRS) | ||
| assert_type(CRS.from_string("EPSG:4326"), CRS) | ||
| assert_type(CRS.from_wkt("..."), CRS) | ||
| assert_type(CRS.from_authority("EPSG", 4326), CRS) | ||
| assert_type(CRS.from_user_input(4326), CRS) | ||
|
|
||
| # from_epsg expects int | str, not float. | ||
| CRS.from_epsg(4326.0) # type: ignore[arg-type] |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| """Verify the inferred property types on a `DatasetReader`.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Any | ||
| from typing_extensions import assert_type | ||
|
|
||
| from numpy.typing import NDArray | ||
| from rasterio.coords import BoundingBox | ||
| from rasterio.crs import CRS | ||
| from rasterio.io import DatasetReader | ||
| from rasterio.profiles import Profile | ||
|
|
||
|
|
||
| def check_props(reader: DatasetReader) -> None: | ||
| assert_type(reader.crs, CRS) | ||
| assert_type(reader.bounds, BoundingBox) | ||
| assert_type(reader.profile, Profile) | ||
| assert_type(reader.count, int) | ||
| assert_type(reader.width, int) | ||
| assert_type(reader.height, int) | ||
| assert_type(reader.shape, tuple[int, int]) | ||
| assert_type(reader.dtypes, tuple[str, ...]) | ||
| assert_type(reader.indexes, tuple[int, ...]) | ||
| assert_type(reader.nodata, float | None) | ||
| assert_type(reader.read(1), NDArray[Any]) | ||
| assert_type(reader.read_crs(), CRS | None) | ||
| assert_type(reader.tags(), dict[str, str]) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| """Verify `rasterio.open` overloads route to the right return type.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing_extensions import assert_type | ||
|
|
||
| import rasterio | ||
| from rasterio.io import DatasetReader, DatasetWriter | ||
|
|
||
| # Default mode is "r" → DatasetReader | ||
| assert_type(rasterio.open("foo.tif"), DatasetReader) | ||
| assert_type(rasterio.open("foo.tif", "r"), DatasetReader) | ||
|
|
||
| # Write/update modes → DatasetWriter | ||
| assert_type(rasterio.open("foo.tif", "w", driver="GTiff"), DatasetWriter) | ||
| assert_type(rasterio.open("foo.tif", "r+"), DatasetWriter) | ||
| assert_type(rasterio.open("foo.tif", "w+"), DatasetWriter) | ||
|
|
||
|
|
||
| def check_opaque_mode(mode: str) -> None: | ||
| assert_type(rasterio.open("foo.tif", mode), DatasetReader | DatasetWriter) | ||
|
|
||
|
|
||
| rasterio.open(123) # type: ignore[call-overload] |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| """Verify the deprecated-overload routing on `rasterio.warp.transform_geom`.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from rasterio.crs import CRS | ||
| from rasterio.warp import transform_geom | ||
|
|
||
| src = CRS.from_epsg(4326) | ||
| dst = CRS.from_epsg(3857) | ||
| geom: dict[str, object] = {"type": "Point", "coordinates": [0.0, 0.0]} | ||
|
|
||
| transform_geom(src, dst, geom) | ||
| transform_geom(src, dst, geom, precision=2) | ||
|
|
||
| transform_geom(src, dst, geom, antimeridian_cutting=True) # pyright: ignore[reportDeprecated] | ||
| transform_geom(src, dst, geom, antimeridian_offset=10.0) # pyright: ignore[reportDeprecated] |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| """Verify `rasterio.windows.Window` constructors and the from_bounds overload.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing_extensions import assert_type | ||
|
|
||
| from rasterio.transform import from_origin | ||
| from rasterio.windows import Window, from_bounds | ||
|
|
||
| # Constructors. | ||
| w = Window(0, 0, 10, 10) | ||
| assert_type(w, Window) | ||
| assert_type(Window.from_slices(slice(0, 5), slice(0, 5)), Window) | ||
| assert_type(w.intersection(w), Window) | ||
| assert_type(w.todict(), dict[str, float]) | ||
|
|
||
| # from_bounds — modern overload (no deprecated kwargs). | ||
| t = from_origin(0.0, 0.0, 1.0, 1.0) | ||
| assert_type(from_bounds(0, 0, 1, 1, t), Window) | ||
|
|
||
| # Negative widths are accepted at the type level; runtime validates. | ||
| assert_type(Window(0, 0, -1, -1), Window) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| version = "1.5.*" | ||
| upstream-repository = "https://github.com/rasterio/rasterio" | ||
| requires-python = ">=3.12" | ||
| dependencies = ["numpy>=2", "click>=8"] | ||
| partial-stub = true | ||
|
|
||
| [tool.stubtest] | ||
| ignore-missing-stub = true | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are these two options really necessary? Are really modules (that are not listed in the allowlist) missing?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i removed those flags added missing stubs |
||
| stubtest-dependencies = ["rasterio==1.5.*"] | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| import logging | ||
| import os | ||
| from collections.abc import Callable, Sequence | ||
| from typing import Any, Final, Literal, NamedTuple, TypeAlias, overload | ||
|
|
||
| from numpy.typing import DTypeLike, NDArray | ||
| from rasterio._base import DatasetBase as DatasetBase | ||
| from rasterio._io import Statistics as Statistics | ||
| from rasterio._path import _parse_path as _parse_path, _UnparsedPath as _UnparsedPath | ||
| from rasterio._show_versions import show_versions as show_versions | ||
| from rasterio._typing import AnyDataset, CRSInput | ||
| from rasterio._version import ( | ||
| gdal_version as gdal_version, | ||
| get_geos_version as get_geos_version, | ||
| get_proj_version as get_proj_version, | ||
| ) | ||
| from rasterio._vsiopener import _opener_registration as _opener_registration | ||
| from rasterio.crs import CRS as CRS | ||
| from rasterio.drivers import driver_from_extension as driver_from_extension, is_blacklisted as is_blacklisted | ||
| from rasterio.dtypes import ( | ||
| bool_ as bool_, | ||
| check_dtype as check_dtype, | ||
| complex_ as complex_, | ||
| complex_int16 as complex_int16, | ||
| float16 as float16, | ||
| float32 as float32, | ||
| float64 as float64, | ||
| int8 as int8, | ||
| int16 as int16, | ||
| int32 as int32, | ||
| int64 as int64, | ||
| sbyte as sbyte, | ||
| ubyte as ubyte, | ||
| uint8 as uint8, | ||
| uint16 as uint16, | ||
| uint32 as uint32, | ||
| uint64 as uint64, | ||
| ) | ||
| from rasterio.env import Env as Env, ensure_env_with_credentials as ensure_env_with_credentials | ||
| from rasterio.errors import ( | ||
| DriverCapabilityError as DriverCapabilityError, | ||
| RasterioDeprecationWarning as RasterioDeprecationWarning, | ||
| RasterioIOError as RasterioIOError, | ||
| ) | ||
| from rasterio.io import ( | ||
| BufferedDatasetWriter as BufferedDatasetWriter, | ||
| DatasetReader as DatasetReader, | ||
| DatasetWriter as DatasetWriter, | ||
| FilePath as FilePath, | ||
| MemoryFile as MemoryFile, | ||
| get_writer_for_driver as get_writer_for_driver, | ||
| get_writer_for_path as get_writer_for_path, | ||
| ) | ||
| from rasterio.profiles import default_gtiff_profile as default_gtiff_profile | ||
| from rasterio.transform import Affine as Affine, guard_transform as guard_transform | ||
|
|
||
| __all__ = ["CRS", "Band", "Env", "band", "open", "pad"] | ||
|
|
||
| __version__: Final[str] | ||
| __gdal_version__: Final[str] | ||
| __proj_version__: Final[str] | ||
| __geos_version__: Final[str] | ||
|
|
||
| have_vsi_plugin: Final[bool] | ||
| log: logging.Logger | ||
|
|
||
| _Fp: TypeAlias = str | os.PathLike[str] | MemoryFile | FilePath | ||
|
|
||
| @overload | ||
| def open( | ||
| fp: _Fp, | ||
| mode: Literal["r"] = "r", | ||
| driver: str | Sequence[str] | None = None, | ||
| width: int | None = None, | ||
| height: int | None = None, | ||
| count: int | None = None, | ||
| crs: CRSInput | None = None, | ||
| transform: Affine | None = None, | ||
| dtype: DTypeLike | None = None, | ||
| nodata: float | None = None, | ||
| sharing: bool = False, | ||
| thread_safe: bool = False, | ||
| opener: Callable[..., Any] | None = None, | ||
| **kwargs: Any, | ||
| ) -> DatasetReader: ... | ||
| @overload | ||
| def open( | ||
| fp: _Fp, | ||
| mode: Literal["r+", "w", "w+"], | ||
| driver: str | Sequence[str] | None = None, | ||
| width: int | None = None, | ||
| height: int | None = None, | ||
| count: int | None = None, | ||
| crs: CRSInput | None = None, | ||
| transform: Affine | None = None, | ||
| dtype: DTypeLike | None = None, | ||
| nodata: float | None = None, | ||
| sharing: bool = False, | ||
| thread_safe: bool = False, | ||
| opener: Callable[..., Any] | None = None, | ||
| **kwargs: Any, | ||
| ) -> DatasetWriter: ... | ||
| @overload | ||
| def open( | ||
| fp: _Fp, | ||
| mode: str = "r", | ||
| driver: str | Sequence[str] | None = None, | ||
| width: int | None = None, | ||
| height: int | None = None, | ||
| count: int | None = None, | ||
| crs: CRSInput | None = None, | ||
| transform: Affine | None = None, | ||
| dtype: DTypeLike | None = None, | ||
| nodata: float | None = None, | ||
| sharing: bool = False, | ||
| thread_safe: bool = False, | ||
| opener: Callable[..., Any] | None = None, | ||
| **kwargs: Any, | ||
| ) -> DatasetReader | DatasetWriter: ... | ||
|
|
||
| class Band(NamedTuple): | ||
| ds: AnyDataset | ||
| bidx: int | Sequence[int] | ||
| dtype: str | ||
| shape: tuple[int, ...] | ||
|
|
||
| def band(ds: AnyDataset, bidx: int | Sequence[int]) -> Band: ... | ||
| def pad( | ||
| array: NDArray[Any], transform: Affine, pad_width: int, mode: str | Callable[..., Any] | None = None, **kwargs: Any | ||
| ) -> tuple[NDArray[Any], Affine]: ... |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # Swap to `from affine import Affine as Affine` once affine ships `py.typed` (v3). | ||
| from typing import Any, TypeAlias | ||
|
|
||
| Affine: TypeAlias = Any |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should be comments explaining why these entries are on the allowlist.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added comments