Skip to content

Commit 7d1d912

Browse files
- new _typing and _expression stub file to centralize type aliases and allow circular imports between files.
- added nested dtypes, bytesarray, and memoryview as literal, convertible python types - PythonLiteral is a recursive type, to allow dict of list, list of list, etc...
1 parent 6b092b0 commit 7d1d912

2 files changed

Lines changed: 90 additions & 0 deletions

File tree

_duckdb-stubs/_expression.pyi

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from typing import TYPE_CHECKING, Any, overload
2+
from duckdb import sqltypes
3+
4+
if TYPE_CHECKING:
5+
from ._typing import IntoExpr
6+
7+
class Expression:
8+
def __add__(self, other: IntoExpr) -> Expression: ...
9+
def __and__(self, other: IntoExpr) -> Expression: ...
10+
def __div__(self, other: IntoExpr) -> Expression: ...
11+
def __eq__(self, other: IntoExpr) -> Expression: ... # type: ignore[override]
12+
def __floordiv__(self, other: IntoExpr) -> Expression: ...
13+
def __ge__(self, other: IntoExpr) -> Expression: ...
14+
def __gt__(self, other: IntoExpr) -> Expression: ...
15+
@overload
16+
def __init__(self, arg0: str) -> None: ...
17+
@overload
18+
def __init__(self, arg0: Any) -> None: ...
19+
def __invert__(self) -> Expression: ...
20+
def __le__(self, other: IntoExpr) -> Expression: ...
21+
def __lt__(self, other: IntoExpr) -> Expression: ...
22+
def __mod__(self, other: IntoExpr) -> Expression: ...
23+
def __mul__(self, other: IntoExpr) -> Expression: ...
24+
def __ne__(self, other: IntoExpr) -> Expression: ... # type: ignore[override]
25+
def __neg__(self) -> Expression: ...
26+
def __or__(self, other: IntoExpr) -> Expression: ...
27+
def __pow__(self, other: IntoExpr) -> Expression: ...
28+
def __radd__(self, other: IntoExpr) -> Expression: ...
29+
def __rand__(self, other: IntoExpr) -> Expression: ...
30+
def __rdiv__(self, other: IntoExpr) -> Expression: ...
31+
def __rfloordiv__(self, other: IntoExpr) -> Expression: ...
32+
def __rmod__(self, other: IntoExpr) -> Expression: ...
33+
def __rmul__(self, other: IntoExpr) -> Expression: ...
34+
def __ror__(self, other: IntoExpr) -> Expression: ...
35+
def __rpow__(self, other: IntoExpr) -> Expression: ...
36+
def __rsub__(self, other: IntoExpr) -> Expression: ...
37+
def __rtruediv__(self, other: IntoExpr) -> Expression: ...
38+
def __sub__(self, other: IntoExpr) -> Expression: ...
39+
def __truediv__(self, other: IntoExpr) -> Expression: ...
40+
def alias(self, name: str) -> Expression: ...
41+
def asc(self) -> Expression: ...
42+
def between(self, lower: IntoExpr, upper: IntoExpr) -> Expression: ...
43+
def cast(self, type: sqltypes.DuckDBPyType) -> Expression: ...
44+
def collate(self, collation: str) -> Expression: ...
45+
def desc(self) -> Expression: ...
46+
def get_name(self) -> str: ...
47+
def isin(self, *args: IntoExpr) -> Expression: ...
48+
def isnotin(self, *args: IntoExpr) -> Expression: ...
49+
def isnotnull(self) -> Expression: ...
50+
def isnull(self) -> Expression: ...
51+
def nulls_first(self) -> Expression: ...
52+
def nulls_last(self) -> Expression: ...
53+
def otherwise(self, value: IntoExpr) -> Expression: ...
54+
def show(self) -> None: ...
55+
def when(self, condition: IntoExpr, value: IntoExpr) -> Expression: ...

_duckdb-stubs/_typing.pyi

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from __future__ import annotations
2+
from typing import TypeAlias, TYPE_CHECKING
3+
from datetime import date, datetime, time, timedelta
4+
from decimal import Decimal
5+
from uuid import UUID
6+
from collections.abc import Mapping
7+
8+
if TYPE_CHECKING:
9+
from ._expression import Expression
10+
11+
NumericLiteral: TypeAlias = int | float | Decimal
12+
"""Python objects that can be converted to a numerical `ConstantExpression` (integer or floating points numbers.)"""
13+
TemporalLiteral: TypeAlias = date | datetime | time | timedelta
14+
BlobLiteral: TypeAlias = bytes | bytearray | memoryview
15+
"""Python objects that can be converted to a `BLOB` `ConstantExpression`.
16+
17+
Note:
18+
`bytes` can also be converted to a `BITSTRING`.
19+
"""
20+
NonNestedLiteral: TypeAlias = NumericLiteral | TemporalLiteral | str | bool | BlobLiteral | UUID
21+
PythonLiteral: TypeAlias = (
22+
NonNestedLiteral | list[PythonLiteral] | tuple[PythonLiteral, ...] | dict[PythonLiteral, PythonLiteral] | None
23+
)
24+
"""Python objects that can be converted to a `ConstantExpression`."""
25+
# the field_ids argument to to_parquet and write_parquet has a recursive structure
26+
ParquetFieldIdsType: TypeAlias = Mapping[str, int | ParquetFieldIdsType]
27+
28+
IntoExprColumn: TypeAlias = Expression | str
29+
"""Types that are, or can be used as a `ColumnExpression`."""
30+
IntoExpr: TypeAlias = IntoExprColumn | PythonLiteral
31+
"""Any type that can be converted to an `Expression` (or is already one).
32+
33+
See Also:
34+
https://duckdb.org/docs/stable/clients/python/conversion
35+
"""

0 commit comments

Comments
 (0)