PR: Add scipy-stubs as development dependency.#1342
Conversation
|
I have analyzed the typing errors in scipy/scipy-stubs#495 (comment). Through this, I found three bugs in But the majority of the typing errors are true positives, and should probably be fixed in within colour itself. Unless anyone objects, I'd be willing to do include those fixes in this PR. |
|
Thanks @jorenham, sounds good with me! |
for more information, see https://pre-commit.ci
|
All errors are fixed when the edit I didn't want to force you to upgrade to the latest |
|
|
||
| def __init__(self, x: ArrayLike, y: ArrayLike, *args: Any, **kwargs: Any) -> None: | ||
| super().__init__(x, y, *args, **kwargs) | ||
| super().__init__(as_float_array(x), as_float_array(y), *args, **kwargs) |
There was a problem hiding this comment.
typing.cast would also be an option, but strictly speaking I'd have to cast it to Any for it to be theoretically allowed. Downcasting this correctly would require recreating the ToFloatND or ToFloat1D types from optype.numpy (docs) here, which would be rather messy.
| M = np.matmul(np.matmul(MATRIX_R, row_as_diagonal(LMS_a_L)), MATRIX_XYZ_TO_HPE) | ||
| XYZ_ref = vecmul(M, XYZ) | ||
|
|
||
| Y_ref: NDArrayFloat |
There was a problem hiding this comment.
This ensures that the right @overload of spow is selected, so that LR will be inferred as NDArrayFloat
| ) = optimisation_factory() | ||
| optimisation_settings = { | ||
|
|
||
| optimisation_settings: dict[str, Any] = { |
There was a problem hiding this comment.
without this, it'd be inferred as dict[str, str], which due to its invariant type-parameters, would lead to it being rejected when passed as **kwargs to minimize below
| **settings_BT2246, | ||
| ) | ||
| ), | ||
| ).item(), |
There was a problem hiding this comment.
The fmin objective function must return a scalar type, but this returns an array of shape (1,). This sneaky workaround was was the least intrusive I could find.
| def __init__(self, *args: Any, **kwargs: Any) -> None: ... # pragma: no cover | ||
|
|
||
| def __call__(self, x: ArrayLike) -> NDArray: # noqa: D102 | ||
| def __call__(self, x: NDArrayFloat) -> NDArray: # noqa: D102 |
There was a problem hiding this comment.
input positions are contravariant, so assigning to this protocol, requires that the parameter types of that type are less strict than that of this Protocol. Put differently; narrowing the ArrayLike parameter type to NDArrayFloat results in a broader Protocol type.
| target_o: ArrayLike, coefficients_0_o: ArrayLike | ||
| ) -> Tuple[NDArrayFloat, float]: | ||
| target_o: NDArrayFloat, coefficients_0_o: NDArrayFloat | ||
| ) -> Tuple[NDArrayFloat, float | np.float64]: |
There was a problem hiding this comment.
Both result.fun: np.float64 and errors.error: float are assignable to float | np.float64, and on numpy >= 2.2 it's even equivalent to float64 (float64 has been made a proper subtype of builtins.float)
| coefficients = dimensionalise_coefficients(coefficients, cmfs.shape) | ||
|
|
||
| return coefficients, error | ||
| return coefficients, float(error) |
There was a problem hiding this comment.
because error: float | np.float64 at this point
| """Define the objective function.""" | ||
|
|
||
| return cast(NDArrayFloat, np.sum(np.diff(a) ** 2)) | ||
| return np.sum(np.square(np.diff(a))) |
There was a problem hiding this comment.
In numpy==2.2.0 there was a bug that caused np.diff(a) ** 2 to be inferred as np.signedinteger. This has been resolved in 2.2.4, but I figured that square would be a more stable solution.
|
Pardon the spam. I wanted to explain some of the non-obvious choices I made here. Feel free to resolve them as you see fit. |
|
This is ready for review, in case you missed it :) |
scipy-stubs as dev dependencyscipy-stubs as development dependency.
KelSolaar
left a comment
There was a problem hiding this comment.
Thanks a lot! LGTM besides the private _typeshed import.
Co-authored-by: Thomas Mansencal <thomas.mansencal@gmail.com>
|
Thanks @jorenham, merged! |
Summary
This adds scipy-stubs as a development dependency. By doing so, 58 new pyright errors popped up. There were 18 that were caused by two bugs and two other issues in
scipy-stubs, and I've fixed them. See scipy/scipy-stubs#495 for details.The other 40 errors turned out to be typing issues in
colouritself. I have included their fixes in this PR.I also noticed that an additional error pops up when the latest(resolved in numpy 2.2.5)numpy==2.2.4release is installed (currently pinned at2.2.0). I've addressed this in numpy/numpy#28660, and the fix will be included in the upcoming2.2.5(and2.3.0) NumPy release(s).This additionally improves the static-typing compatibility for different versions of pyright, numpy, scipy, and scipy-stubs.
Preflight
Code Style and Quality