Skip to content

Commit 2600c65

Browse files
NickCrewscpcloud
authored andcommitted
feat: improve typing and docstring for to_pyarrow
1 parent 3a383d8 commit 2600c65

File tree

4 files changed

+82
-13
lines changed

4 files changed

+82
-13
lines changed

ibis/backends/__init__.py

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import weakref
1313
from collections import Counter
1414
from pathlib import Path
15-
from typing import TYPE_CHECKING, Any, ClassVar, NamedTuple
15+
from typing import TYPE_CHECKING, Any, ClassVar, NamedTuple, overload
1616

1717
import ibis
1818
import ibis.common.exceptions as exc
@@ -179,6 +179,39 @@ def to_pandas_batches(
179179
)
180180
)
181181

182+
@overload
183+
def to_pyarrow(
184+
self,
185+
expr: ir.Table,
186+
/,
187+
*,
188+
params: Mapping[ir.Scalar, Any] | None = None,
189+
limit: int | str | None = None,
190+
**kwargs: Any,
191+
) -> pa.Table: ...
192+
193+
@overload
194+
def to_pyarrow(
195+
self,
196+
expr: ir.Column,
197+
/,
198+
*,
199+
params: Mapping[ir.Scalar, Any] | None = None,
200+
limit: int | str | None = None,
201+
**kwargs: Any,
202+
) -> pa.Array: ...
203+
204+
@overload
205+
def to_pyarrow(
206+
self,
207+
expr: ir.Scalar,
208+
/,
209+
*,
210+
params: Mapping[ir.Scalar, Any] | None = None,
211+
limit: int | str | None = None,
212+
**kwargs: Any,
213+
) -> pa.Scalar: ...
214+
182215
@util.experimental
183216
def to_pyarrow(
184217
self,
@@ -188,8 +221,8 @@ def to_pyarrow(
188221
params: Mapping[ir.Scalar, Any] | None = None,
189222
limit: int | str | None = None,
190223
**kwargs: Any,
191-
) -> pa.Table:
192-
"""Execute expression and return results in as a pyarrow table.
224+
) -> pa.Table | pa.Array | pa.Scalar:
225+
"""Execute expression to a pyarrow object.
193226
194227
This method is eager and will execute the associated expression
195228
immediately.
@@ -208,9 +241,10 @@ def to_pyarrow(
208241
209242
Returns
210243
-------
211-
Table
212-
A pyarrow table holding the results of the executed expression.
213-
244+
result
245+
If the passed expression is a Table, a pyarrow table is returned.
246+
If the passed expression is a Column, a pyarrow array is returned.
247+
If the passed expression is a Scalar, a pyarrow scalar is returned.
214248
"""
215249
pa = self._import_pyarrow()
216250
self._run_pre_execute_hooks(expr)

ibis/expr/types/core.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -555,8 +555,8 @@ def to_pyarrow(
555555
params: Mapping[ir.Scalar, Any] | None = None,
556556
limit: int | str | None = None,
557557
**kwargs: Any,
558-
) -> pa.Table:
559-
"""Execute expression and return results in as a pyarrow table.
558+
) -> pa.Table | pa.Array | pa.Scalar:
559+
"""Execute expression to a pyarrow object.
560560
561561
This method is eager and will execute the associated expression
562562
immediately.
@@ -567,14 +567,16 @@ def to_pyarrow(
567567
Mapping of scalar parameter expressions to value.
568568
limit
569569
An integer to effect a specific row limit. A value of `None` means
570-
"no limit". The default is in `ibis/config.py`.
570+
no limit. The default is in `ibis/config.py`.
571571
kwargs
572572
Keyword arguments
573573
574574
Returns
575575
-------
576-
Table
577-
A pyarrow table holding the results of the executed expression.
576+
result
577+
If the passed expression is a Table, a pyarrow table is returned.
578+
If the passed expression is a Column, a pyarrow array is returned.
579+
If the passed expression is a Scalar, a pyarrow scalar is returned.
578580
"""
579581
return self._find_backend(use_default=True).to_pyarrow(
580582
self, params=params, limit=limit, **kwargs

ibis/expr/types/generic.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from ibis.common.grounds import Singleton
1515
from ibis.expr.rewrites import rewrite_window_input
1616
from ibis.expr.types.core import Expr, _binop, _FixedTextJupyterMixin
17-
from ibis.util import deprecated, promote_list
17+
from ibis.util import deprecated, experimental, promote_list
1818

1919
if TYPE_CHECKING:
2020
import datetime
@@ -1385,6 +1385,17 @@ def to_pandas(
13851385

13861386
@public
13871387
class Scalar(Value):
1388+
# overriding Expr's implementation just for typing
1389+
@experimental
1390+
def to_pyarrow(
1391+
self,
1392+
*,
1393+
params: Mapping[ir.Scalar, Any] | None = None,
1394+
limit: int | str | None = None,
1395+
**kwargs: Any,
1396+
) -> pa.Scalar:
1397+
return super().to_pyarrow(params=params, limit=limit, **kwargs)
1398+
13881399
def __pyarrow_result__(
13891400
self,
13901401
table: pa.Table,
@@ -1575,6 +1586,17 @@ def preview(
15751586
console_width=console_width,
15761587
)
15771588

1589+
# overriding Expr's implementation just for typing
1590+
@experimental
1591+
def to_pyarrow(
1592+
self,
1593+
*,
1594+
params: Mapping[ir.Scalar, Any] | None = None,
1595+
limit: int | str | None = None,
1596+
**kwargs: Any,
1597+
) -> pa.Array:
1598+
return super().to_pyarrow(params=params, limit=limit, **kwargs)
1599+
15781600
def __pyarrow_result__(
15791601
self,
15801602
table: pa.Table,

ibis/expr/types/relations.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from ibis.expr.types.core import Expr, _FixedTextJupyterMixin
2525
from ibis.expr.types.generic import Value, literal
2626
from ibis.expr.types.temporal import TimestampColumn
27-
from ibis.util import deprecated
27+
from ibis.util import deprecated, experimental
2828

2929
if TYPE_CHECKING:
3030
import pandas as pd
@@ -242,6 +242,17 @@ def __polars_result__(self, df: pl.DataFrame) -> Any:
242242

243243
return PolarsData.convert_table(df, self.schema())
244244

245+
# overriding Expr's implementation just for typing
246+
@experimental
247+
def to_pyarrow(
248+
self,
249+
*,
250+
params: Mapping[ir.Scalar, Any] | None = None,
251+
limit: int | str | None = None,
252+
**kwargs: Any,
253+
) -> pa.Table:
254+
return super().to_pyarrow(params=params, limit=limit, **kwargs)
255+
245256
def _fast_bind(self, *args, **kwargs):
246257
# allow the first argument to be either a dictionary or a list of values
247258
if len(args) == 1:

0 commit comments

Comments
 (0)