Unparameterized `np.ndarray` typings produce "Type of ... is partially unknown" Pyright type errors.
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 35/100
- Tipo di issue
- Bug
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Ferma
- Stack tecnologico
- numpy, python
- Ambito
- developer-experience, tooling
Direzione di ricerca
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.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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"
- Lingua principale
- Python
- Stelle
- 304
- Fork
- 104
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di microsoft/python-type-stubs
-
Maintenance Status Aperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 35/100
microsoft/python-type-stubs#395 · 1 commento ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 58/100
microsoft/python-type-stubs#392 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 45/100
microsoft/python-type-stubs#345 ·
-
Inconsistent naming of stubs Aperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
microsoft/python-type-stubs#315 · 1 commento ·
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 25/100
microsoft/python-type-stubs#211 · 8 commenti · 29 reazioni ·
Tutte le issue di microsoft/python-type-stubs
Issue simili
-
area: harness bug status: needs-triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
Human-Agent-Society/reef#625 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 80/100
learningequality/kolibri#15351 · 2 commenti ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
-
Name consistency Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
eellak/triplestore#65 · 1 commento ·