无法通过numpy.typing.mypy_plugin复现NBitBase示例的类型推断结果
numpy.typing.mypy_plugin类型推断与官方文档预期不符
问题描述
使用numpy.typing.mypy_plugin时,按照官方文档中NBitBase的示例代码执行后,mypy输出的类型推断结果与预期不一致。
预期行为
根据官方文档,mypy应输出如下类型信息:
# 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*]
实际行为
运行mypy后得到的实际输出:
╭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)
已准备包含src/example/__init__.py代码、pyproject.toml配置的最小复现环境,可通过poetry install创建相同版本的虚拟环境。
已尝试的操作
- 验证Python和mypy可执行文件路径正确;
- 确认mypy已正确加载
numpy.typing.mypy_plugin; - 禁用插件后,mypy输出仍与错误结果一致。
内容的提问来源于stack exchange,提问作者Sam De Meyer
相关产品推荐
相关产品推荐

