Skip to content

Commit 520c0d0

Browse files
committed
Now that we are on Python 3.10 change from union and otional to |
1 parent 51dc78a commit 520c0d0

File tree

6 files changed

+129
-141
lines changed

6 files changed

+129
-141
lines changed

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,8 @@ ignore = [
8484
"FBT002", # Allow boolean positional args
8585
"ISC001", # Recommended to ignore these rules when using with ruff-format
8686
"SLF001", # Allow accessing private members
87-
"TD002",
87+
"TD002", # Do not require author names in TODO statements
8888
"TD003", # Allow TODO lines
89-
"UP007", # Disallowing Union is pedantic
9089
# TODO: Enable all of the following, but this PR is getting too large already
9190
"PLR0913",
9291
"TRY003",

python/datafusion/dataframe.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
Any,
2929
Iterable,
3030
Literal,
31-
Optional,
32-
Union,
3331
overload,
3432
)
3533

@@ -107,7 +105,7 @@ def from_str(cls: type[Compression], value: str) -> Compression:
107105
"""
108106
raise ValueError(error_msg) from err
109107

110-
def get_default_level(self) -> Optional[int]:
108+
def get_default_level(self) -> int | None:
111109
"""Get the default compression level for the compression type.
112110
113111
Returns:
@@ -140,24 +138,24 @@ def __init__(
140138
write_batch_size: int = 1024,
141139
writer_version: str = "1.0",
142140
skip_arrow_metadata: bool = False,
143-
compression: Optional[str] = "zstd(3)",
144-
compression_level: Optional[int] = None,
145-
dictionary_enabled: Optional[bool] = True,
141+
compression: str | None = "zstd(3)",
142+
compression_level: int | None = None,
143+
dictionary_enabled: bool | None = True,
146144
dictionary_page_size_limit: int = 1024 * 1024,
147-
statistics_enabled: Optional[str] = "page",
145+
statistics_enabled: str | None = "page",
148146
max_row_group_size: int = 1024 * 1024,
149147
created_by: str = "datafusion-python",
150-
column_index_truncate_length: Optional[int] = 64,
151-
statistics_truncate_length: Optional[int] = None,
148+
column_index_truncate_length: int | None = 64,
149+
statistics_truncate_length: int | None = None,
152150
data_page_row_count_limit: int = 20_000,
153-
encoding: Optional[str] = None,
151+
encoding: str | None = None,
154152
bloom_filter_on_write: bool = False,
155-
bloom_filter_fpp: Optional[float] = None,
156-
bloom_filter_ndv: Optional[int] = None,
153+
bloom_filter_fpp: float | None = None,
154+
bloom_filter_ndv: int | None = None,
157155
allow_single_file_parallelism: bool = True,
158156
maximum_parallel_row_group_writers: int = 1,
159157
maximum_buffered_record_batches_per_stream: int = 2,
160-
column_specific_options: Optional[dict[str, ParquetColumnOptions]] = None,
158+
column_specific_options: dict[str, ParquetColumnOptions] | None = None,
161159
) -> None:
162160
"""Initialize the ParquetWriterOptions.
163161
@@ -262,13 +260,13 @@ class ParquetColumnOptions:
262260

263261
def __init__(
264262
self,
265-
encoding: Optional[str] = None,
266-
dictionary_enabled: Optional[bool] = None,
267-
compression: Optional[str] = None,
268-
statistics_enabled: Optional[str] = None,
269-
bloom_filter_enabled: Optional[bool] = None,
270-
bloom_filter_fpp: Optional[float] = None,
271-
bloom_filter_ndv: Optional[int] = None,
263+
encoding: str | None = None,
264+
dictionary_enabled: bool | None = None,
265+
compression: str | None = None,
266+
statistics_enabled: str | None = None,
267+
bloom_filter_enabled: bool | None = None,
268+
bloom_filter_fpp: float | None = None,
269+
bloom_filter_ndv: int | None = None,
272270
) -> None:
273271
"""Initialize the ParquetColumnOptions.
274272
@@ -1063,7 +1061,7 @@ def write_parquet(
10631061
def write_parquet(
10641062
self,
10651063
path: str | pathlib.Path,
1066-
compression: Union[str, Compression, ParquetWriterOptions] = Compression.ZSTD,
1064+
compression: str | Compression | ParquetWriterOptions = Compression.ZSTD,
10671065
compression_level: int | None = None,
10681066
write_options: DataFrameWriteOptions | None = None,
10691067
) -> None:

python/datafusion/dataframe_formatter.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from typing import (
2222
Any,
2323
Callable,
24-
Optional,
2524
Protocol,
2625
runtime_checkable,
2726
)
@@ -144,9 +143,9 @@ def __init__(
144143
min_rows_display: int = 20,
145144
repr_rows: int = 10,
146145
enable_cell_expansion: bool = True,
147-
custom_css: Optional[str] = None,
146+
custom_css: str | None = None,
148147
show_truncation_message: bool = True,
149-
style_provider: Optional[StyleProvider] = None,
148+
style_provider: StyleProvider | None = None,
150149
use_shared_styles: bool = True,
151150
) -> None:
152151
"""Initialize the HTML formatter.
@@ -226,8 +225,8 @@ def __init__(
226225
# Registry for custom type formatters
227226
self._type_formatters: dict[type, CellFormatter] = {}
228227
# Custom cell builders
229-
self._custom_cell_builder: Optional[Callable[[Any, int, int, str], str]] = None
230-
self._custom_header_builder: Optional[Callable[[Any], str]] = None
228+
self._custom_cell_builder: Callable[[Any, int, int, str], str] | None = None
229+
self._custom_header_builder: Callable[[Any], str] | None = None
231230

232231
def register_formatter(self, type_class: type, formatter: CellFormatter) -> None:
233232
"""Register a custom formatter for a specific data type.

python/datafusion/expr.py

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

2323
from __future__ import annotations
2424

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

2827
try:
2928
from warnings import deprecated # Python 3.13+
@@ -230,7 +229,7 @@
230229
]
231230

