Unparameterized `np.ndarray` typings produce "Type of ... is partially unknown" Pyright type errors.

未关闭
#309 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
numpy, python

调研方向

Start by reproducing the issue with example.py, the strict Pyright configuration, and the LinearRegression.fit entry point. Trace the private MatrixLike alias in the sklearn.linear_model stubs, identify the unparameterized ndarray annotations involved, and run Pyright again to confirm the partially-unknown errors are resolved.

由索引模型根据 Issue 内容生成。

描述

bug
Problem

The type np.ndarray is stubbed in this library as:

class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]):
    ...

Throughout these stubs, the type np.ndarray is used without provided type parameters, seemingly with the expectation that this is treated as np.ndarray[Any, Any] (or more properly np.ndarray[object. object]). However, Pyright in strict mode alternately interprets this as np.ndarray[Unknown, Unknown].

As a result, every method that involves np.ndarray or a type alias which includes it produces a partially-unknown-type error:

Example Reproduction

For example, if we take the following simple file example.py...

import numpy as np

from typings.sklearn.linear_model import LinearRegression


def example():
    x = np.array([1, 2, 3, 4, 5])
    y = np.array([2, 4, 6, 8, 10])

    linreg = LinearRegression()
    linreg.fit(x, y)
❯ pyright src/path/to/example.py

src/path/to/example.py
  src/path/to/example/example.py:11:5 - error: Type of "fit" is partially unknown
    Type of "fit" is "(X: ndarray[Unknown, Unknown] | DataFrame | spmatrix | Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes], y: ndarray[Unknown, Unknown] | DataFrame | spmatrix | Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes], sample_weight: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None = None) -> LinearRegression" 

In this case, the error occurs because the type of fit is:

    def fit(
        self: LinearRegression_Self,
        X: MatrixLike | ArrayLike,
        y: MatrixLike | ArrayLike,
        sample_weight: None | ArrayLike = None,
    ) -> LinearRegression_Self:
        ...

And in turn MatrixLike is a (private) typealias that resolves to:

MatrixLike = np.ndarray | pd.DataFrame | spmatrix
Resolution

At least for this example, changing that type alias as follows resolves the type error.

MatrixLike = np.ndarray | pd.DataFrame | spmatrix

System Details:

OS: MacOS Sonoma 14.1.2
Python: CPython 3.12.1
Pyright: 1.1.358

Pyright configuration:

[tool.pyright]
include = ["./src", "./tests"]
stubPath = "./typings"

typeCheckingMode = "strict"
reportMissingImports = true
reportMissingTypeStubs = true

pythonVersion = "3.12"
主要语言
Python
星标
304
派生
104
PR 合并指标
30 天内没有已合并 PR

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

microsoft/python-type-stubs 的其他 Issue

查看 microsoft/python-type-stubs 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。