diff --git a/stubs/rasterio/@tests/test_cases/check_crs_input.py b/stubs/rasterio/@tests/test_cases/check_crs_input.py new file mode 100644 index 000000000000..474c927bf952 --- /dev/null +++ b/stubs/rasterio/@tests/test_cases/check_crs_input.py @@ -0,0 +1,23 @@ +from __future__ import annotations + +from typing_extensions import assert_type + +import rasterio.warp +from rasterio.crs import CRS +from rasterio.io import MemoryFile + + +class ForeignCRS: + def to_wkt(self) -> str: + return "" + + +assert_type(rasterio.warp.transform_bounds(4326, 3857, 0.0, 0.0, 1.0, 1.0), tuple[float, float, float, float]) +rasterio.warp.transform_bounds(ForeignCRS(), ForeignCRS(), 0.0, 0.0, 1.0, 1.0) +rasterio.warp.transform_bounds(CRS.from_epsg(4326), "EPSG:3857", 0.0, 0.0, 1.0, 1.0) +rasterio.warp.transform_bounds({"init": "EPSG:4326"}, CRS.from_epsg(3857), 0.0, 0.0, 1.0, 1.0) +rasterio.warp.transform_bounds(object(), 3857, 0.0, 0.0, 1.0, 1.0) # type: ignore +rasterio.warp.transform(4326, ForeignCRS(), [0.0], [0.0]) +rasterio.warp.calculate_default_transform(4326, ForeignCRS(), 10, 10, left=0.0, bottom=0.0, right=1.0, top=1.0) +MemoryFile().open(driver="GTiff", width=1, height=1, count=1, dtype="uint8", crs=4326) +MemoryFile().open(driver="GTiff", width=1, height=1, count=1, dtype="uint8", crs=ForeignCRS()) diff --git a/stubs/rasterio/rasterio/_typing.pyi b/stubs/rasterio/rasterio/_typing.pyi index 974afc3a8369..a1011123b397 100644 --- a/stubs/rasterio/rasterio/_typing.pyi +++ b/stubs/rasterio/rasterio/_typing.pyi @@ -16,13 +16,19 @@ class _SupportsGeoInterface(Protocol): @property def __geo_interface__(self) -> Mapping[str, Any]: ... +@type_check_only +class _SupportsToWkt(Protocol): + def to_wkt(self) -> str: ... + # A GeoJSON-like mapping, or any object exposing one through the # `__geo_interface__` protocol (e.g. shapely / geopandas geometries). # The runtime unwraps `__geo_interface__` before use, so both forms are # accepted anywhere a geometry is expected. Geometry: TypeAlias = Mapping[str, Any] | _SupportsGeoInterface # noqa: Y047 Colormap: TypeAlias = dict[int, tuple[int, int, int] | tuple[int, int, int, int]] -CRSInput: TypeAlias = str | dict[str, str] | CRS +# A WKT / PROJ string, an EPSG code, a PROJ dict, or any object exposing +# `to_wkt` (e.g. a pyproj CRS). `CRS.from_user_input` normalizes all of them. +CRSInput: TypeAlias = str | int | dict[str, str] | CRS | _SupportsToWkt FileOrBytes: TypeAlias = BinaryIO | bytes Indexes: TypeAlias = int | Sequence[int] NumType: TypeAlias = int | float