I am having trouble getting the numpy.typing.mypy_plugin
to work.
Following the example of how to use NBitBase
, I get different output from mypy.
Problem
expected behavior
According to the docs, the output should be:
# note: Revealed local types are:
# note: a: numpy.floating[numpy.typing._16Bit*]
# note: b: numpy.signedinteger[numpy.typing._64Bit*]
# note: out: numpy.floating[numpy.typing._64Bit*]
actual behavior
My output, from running mypy
on the example code is:
╭sam@sam-XPS-15-7590:2 ...mpy_mypy_plugin_not_working ❖ do 16 10:19
╰[poetry: numpy_mypy_plugin_not_working/.venv]$ mypy
src/example/__init__.py:22: note: Revealed local types are:
src/example/__init__.py:22: note: a: numpy.floating[numpy._typing._16Bit]
src/example/__init__.py:22: note: b: numpy.signedinteger[numpy._typing._64Bit]
src/example/__init__.py:22: note: out: numpy.floating[Union[numpy._typing._16Bit, numpy._typing._64Bit]]
MWE
Below is MWE to reproduce the error.
I attached the poetry lock file in a gist here.
src/example/__init__.py
from typing import TYPE_CHECKING, TypeVar
import numpy as np
import numpy.typing as npt
T1 = TypeVar("T1", bound=npt.NBitBase)
T2 = TypeVar("T2", bound=npt.NBitBase)
def add(a: np.floating[T1], b: np.integer[T2]) -> np.floating[T1 | T2]:
return a + b
a = np.float16()
b = np.int64()
out = add(a, b)
print(a)
print(b)
print(out)
if TYPE_CHECKING:
reveal_locals()
pyproject.toml
[tool.poetry]
name = "example"
version = "0.1.0"
description = "Foo"
authors = ["Sam De Meyer <[email protected]>"]
packages = [{ include = "example", from = "src" }]
[tool.poetry.dependencies]
python = ">=3.11,<=3.12"
numpy = "^1.26.4"
[tool.poetry.group.dev.dependencies]
mypy = "^1.8.0"
[build-system]
requires = ["poetry-core>=1.7"]
build-backend = "poetry.core.masonry.api"
[tool.mypy]
files = ["src/example"]
python_version = "3.11"
pretty = true
plugins = ["numpy.typing.mypy_plugin"]
To recreate the python venv
, put the above files under the same directory as:
.
├── pyproject.toml
└── src
└── example
└── __init__.py
Then run poetry install
to create a venv
with the same package versions.
What I tried
-
I verified that I was using the correct python and mypy executables:
╭sam@sam-XPS-15-7590:2 ...mpy_mypy_plugin_not_working ❖ do 16 10:50 ✘1 ╰[poetry: numpy_mypy_plugin_not_working/.venv]$ poetry show mypy 1.10.0 Optional static typing for Python mypy-extensions 1.0.0 Type system extensions for programs checked with the mypy type checker. numpy 1.26.4 Fundamental package for array computing in Python typing-extensions 4.11.0 Backported and Experimental Type Hints for Python 3.8+ ╭sam@sam-XPS-15-7590:2 ...mpy_mypy_plugin_not_working ❖ do 16 10:51 ╰[poetry: numpy_mypy_plugin_not_working/.venv]$ which python /home/sam/Safe/Proj/Scratch/numpy_mypy_plugin_not_working/.venv/bin/python ╭sam@sam-XPS-15-7590:2 ...mpy_mypy_plugin_not_working ❖ do 16 10:52 ╰[poetry: numpy_mypy_plugin_not_working/.venv]$ which mypy /home/sam/Safe/Proj/Scratch/numpy_mypy_plugin_not_working/.venv/bin/mypy
-
I verified that mypy is picking up the plugin settings. If I make a typo in the plugin name I get:
╭sam@sam-XPS-15-7590:2 ...mpy_mypy_plugin_not_working ❖ do 16 10:22 ╰[poetry: numpy_mypy_plugin_not_working/.venv]$ mypy pyproject.toml:1: error: Error importing plugin "numpy.typing.mypy_plugi": No module named 'numpy.typing.mypy_plugi' [misc] [tool.poetry] ^ Found 1 error in 1 file (errors prevented further checking)
Which I see as sufficient proof that mypy is loading the plugin if typed correctly.
-
I tried disabling (commenting out) the line:
plugins = ["numpy.typing.mypy_plugin"]
Which resulted in the exact same (wrong) output of mypy