Skip to content

BUG: Use @ instead of * for Affine multiplication (#937) - #941

Closed
VolodymyrLinuxovich wants to merge 1 commit into
corteva:masterfrom
VolodymyrLinuxovich:fix-937-affine-matmul
Closed

VolodymyrLinuxovich wants to merge 1 commit into
corteva:masterfrom
VolodymyrLinuxovich:fix-937-affine-matmul

Conversation

@VolodymyrLinuxovich

Copy link
Copy Markdown

Problem

affine 3.0.1 (2026-08-28) emits PendingDeprecationWarning: Use @ matmul instead of * mul operator for matrix multiplication whenever * is used, both for composing transforms and for applying a transform to coordinates. open_rasterio triggers it through affine_to_coords, and rio.transform(recalc=True) triggers it through XRasterBase.transform.

Change

  • rioxarray/_spatial_utils.py and rioxarray/rioxarray.py: replace the 5 Affine * multiplications with @. Running the test suite with that warning turned into an error finds no other call sites in rioxarray.
  • pyproject.toml: add affine>=3.0. @ was added in affine 3.0, and rasterio 1.4.3 still accepts affine 2.x. rioxarray already imports affine directly in several modules, so this also declares a dependency it was relying on implicitly. If you'd rather keep supporting affine 2.x, I can switch to a version-agnostic approach.
  • affine_to_coords wraps its return values in numpy.asarray (a no-op at runtime). This is needed because mypy picks a float return type from affine's __matmul__ overloads, which would otherwise add two new dict-item errors.

Some warnings remain from inside rasterio itself (rasterio.windows, rasterio.transform, rasterio.merge). Those need a fix in rasterio.

Tests

  • New test_open_rasterio__no_affine_mul_warning, parametrized for rectilinear and rotated transforms so it covers both affine_to_coords branches. It opens the file from the issue with that warning turned into an error and calls rio.transform(recalc=True). It fails on master and passes with this PR.
  • pre-commit passes. pylint rates the changed modules 10.00/10. mypy rioxarray/ shows the same 3 pre-existing operator errors in _spatial_utils.py as master, because affine's type hints don't cover NumPy arrays.
  • Full suite, locally (pip wheels: rasterio 1.5.1 / GDAL 3.12.4, affine 3.0.1): no new failures. The same 91 tests fail on master in this environment because the wheel's GDAL has no netCDF/HDF drivers. Warnings drop from 2787 to 672.

affine 3.0.1 emits a PendingDeprecationWarning when * is used for
matrix multiplication. The @ operator requires affine>=3.0, so declare
affine (already imported directly) as an explicit dependency.
@VolodymyrLinuxovich

Copy link
Copy Markdown
Author

Closing as a duplicate of #939, which already does this with the requested pins.

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.

rioxarray.open_rasterio starting to emit warning from affine

1 participant