Skip to content

[rasterio] Fix DatasetBase.crs and DatasetReaderBase.read overloads - #16588

Open
omkhpb25 wants to merge 2 commits into
python:mainfrom
omkhpb25:fix-rasterio-stubs
Open

omkhpb25 wants to merge 2 commits into
python:mainfrom
omkhpb25:fix-rasterio-stubs

Conversation

@omkhpb25

Copy link
Copy Markdown

Summary of Changes

  1. DatasetBase.crs: Updated the return type annotation in stubs/rasterio/rasterio/_base.pyi to CRS | None.
  2. DatasetReaderBase.read: Added @overload definitions in stubs/rasterio/rasterio/_io.pyi so that passing masked=True returns numpy.ma.MaskedArray.
  3. Formatting: Ran ruff formatting across the stubs/rasterio directory.

Verification

  • Passed repository structure check (python tests/check_typeshed_structure.py).
  • Passed formatting check (python -m ruff format --check stubs/rasterio).
  • Passed Mypy type tests across Python 3.12–3.15 (python tests/mypy_test.py stubs/rasterio).

@github-actions

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

sphinx (https://github.com/sphinx-doc/sphinx)
- sphinx/builders/_epub_base.py:34:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports

indexes: Indexes | None = None,
out: NDArray[Any] | None = None,
window: WindowInput | None = None,
masked: Literal[True] = True,

@thomas-maschler thomas-maschler Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The runtime default is False. Make masked a kwarg and don't pass a default to avoid matches with this overload in case users don't pass masked

Suggested change
masked: Literal[True] = True,
*,
masked: Literal[True],

fill_value: NumType | None = None,
out_dtype: DTypeLike | None = None,
) -> NDArray[Any]: ...
) -> NDArray[Any] | ma.MaskedArray[Any, Any]: ...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With masked=False results will always be a simple NDArray

Suggested change
) -> NDArray[Any] | ma.MaskedArray[Any, Any]: ...
) -> NDArray[Any]: ...

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.

2 participants