facebook / facebook/pyrefly

Errors with numpy indexing

Open
#3,697 4 comments 0 reactions 0 assignees View on GitHub
typechecking
Dominant language
Rust
Stars
7k
Forks
516
PR merge metrics
No merged PRs in 30d

Description

### Describe the Bug

On pyrefly version 1.0.0:

```
import numpy as np
import numpy.typing as npt

x: npt.NDArray[np.float64] = np.array([1.0, 2.0])
y = x[0]
```

gives the following error (edited only to remove user information):

```
% pyrefly check file.py
ERROR Cannot index into `ndarray[tuple[int, ...], dtype[float64]]` [bad-index]
--> file.py:29:5
|
29 | y = x[0]
| ^^^^
|
Missing positional argument `key` in function `numpy.ndarray.__getitem__`
ERROR Cannot index into `ndarray[tuple[int, ...], dtype[float64]]` [bad-index]
--> file.py:29:7
|
29 | y = x[0]
| ^
|
Argument `Literal[0]` is not assignable to parameter `self` with type `ndarray[tuple[int, ...], dtype[void]]` in function `numpy.ndarray.__getitem__
```

Similarly,

```
import numpy as np

def test(x: np.ndarray[tuple[int], np.dtype[np.float64]]):
y = x[0]
```
causes
```
% pyrefly check test.py
ERROR Cannot index into `ndarray[tuple[int], dtype[float64]]` [bad-index]
--> test.py:29:9
|
29 | y = x[0]
| ^^^^
|
Missing positional argument `key` in function `numpy.ndarray.__getitem__`
ERROR Cannot index into `ndarray[tuple[int], dtype[float64]]` [bad-index]
--> test.py:29:11
|
29 | y = x[0]
| ^
|
Argument `Literal[0]` is not assignable to parameter `self` with type `ndarray[tuple[int, ...], dtype[void]]` in function `numpy.ndarray.__getitem__`
```

I would expect no error, and in the second case, I expect Pyrefly to identify `y` as a `float`.

### Sandbox Link

_No response_

### (Only applicable for extension issues) IDE Information

I see the same issue on the Pycharm extension

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.