Skip to content

Commit bc71f20

Browse files
committed
feat(types): add typing overloads to common .cast() dtypes
1 parent a720e68 commit bc71f20

File tree

1 file changed

+49
-4
lines changed

1 file changed

+49
-4
lines changed

ibis/expr/types/generic.py

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from collections.abc import Sequence
4-
from typing import TYPE_CHECKING, Any
4+
from typing import TYPE_CHECKING, Any, Literal, overload
55

66
from public import public
77

@@ -17,6 +17,9 @@
1717
from ibis.util import deprecated, promote_list
1818

1919
if TYPE_CHECKING:
20+
import datetime
21+
import uuid
22+
2023
import pandas as pd
2124
import polars as pl
2225
import pyarrow as pa
@@ -135,6 +138,48 @@ def hash(self) -> ir.IntegerValue:
135138
"""
136139
return ops.Hash(self).to_expr()
137140

141+
@overload
142+
def cast(
143+
self, target_type: Literal["string", "str"] | type[str]
144+
) -> ir.StringValue: ...
145+
@overload
146+
def cast(
147+
self,
148+
target_type: Literal[
149+
"int",
150+
"uint",
151+
"int8",
152+
"uint8",
153+
"int16",
154+
"uint16",
155+
"int32",
156+
"uint32",
157+
"int64",
158+
"uint64",
159+
]
160+
| type[int],
161+
) -> ir.IntegerValue: ...
162+
@overload
163+
def cast(
164+
self,
165+
target_type: Literal["float", "float8", "float16", "float32", "float64"]
166+
| type[float],
167+
) -> ir.FloatingValue: ...
168+
@overload
169+
def cast(
170+
self, target_type: Literal["bool", "boolean"] | type[bool]
171+
) -> ir.BooleanValue: ...
172+
@overload
173+
def cast(self, target_type: Literal["date"]) -> ir.DateValue: ...
174+
@overload
175+
def cast(
176+
self, target_type: Literal["datetime", "timestamp"] | type[datetime.datetime]
177+
) -> ir.TimestampValue: ...
178+
@overload
179+
def cast(self, target_type: Literal["time"]) -> ir.TimeValue: ...
180+
@overload
181+
def cast(self, target_type: Literal["uuid"] | type[uuid.UUID]) -> ir.UUIDValue: ...
182+
138183
def cast(self, target_type: Any) -> Value:
139184
"""Cast expression to indicated data type.
140185
@@ -1526,9 +1571,9 @@ def __pandas_result__(
15261571
if data_mapper is None:
15271572
from ibis.formats.pandas import PandasData as data_mapper
15281573

1529-
assert len(df.columns) == 1, (
1530-
"more than one column when converting columnar result DataFrame to Series"
1531-
)
1574+
assert (
1575+
len(df.columns) == 1
1576+
), "more than one column when converting columnar result DataFrame to Series"
15321577
# in theory we could use df.iloc[:, 0], but there seems to be a bug in
15331578
# older geopandas where df.iloc[:, 0] doesn't return the same kind of
15341579
# object as df.loc[:, column_name] when df is a GeoDataFrame

0 commit comments

Comments
 (0)