From f69472ff2893272b332625b9116f470c37507b93 Mon Sep 17 00:00:00 2001 From: Kyle Barron Date: Tue, 27 Jan 2026 23:29:05 -0500 Subject: [PATCH 1/2] Improved typing for `__array__` --- src/affine.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/affine.py b/src/affine.py index 41c54ed..3a16fae 100644 --- a/src/affine.py +++ b/src/affine.py @@ -37,10 +37,20 @@ from collections.abc import MutableSequence, Sequence from functools import cached_property import math +from typing import TYPE_CHECKING, overload import warnings from attrs import astuple, define, field +if TYPE_CHECKING: + from typing import TypeVar, overload + + import numpy as np + from numpy.typing import NDArray + + _ScalarType = TypeVar("_ScalarType", bound=np.generic) + + __all__ = ["Affine"] __author__ = "Sean Gillies" __version__ = "3.0rc2" @@ -280,6 +290,18 @@ def permutation(cls, *scaling: float) -> Affine: """ return cls(0.0, 1.0, 0.0, 1.0, 0.0, 0.0) + @overload + def __array__( + self, dtype: None = None, copy: bool | None = None + ) -> NDArray[np.float64]: ... + @overload + def __array__( + self, dtype: type[_ScalarType], copy: bool | None = None + ) -> NDArray[_ScalarType]: ... + @overload + def __array__( + self, dtype: np.dtype[_ScalarType], copy: bool | None = None + ) -> NDArray[_ScalarType]: ... def __array__(self, dtype=None, copy: bool | None = None): """Get affine matrix as a 3x3 NumPy array. From 01c0a0fb8a88b5089303ea7c376040d7b0623c13 Mon Sep 17 00:00:00 2001 From: Kyle Barron Date: Mon, 2 Feb 2026 22:07:43 -0500 Subject: [PATCH 2/2] Hard-code return value as np.float64 --- src/affine/__init__.py | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/src/affine/__init__.py b/src/affine/__init__.py index 3a16fae..108f3a5 100644 --- a/src/affine/__init__.py +++ b/src/affine/__init__.py @@ -290,19 +290,7 @@ def permutation(cls, *scaling: float) -> Affine: """ return cls(0.0, 1.0, 0.0, 1.0, 0.0, 0.0) - @overload - def __array__( - self, dtype: None = None, copy: bool | None = None - ) -> NDArray[np.float64]: ... - @overload - def __array__( - self, dtype: type[_ScalarType], copy: bool | None = None - ) -> NDArray[_ScalarType]: ... - @overload - def __array__( - self, dtype: np.dtype[_ScalarType], copy: bool | None = None - ) -> NDArray[_ScalarType]: ... - def __array__(self, dtype=None, copy: bool | None = None): + def __array__(self, dtype=None, copy: bool | None = None) -> NDArray[np.float64]: """Get affine matrix as a 3x3 NumPy array. Parameters