Skip to content

Commit 05cd237

Browse files
committed
refactor: update type hints to use typing.Union for better clarity and consistency
1 parent 37307b0 commit 05cd237

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

python/datafusion/expr.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222

2323
from __future__ import annotations
2424

25-
from typing import TYPE_CHECKING, Any, ClassVar, Iterable, Optional
25+
import typing as _typing
26+
from typing import TYPE_CHECKING, Any, ClassVar, Iterable, Optional, Sequence
2627

2728
import pyarrow as pa
2829

@@ -227,7 +228,7 @@
227228
]
228229

229230

230-
def ensure_expr(value: Expr | Any) -> expr_internal.Expr:
231+
def ensure_expr(value: _typing.Union["Expr", Any]) -> expr_internal.Expr:
231232
"""Return the internal expression from ``Expr`` or raise ``TypeError``.
232233
233234
This helper rejects plain strings and other non-:class:`Expr` values so
@@ -249,7 +250,7 @@ def ensure_expr(value: Expr | Any) -> expr_internal.Expr:
249250

250251

251252
def ensure_expr_list(
252-
exprs: Iterable[Expr | Iterable[Expr]],
253+
exprs: Iterable[_typing.Union["Expr", Iterable["Expr"]]],
253254
) -> list[expr_internal.Expr]:
254255
"""Flatten an iterable of expressions, validating each via ``ensure_expr``.
255256
@@ -263,7 +264,7 @@ def ensure_expr_list(
263264
TypeError: If any item is not an instance of :class:`Expr`.
264265
"""
265266

266-
def _iter(items: Iterable[Expr | Iterable[Expr]]) -> Iterable[expr_internal.Expr]:
267+
def _iter(items: Iterable[_typing.Union["Expr", Iterable["Expr"]]]) -> Iterable[expr_internal.Expr]:
267268
for expr in items:
268269
if isinstance(expr, Iterable) and not isinstance(
269270
expr, (Expr, str, bytes, bytearray)
@@ -276,7 +277,7 @@ def _iter(items: Iterable[Expr | Iterable[Expr]]) -> Iterable[expr_internal.Expr
276277
return list(_iter(exprs))
277278

278279

279-
def _to_raw_expr(value: Expr | str) -> expr_internal.Expr:
280+
def _to_raw_expr(value: _typing.Union["Expr", str]) -> expr_internal.Expr:
280281
"""Convert a Python expression or column name to its raw variant.
281282
282283
Args:
@@ -300,7 +301,7 @@ def _to_raw_expr(value: Expr | str) -> expr_internal.Expr:
300301

301302

302303
def expr_list_to_raw_expr_list(
303-
expr_list: Optional[Sequence[Expr | str] | Expr | str],
304+
expr_list: Optional[_typing.Union[Sequence[_typing.Union["Expr", str]], "Expr", str]],
304305
) -> Optional[list[expr_internal.Expr]]:
305306
"""Convert a sequence of expressions or column names to raw expressions."""
306307
if isinstance(expr_list, (Expr, str)):
@@ -310,15 +311,15 @@ def expr_list_to_raw_expr_list(
310311
return [_to_raw_expr(e) for e in expr_list]
311312

312313

313-
def sort_or_default(e: Expr | SortExpr) -> expr_internal.SortExpr:
314+
def sort_or_default(e: _typing.Union["Expr", "SortExpr"]) -> expr_internal.SortExpr:
314315
"""Helper function to return a default Sort if an Expr is provided."""
315316
if isinstance(e, SortExpr):
316317
return e.raw_sort
317318
return SortExpr(e, ascending=True, nulls_first=True).raw_sort
318319

319320

320321
def sort_list_to_raw_sort_list(
321-
sort_list: Optional[Sequence[SortKey] | SortKey],
322+
sort_list: Optional[_typing.Union[Sequence["SortKey"], "SortKey"]],
322323
) -> Optional[list[expr_internal.SortExpr]]:
323324
"""Helper function to return an optional sort list to raw variant."""
324325
if isinstance(sort_list, (Expr, SortExpr, str)):
@@ -447,7 +448,7 @@ def __invert__(self) -> Expr:
447448
"""Binary not (~)."""
448449
return Expr(self.expr.__invert__())
449450

450-
def __getitem__(self, key: str | int) -> Expr:
451+
def __getitem__(self, key: _typing.Union[str, int]) -> "Expr":
451452
"""Retrieve sub-object.
452453
453454
If ``key`` is a string, returns the subfield of the struct.
@@ -598,13 +599,13 @@ def is_not_null(self) -> Expr:
598599
"""Returns ``True`` if this expression is not null."""
599600
return Expr(self.expr.is_not_null())
600601

601-
def fill_nan(self, value: Any | Expr | None = None) -> Expr:
602+
def fill_nan(self, value: Optional[_typing.Union[Any, "Expr"]] = None) -> "Expr":
602603
"""Fill NaN values with a provided value."""
603604
if not isinstance(value, Expr):
604605
value = Expr.literal(value)
605606
return Expr(functions_internal.nanvl(self.expr, value.expr))
606607

607-
def fill_null(self, value: Any | Expr | None = None) -> Expr:
608+
def fill_null(self, value: Optional[_typing.Union[Any, "Expr"]] = None) -> "Expr":
608609
"""Fill NULL values with a provided value."""
609610
if not isinstance(value, Expr):
610611
value = Expr.literal(value)
@@ -617,7 +618,7 @@ def fill_null(self, value: Any | Expr | None = None) -> Expr:
617618
bool: pa.bool_(),
618619
}
619620

620-
def cast(self, to: pa.DataType[Any] | type[float | int | str | bool]) -> Expr:
621+
def cast(self, to: _typing.Union[pa.DataType[Any], type]) -> "Expr":
621622
"""Cast to a new data type."""
622623
if not isinstance(to, pa.DataType):
623624
try:
@@ -690,7 +691,7 @@ def column_name(self, plan: LogicalPlan) -> str:
690691
"""Compute the output column name based on the provided logical plan."""
691692
return self.expr.column_name(plan._raw_plan)
692693

693-
def order_by(self, *exprs: Expr | SortExpr) -> ExprFuncBuilder:
694+
def order_by(self, *exprs: _typing.Union["Expr", "SortExpr"]) -> ExprFuncBuilder:
694695
"""Set the ordering for a window or aggregate function.
695696
696697
This function will create an :py:class:`ExprFuncBuilder` that can be used to
@@ -1239,9 +1240,9 @@ class Window:
12391240

12401241
def __init__(
12411242
self,
1242-
partition_by: Optional[list[Expr] | Expr] = None,
1243+
partition_by: Optional[_typing.Union[list["Expr"], "Expr"]] = None,
12431244
window_frame: Optional[WindowFrame] = None,
1244-
order_by: Optional[list[SortExpr | Expr | str] | Expr | SortExpr | str] = None,
1245+
order_by: Optional[_typing.Union[list[_typing.Union["SortExpr", "Expr", str]], "Expr", "SortExpr", str]] = None,
12451246
null_treatment: Optional[NullTreatment] = None,
12461247
) -> None:
12471248
"""Construct a window definition.
@@ -1312,7 +1313,7 @@ def __init__(self, frame_bound: expr_internal.WindowFrameBound) -> None:
13121313
"""Constructs a window frame bound."""
13131314
self.frame_bound = frame_bound
13141315

1315-
def get_offset(self) -> int | None:
1316+
def get_offset(self) -> Optional[int]:
13161317
"""Returns the offset of the window frame."""
13171318
return self.frame_bound.get_offset()
13181319

@@ -1396,4 +1397,4 @@ def __repr__(self) -> str:
13961397
return self.raw_sort.__repr__()
13971398

13981399

1399-
SortKey = Expr | SortExpr | str
1400+
SortKey = _typing.Union[Expr, SortExpr, str]

0 commit comments

Comments
 (0)