232231

233-
def ensure_expr(value: _typing.Union[Expr, Any]) -> expr_internal.Expr:
232+
def ensure_expr(value: Expr | Any) -> expr_internal.Expr:
234233
"""Return the internal expression from ``Expr`` or raise ``TypeError``.
235234
236235
This helper rejects plain strings and other non-:class:`Expr` values so
@@ -252,7 +251,7 @@ def ensure_expr(value: _typing.Union[Expr, Any]) -> expr_internal.Expr:
252251

253252

254253
def ensure_expr_list(
255-
exprs: Iterable[_typing.Union[Expr, Iterable[Expr]]],
254+
exprs: Iterable[Expr | Iterable[Expr]],
256255
) -> list[expr_internal.Expr]:
257256
"""Flatten an iterable of expressions, validating each via ``ensure_expr``.
258257
@@ -267,7 +266,7 @@ def ensure_expr_list(
267266
"""
268267

269268
def _iter(
270-
items: Iterable[_typing.Union[Expr, Iterable[Expr]]],
269+
items: Iterable[Expr | Iterable[Expr]],
271270
) -> Iterable[expr_internal.Expr]:
272271
for expr in items:
273272
if isinstance(expr, Iterable) and not isinstance(
@@ -281,7 +280,7 @@ def _iter(
281280
return list(_iter(exprs))
282281

283282

284-
def _to_raw_expr(value: _typing.Union[Expr, str]) -> expr_internal.Expr:
283+
def _to_raw_expr(value: Expr | str) -> expr_internal.Expr:
285284
"""Convert a Python expression or column name to its raw variant.
286285
287286
Args:
@@ -305,8 +304,8 @@ def _to_raw_expr(value: _typing.Union[Expr, str]) -> expr_internal.Expr:
305304

306305

307306
def expr_list_to_raw_expr_list(
308-
expr_list: Optional[list[Expr] | Expr],
309-
) -> Optional[list[expr_internal.Expr]]:
307+
expr_list: list[Expr] | Expr | None,
308+
) -> list[expr_internal.Expr] | None:
310309
"""Convert a sequence of expressions or column names to raw expressions."""
311310
if isinstance(expr_list, Expr | str):
312311
expr_list = [expr_list]
@@ -315,16 +314,16 @@ def expr_list_to_raw_expr_list(
315314
return [_to_raw_expr(e) for e in expr_list]
316315

317316

318-
def sort_or_default(e: _typing.Union[Expr, SortExpr]) -> expr_internal.SortExpr:
317+
def sort_or_default(e: Expr | SortExpr) -> expr_internal.SortExpr:
319318
"""Helper function to return a default Sort if an Expr is provided."""
320319
if isinstance(e, SortExpr):
321320
return e.raw_sort
322321
return SortExpr(e, ascending=True, nulls_first=True).raw_sort
323322

324323

325324
def sort_list_to_raw_sort_list(
326-
sort_list: Optional[_typing.Union[Sequence[SortKey], SortKey]],
327-
) -> Optional[list[expr_internal.SortExpr]]:
325+
sort_list: Sequence[SortKey] | SortKey | None,
326+
) -> list[expr_internal.SortExpr] | None:
328327
"""Helper function to return an optional sort list to raw variant."""
329328
if isinstance(sort_list, Expr | SortExpr | str):
330329
sort_list = [sort_list]
@@ -601,7 +600,7 @@ def column(value: str) -> Expr:
601600
"""Creates a new expression representing a column."""
602601
return Expr(expr_internal.RawExpr.column(value))
603602

