Problem
The type np.ndarray is stubbed in this library as:
classndarray(_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...
importnumpyasnpfromtypings.sklearn.linear_modelimportLinearRegressiondefexample():
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:
deffit(
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 = truereportMissingTypeStubs = truepythonVersion = "3.12"
Problem
The type
np.ndarrayis stubbed in this library as:Throughout these stubs, the type
np.ndarrayis used without provided type parameters, seemingly with the expectation that this is treated asnp.ndarray[Any, Any](or more properlynp.ndarray[object. object]). However, Pyright in strict mode alternately interprets this asnp.ndarray[Unknown, Unknown].As a result, every method that involves
np.ndarrayor 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...In this case, the error occurs because the type of
fitis:And in turn
MatrixLikeis a (private) typealias that resolves to:Resolution
At least for this example, changing that type alias as follows resolves the type error.
System Details:
OS: MacOS Sonoma 14.1.2
Python: CPython 3.12.1
Pyright: 1.1.358
Pyright configuration: