Skip to content

Commit 03f915e

Browse files
added Numpy Array protocol to accepted literal types. allow to add numpy ndarray without creating unknown type errors if the library isn't installed in the venv
1 parent 40b10d4 commit 03f915e

1 file changed

Lines changed: 32 additions & 3 deletions

File tree

_duckdb-stubs/_typing.pyi

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,37 @@
11
from __future__ import annotations
2-
from typing import TypeAlias, TYPE_CHECKING
2+
from typing import TypeAlias, TYPE_CHECKING, Protocol, Any, TypeVar, Generic
33
from datetime import date, datetime, time, timedelta
44
from decimal import Decimal
55
from uuid import UUID
6-
from collections.abc import Mapping
6+
from collections.abc import Mapping, Iterator
77

88
if TYPE_CHECKING:
99
from ._expression import Expression
1010

11+
_T_co = TypeVar("_T_co", covariant=True)
12+
_S_co = TypeVar("_S_co", bound=tuple[Any, ...], covariant=True)
13+
_D_co = TypeVar("_D_co", covariant=True)
14+
15+
class NPTypeLike(Protocol, Generic[_T_co]): ...
16+
17+
class NPArrayLike(Protocol, Generic[_S_co, _D_co]):
18+
def __len__(self) -> int: ...
19+
def __contains__(self, value: object, /) -> bool: ...
20+
def __iter__(self) -> Iterator[_D_co]: ...
21+
def __array__(self, *args: Any, **kwargs: Any) -> Any: ...
22+
def __array_finalize__(self, *args: Any, **kwargs: Any) -> None: ...
23+
def __array_wrap__(self, *args: Any, **kwargs: Any) -> Any: ...
24+
def __getitem__(self, *args: Any, **kwargs: Any) -> Any: ...
25+
def __setitem__(self, *args: Any, **kwargs: Any) -> None: ...
26+
@property
27+
def shape(self) -> _S_co: ...
28+
@property
29+
def dtype(self) -> Any: ...
30+
@property
31+
def ndim(self) -> int: ...
32+
@property
33+
def size(self) -> int: ...
34+
1135
NumericLiteral: TypeAlias = int | float | Decimal
1236
"""Python objects that can be converted to a numerical `ConstantExpression` (integer or floating points numbers.)"""
1337
TemporalLiteral: TypeAlias = date | datetime | time | timedelta
@@ -19,7 +43,12 @@ Note:
1943
"""
2044
NonNestedLiteral: TypeAlias = NumericLiteral | TemporalLiteral | str | bool | BlobLiteral | UUID
2145
PythonLiteral: TypeAlias = (
22-
NonNestedLiteral | list[PythonLiteral] | tuple[PythonLiteral, ...] | dict[PythonLiteral, PythonLiteral] | None
46+
NonNestedLiteral
47+
| list[PythonLiteral]
48+
| tuple[PythonLiteral, ...]
49+
| dict[PythonLiteral, PythonLiteral]
50+
| NPArrayLike[Any, Any]
51+
| None
2352
)
2453
"""Python objects that can be converted to a `ConstantExpression`."""
2554
# the field_ids argument to to_parquet and write_parquet has a recursive structure

0 commit comments

Comments
 (0)