3232except ImportError :
3333 from typing_extensions import deprecated # Python 3.12
3434
35- from datafusion .common import DataTypeMap , NullTreatment , RexType
35+ from datafusion .common import NullTreatment
3636
3737from ._internal import expr as expr_internal
3838from ._internal import functions as functions_internal
3939
4040if TYPE_CHECKING :
4141 from collections .abc import Sequence
4242
43+ # These are only imported for type checking to avoid runtime
44+ # evaluation issues with typing constructs.
4345 from datafusion .common import DataTypeMap , RexType
46+ # Make the datafusion package available to type checkers for
47+ # fully-qualified string-literal annotations.
48+ import datafusion # type: ignore
4449 from datafusion .plan import LogicalPlan
50+ # Note: DataTypeMap and RexType are only available for type checking.
51+ # We intentionally avoid importing them at runtime to prevent evaluation
52+ # issues with complex typing constructs.
4553
4654
4755# Standard error message for invalid expression types
228236]
229237
230238
231- def ensure_expr (value : _typing .Union [" Expr" , Any ]) -> expr_internal .Expr :
239+ def ensure_expr (value : _typing .Union [Expr , Any ]) -> expr_internal .Expr :
232240 """Return the internal expression from ``Expr`` or raise ``TypeError``.
233241
234242 This helper rejects plain strings and other non-:class:`Expr` values so
@@ -250,7 +258,7 @@ def ensure_expr(value: _typing.Union["Expr", Any]) -> expr_internal.Expr:
250258
251259
252260def ensure_expr_list (
253- exprs : Iterable [_typing .Union [" Expr" , Iterable [" Expr" ]]],
261+ exprs : Iterable [_typing .Union [Expr , Iterable [Expr ]]],
254262) -> list [expr_internal .Expr ]:
255263 """Flatten an iterable of expressions, validating each via ``ensure_expr``.
256264
@@ -264,7 +272,9 @@ def ensure_expr_list(
264272 TypeError: If any item is not an instance of :class:`Expr`.
265273 """
266274
267- def _iter (items : Iterable [_typing .Union ["Expr" , Iterable ["Expr" ]]]) -> Iterable [expr_internal .Expr ]:
275+ def _iter (
276+ items : Iterable [_typing .Union [Expr , Iterable [Expr ]]],
277+ ) -> Iterable [expr_internal .Expr ]:
268278 for expr in items :
269279 if isinstance (expr , Iterable ) and not isinstance (
270280 expr , (Expr , str , bytes , bytearray )
@@ -277,7 +287,7 @@ def _iter(items: Iterable[_typing.Union["Expr", Iterable["Expr"]]]) -> Iterable[
277287 return list (_iter (exprs ))
278288
279289
280- def _to_raw_expr (value : _typing .Union [" Expr" , str ]) -> expr_internal .Expr :
290+ def _to_raw_expr (value : _typing .Union [Expr , str ]) -> expr_internal .Expr :
281291 """Convert a Python expression or column name to its raw variant.
282292
283293 Args:
@@ -301,7 +311,7 @@ def _to_raw_expr(value: _typing.Union["Expr", str]) -> expr_internal.Expr:
301311
302312
303313def expr_list_to_raw_expr_list (
304- expr_list : Optional [_typing .Union [Sequence [_typing .Union [" Expr" , str ]], " Expr" , str ]],
314+ expr_list : Optional [_typing .Union [Sequence [_typing .Union [Expr , str ]], Expr , str ]],
305315) -> Optional [list [expr_internal .Expr ]]:
306316 """Convert a sequence of expressions or column names to raw expressions."""
307317 if isinstance (expr_list , (Expr , str )):
@@ -311,15 +321,15 @@ def expr_list_to_raw_expr_list(
311321 return [_to_raw_expr (e ) for e in expr_list ]
312322
313323
314- def sort_or_default (e : _typing .Union [" Expr" , " SortExpr" ]) -> expr_internal .SortExpr :
324+ def sort_or_default (e : _typing .Union [Expr , SortExpr ]) -> expr_internal .SortExpr :
315325 """Helper function to return a default Sort if an Expr is provided."""
316326 if isinstance (e , SortExpr ):
317327 return e .raw_sort
318328 return SortExpr (e , ascending = True , nulls_first = True ).raw_sort
319329
320330
321331def sort_list_to_raw_sort_list (
322- sort_list : Optional [_typing .Union [Sequence [" SortKey" ], " SortKey" ]],
332+ sort_list : Optional [_typing .Union [Sequence [SortKey ], SortKey ]],
323333) -> Optional [list [expr_internal .SortExpr ]]:
324334 """Helper function to return an optional sort list to raw variant."""
325335 if isinstance (sort_list , (Expr , SortExpr , str )):
@@ -448,7 +458,7 @@ def __invert__(self) -> Expr:
448458 """Binary not (~)."""
449459 return Expr (self .expr .__invert__ ())
450460
451- def __getitem__ (self , key : _typing .Union [str , int ]) -> " Expr" :
461+ def __getitem__ (self , key : _typing .Union [str , int ]) -> Expr :
452462 """Retrieve sub-object.
453463
454464 If ``key`` is a string, returns the subfield of the struct.
@@ -599,13 +609,13 @@ def is_not_null(self) -> Expr:
599609 """Returns ``True`` if this expression is not null."""
600610 return Expr (self .expr .is_not_null ())
601611
602- def fill_nan (self , value : Optional [_typing .Union [Any , " Expr" ]] = None ) -> " Expr" :
612+ def fill_nan (self , value : Optional [_typing .Union [Any , Expr ]] = None ) -> Expr :
603613 """Fill NaN values with a provided value."""
604614 if not isinstance (value , Expr ):
605615 value = Expr .literal (value )
606616 return Expr (functions_internal .nanvl (self .expr , value .expr ))
607617
608- def fill_null (self , value : Optional [_typing .Union [Any , " Expr" ]] = None ) -> " Expr" :
618+ def fill_null (self , value : Optional [_typing .Union [Any , Expr ]] = None ) -> Expr :
609619 """Fill NULL values with a provided value."""
610620 if not isinstance (value , Expr ):
611621 value = Expr .literal (value )
@@ -618,7 +628,7 @@ def fill_null(self, value: Optional[_typing.Union[Any, "Expr"]] = None) -> "Expr
618628 bool : pa .bool_ (),
619629 }
620630
621- def cast (self , to : _typing .Union [pa .DataType [Any ], type ]) -> " Expr" :
631+ def cast (self , to : _typing .Union [pa .DataType [Any ], type ]) -> Expr :
622632 """Cast to a new data type."""
623633 if not isinstance (to , pa .DataType ):
624634 try :
@@ -645,7 +655,7 @@ def between(self, low: Any, high: Any, negated: bool = False) -> Expr:
645655
646656 return Expr (self .expr .between (low .expr , high .expr , negated = negated ))
647657
648- def rex_type (self ) -> RexType :
658+ def rex_type (self ) -> "datafusion.common. RexType" :
649659 """Return the Rex Type of this expression.
650660
651661 A Rex (Row Expression) specifies a single row of data.That specification
@@ -654,7 +664,7 @@ def rex_type(self) -> RexType:
654664 """
655665 return self .expr .rex_type ()
656666
657- def types (self ) -> DataTypeMap :
667+ def types (self ) -> "datafusion.common. DataTypeMap" :
658668 """Return the ``DataTypeMap``.
659669
660670 Returns:
@@ -691,7 +701,7 @@ def column_name(self, plan: LogicalPlan) -> str:
691701 """Compute the output column name based on the provided logical plan."""
692702 return self .expr .column_name (plan ._raw_plan )
693703
694- def order_by (self , * exprs : _typing .Union [" Expr" , " SortExpr" ]) -> ExprFuncBuilder :
704+ def order_by (self , * exprs : _typing .Union [Expr , SortExpr ]) -> ExprFuncBuilder :
695705 """Set the ordering for a window or aggregate function.
696706
697707 This function will create an :py:class:`ExprFuncBuilder` that can be used to
@@ -1240,9 +1250,16 @@ class Window:
12401250
12411251 def __init__ (
12421252 self ,
1243- partition_by : Optional [_typing .Union [list [" Expr" ], " Expr" ]] = None ,
1253+ partition_by : Optional [_typing .Union [list [Expr ], Expr ]] = None ,
12441254 window_frame : Optional [WindowFrame ] = None ,
1245- order_by : Optional [_typing .Union [list [_typing .Union ["SortExpr" , "Expr" , str ]], "Expr" , "SortExpr" , str ]] = None ,
1255+ order_by : Optional [
1256+ _typing .Union [
1257+ list [_typing .Union [SortExpr , Expr , str ]],
1258+ Expr ,
1259+ SortExpr ,
1260+ str ,
1261+ ]
1262+ ] = None ,
12461263 null_treatment : Optional [NullTreatment ] = None ,
12471264 ) -> None :
12481265 """Construct a window definition.
0 commit comments