604-
def alias(self, name: str, metadata: Optional[dict[str, str]] = None) -> Expr:
603+
def alias(self, name: str, metadata: dict[str, str] | None = None) -> Expr:
605604
"""Assign a name to the expression.
606605
607606
Args:
@@ -630,13 +629,13 @@ def is_not_null(self) -> Expr:
630629
"""Returns ``True`` if this expression is not null."""
631630
return Expr(self.expr.is_not_null())
632631

633-
def fill_nan(self, value: Optional[_typing.Union[Any, Expr]] = None) -> Expr:
632+
def fill_nan(self, value: Any | Expr | None = None) -> Expr:
634633
"""Fill NaN values with a provided value."""
635634
if not isinstance(value, Expr):
636635
value = Expr.literal(value)
637636
return Expr(functions_internal.nanvl(self.expr, value.expr))
638637

639-
def fill_null(self, value: Optional[_typing.Union[Any, Expr]] = None) -> Expr:
638+
def fill_null(self, value: Any | Expr | None = None) -> Expr:
640639
"""Fill NULL values with a provided value."""
641640
if not isinstance(value, Expr):
642641
value = Expr.literal(value)
@@ -649,7 +648,7 @@ def fill_null(self, value: Optional[_typing.Union[Any, Expr]] = None) -> Expr:
649648
bool: pa.bool_(),
650649
}
651650

652-
def cast(self, to: _typing.Union[pa.DataType[Any], type]) -> Expr:
651+
def cast(self, to: pa.DataType[Any] | type) -> Expr:
653652
"""Cast to a new data type."""
654653
if not isinstance(to, pa.DataType):
655654
try:
@@ -722,7 +721,7 @@ def column_name(self, plan: LogicalPlan) -> str:
722721
"""Compute the output column name based on the provided logical plan."""
723722
return self.expr.column_name(plan._raw_plan)
724723

725-
def order_by(self, *exprs: _typing.Union[Expr, SortExpr]) -> ExprFuncBuilder:
724+
def order_by(self, *exprs: Expr | SortExpr) -> ExprFuncBuilder:
726725
"""Set the ordering for a window or aggregate function.
727726
728727
This function will create an :py:class:`ExprFuncBuilder` that can be used to
@@ -1271,17 +1270,10 @@ class Window:
12711270

12721271
def __init__(
12731272
self,
1274-
partition_by: Optional[_typing.Union[list[Expr], Expr]] = None,
1275-
window_frame: Optional[WindowFrame] = None,
1276-
order_by: Optional[
1277-
_typing.Union[
1278-
list[_typing.Union[SortExpr, Expr, str]],
1279-
Expr,
1280-
SortExpr,
1281-
str,
1282-
]
1283-
] = None,
1284-
null_treatment: Optional[NullTreatment] = None,
1273+
partition_by: list[Expr] | Expr | None = None,
1274+
window_frame: WindowFrame | None = None,
1275+
order_by: list[SortExpr | Expr | str] | Expr | SortExpr | str | None = None,
1276+
null_treatment: NullTreatment | None = None,
12851277
) -> None:
12861278
"""Construct a window definition.
12871279
@@ -1301,7 +1293,7 @@ class WindowFrame:
13011293
"""Defines a window frame for performing window operations."""
13021294

13031295
def __init__(
1304-
self, units: str, start_bound: Optional[Any], end_bound: Optional[Any]
1296+
self, units: str, start_bound: Any | None, end_bound: Any | None
13051297
) -> None:
13061298
"""Construct a window frame using the given parameters.
13071299
@@ -1351,7 +1343,7 @@ def __init__(self, frame_bound: expr_internal.WindowFrameBound) -> None:
13511343
"""Constructs a window frame bound."""
13521344
self.frame_bound = frame_bound
13531345

1354-
def get_offset(self) -> Optional[int]:
1346+
def get_offset(self) -> int | None:
13551347
"""Returns the offset of the window frame."""
13561348
return self.frame_bound.get_offset()
13571349

@@ -1435,4 +1427,4 @@ def __repr__(self) -> str:
14351427
return self.raw_sort.__repr__()
14361428

14371429

1438-
SortKey = _typing.Union[Expr, SortExpr, str]
1430+
SortKey = Expr | SortExpr | str

0 commit comments

Comments
 (0)