Cannot reproduce numpy.typing.NBitBase example with `numpy.typing.mypy_plugin`

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> # 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*]
</code>
<code> # 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*] </code>
    # 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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>╭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]]
</code>
<code>╭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]] </code>
╭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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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()
</code>
<code>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() </code>
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[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"]
</code>
<code>[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"] </code>
[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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>.
├── pyproject.toml
└── src
└── example
└── __init__.py
</code>
<code>. ├── pyproject.toml └── src └── example └── __init__.py </code>
.
├── 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:

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    <code>╭sam@sam-XPS-15-7590:2 ...mpy_mypy_plugin_not_working ❖ do 16 10:501
    [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
    </code>
    <code>╭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 </code>
    ╭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:

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    <code>╭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)
    </code>
    <code>╭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) </code>
    ╭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:

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    <code>plugins = ["numpy.typing.mypy_plugin"]
    </code>
    <code>plugins = ["numpy.typing.mypy_plugin"] </code>
    plugins = ["numpy.typing.mypy_plugin"]
    

    Which resulted in the exact same (wrong) output of mypy

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật