Unparameterized `np.ndarray` typings produce "Type of ... is partially unknown" Pyright type errors.
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 35/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- numpy, python
- Lĩnh vực
- developer-experience, tooling
Hướng nghiên cứu
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.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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"
- Ngôn ngữ chính
- Python
- Star
- 304
- Fork
- 104
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của microsoft/python-type-stubs
-
Maintenance Status Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 35/100
microsoft/python-type-stubs#395 · 1 bình luận ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 58/100
microsoft/python-type-stubs#392 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 45/100
microsoft/python-type-stubs#345 ·
-
Inconsistent naming of stubs Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
microsoft/python-type-stubs#315 · 1 bình luận ·
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 25/100
microsoft/python-type-stubs#211 · 8 bình luận · 29 reaction ·
Tất cả issue của microsoft/python-type-stubs
Issue tương tự
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
zostera/django-bootstrap4#894 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
use-agent-os/agent-os#3276 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
zephyrproject-rtos/zephyr#119726 ·
-
area/auth bug comp/agent P3 platform/discord type/security
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
NousResearch/hermes-agent#117848 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
zilliztech/memsearch#759 ·