Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scipy.stats.qmc.scale does not accept numpy array with typed shape as input when type checked with pyright #168

Closed
jenshnielsen opened this issue Nov 7, 2024 · 2 comments · Fixed by #172
Assignees
Labels
Milestone

Comments

@jenshnielsen
Copy link

The following example type checks with sample being a numpy array without an explicit shape type
but fails to type check with a given shape

numpy 2.1.3
pyright 1.1.388
python 3.12.7

from typing import Literal

import numpy as np
from scipy.stats import qmc

l_bounds, u_bounds = 0, 1


# sample = np.random.rand(10, 10)  # works
# sample: np.ndarray[tuple[int, int], np.dtype[np.float64]] = np.random.rand(10, 10) # fails
sample: np.ndarray[tuple[Literal[10], Literal[10]], np.dtype[np.float64]] = (  # fails
    np.random.rand(10, 10)
)


setpoint_values = qmc.scale(
    sample,
    l_bounds=l_bounds,
    u_bounds=u_bounds,
)

The error returned is

d:\source\repos\debug_typing.py
  d:\source\repos\debug_typing.py:16:5 - error: Argument of type "NDArray[float64]" cannot be assigned to parameter "sample" of type "_Any2D_f" in function "scale"
    Type "NDArray[float64]" is not assignable to type "_Any2D_f"
      "ndarray[tuple[Literal[10], Literal[10]], dtype[float64]]" is incompatible with protocol "_CanLenArray[floating[Any]]"
        "__array__" is an incompatible type
          One or more overloads of "__array__" is not assignable
            No overloaded function matches type "(dtype: _DT@__array__, /) -> ndarray[_ShapeT_co@__array__, _DT@__array__]"
      "ndarray[tuple[Literal[10], Literal[10]], dtype[float64]]" is not assignable to "Sequence[Sequence[float | floating[Any]]]"
      "ndarray[tuple[Literal[10], Literal[10]], dtype[float64]]" is not assignable to "Sequence[_Any1D_f]" (reportArgumentType)
1 error, 0 warnings, 0 informations
@jorenham jorenham added scipy.stats stubs: incorrect Incorrect annotation labels Nov 7, 2024
@jorenham jorenham self-assigned this Nov 7, 2024
@jorenham jorenham added this to the 1.14.1.3 milestone Nov 7, 2024
@jorenham
Copy link
Owner

jorenham commented Nov 8, 2024

I can reproduce this 👌🏻

from typing import Any

import numpy as np
from scipy.stats import qmc

_f8_xd: np.ndarray[Any, np.dtype[np.float64]]
qmc.scale(_f8_xd, 0, 1)  # ok

_f8_nd: np.ndarray[tuple[int, ...], np.dtype[np.float64]]
qmc.scale(_f8_nd, 0, 1)  # E: reportArgumentType

@jorenham
Copy link
Owner

jorenham commented Nov 8, 2024

It turns out that this is caused by a bug in optype.numpy.CanArray when numpy>=2.1.

Luckily, working around this is easy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants