Skip to content

Commit d560d80

Browse files
numpy protocols improvements
1 parent 4ebc3ba commit d560d80

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

_duckdb-stubs/_typing.pyi

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,46 @@ if TYPE_CHECKING:
1010
from ._expression import Expression
1111
from ._sqltypes import DuckDBPyType
1212

13+
# Numpy protocols
14+
1315
_T_co = TypeVar("_T_co", covariant=True)
1416
_S_co = TypeVar("_S_co", bound=tuple[Any, ...], covariant=True)
1517
_D_co = TypeVar("_D_co", covariant=True)
1618

17-
class NPTypeLike(Protocol, Generic[_T_co]): ...
19+
class NPProtocol(Protocol):
20+
"""Base Protocol for numpy objects."""
21+
@property
22+
def dtype(self) -> Any: ...
23+
@property
24+
def ndim(self) -> int: ...
25+
def __array__(self, *args: Any, **kwargs: Any) -> Any: ...
26+
def __array_wrap__(self, *args: Any, **kwargs: Any) -> Any: ...
27+
@property
28+
def __array_interface__(self) -> dict[str, Any]: ...
29+
@property
30+
def __array_priority__(self) -> float: ...
1831

19-
class NPArrayLike(Protocol, Generic[_S_co, _D_co]):
32+
class NPScalarTypeLike(NPProtocol, Protocol):
33+
@property
34+
def itemsize(self) -> int: ...
35+
36+
class NPArrayLike(NPProtocol, Generic[_S_co, _D_co], Protocol):
37+
"""`numpy.ndarray` Protocol.
38+
39+
This is needed to accept numpy arrays as literals in expressions, without emitting type checker errors about unknown symbol if the user doesn't have numpy installed.
40+
41+
Note:
42+
Using `np.typing.NDArray` is still the best option for return types.
43+
"""
2044
def __len__(self) -> int: ...
2145
def __contains__(self, value: object, /) -> bool: ...
2246
def __iter__(self) -> Iterator[_D_co]: ...
23-
def __array__(self, *args: Any, **kwargs: Any) -> Any: ...
2447
def __array_finalize__(self, *args: Any, **kwargs: Any) -> None: ...
25-
def __array_wrap__(self, *args: Any, **kwargs: Any) -> Any: ...
2648
def __getitem__(self, *args: Any, **kwargs: Any) -> Any: ...
2749
def __setitem__(self, *args: Any, **kwargs: Any) -> None: ...
2850
@property
2951
def shape(self) -> _S_co: ...
3052
@property
31-
def dtype(self) -> Any: ...
32-
@property
33-
def ndim(self) -> int: ...
34-
@property
3553
def size(self) -> int: ...
3654

3755
NumericLiteral: TypeAlias = int | float | Decimal

0 commit comments

Comments
 (0)