immediate numpy arguments not type checked (python 3.10)
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
Immediate numpy parameters are not type checked. Consider the following code:
import numpy as np
def do_something_uint8(val: np.typing.NDArray[np.uint8]):
...
def do_something_float(val: np.typing.NDArray[np.float32]):
...
def scalar_int(val: int):
...
scalar_int(3.0) # correctly shows mypy error here
np_u8 = np.random.rand(3, 4).astype(np.uint8)
np_f = np.random.rand(3, 4).astype(np.float32)
do_something_uint8(np_f) # correctly shows mypy error
do_something_float(np_f)
do_something_uint8(np_u8)
do_something_float(np_u8) # correctly shows mypy error
do_something_uint8(np.random.rand(3, 4).astype(np.float32)) # does NOT show mypy error here
Look at the last line. This should show a mypy error (similar to the scalar_int case), since we are passing in a numpy with dtype float32. But it does not. If we first assign to a variable, then the type check works ok.
Expected Behavior
The last line of the code above should show a mypy error, because we are trying to pass in a numpy array with dtype float32, into a function that only accepts numpy array of dtype uint8.
Actual Behavior
The last line of the code above passes mypy checking.
Your Environment
$ python -V
Python 3.10.7
$ pip freeze | grep mypy
mypy==0.991
mypy-extensions==0.4.3
$ uname -a
Darwin Hughs-MacBook-Air.local 21.6.0 Darwin Kernel Version 21.6.0: Thu Sep 29 20:11:33 PDT 2022; root:xnu-8020.240.7~1/RELEASE_ARM64_T8110 arm64
[mypy]
ignore_missing_imports = True
No mypy command-line flags.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the final direct NumPy call and the equivalent variable-based calls with mypy 0.991 under Python 3.10. Trace how immediate call arguments are inferred and checked against the annotated NDArray dtype; done means the direct float32-to-uint8 call reports an error consistently with the assigned-variable case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, python
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100