Skip to content

Commit 4858121

Browse files
authored
Polars spreadsheet type changes (#780)
* Fixes up type hints and imports for polars spreadsheet writer In python 3.9 at least things weren't working. The type hints for the spreadsheet writer use type aliases that require other imports to introspect apparently. That is `type_hints = typing.get_type_hints(cls)` was failing on that class. Adds test for this. * Adds missing deps to polars materializers example
1 parent 31da460 commit 4858121

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
polars
22
sf-hamilton
3+
xlsx2csv # for excel data loader
4+
xlsxwriter # Excel export requires 'xlsxwriter'

hamilton/plugins/polars_extensions.py

+19-16
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,6 @@
2222
except ImportError:
2323
Workbook = Type
2424

25-
26-
from polars.type_aliases import (
27-
ColumnFormatDict,
28-
ColumnTotalsDefinition,
29-
ColumnWidthsDefinition,
30-
ConditionalFormatDict,
31-
OneOrMoreDataTypes,
32-
RowTotalsDefinition,
33-
SelectorType,
34-
)
35-
3625
try:
3726
import polars as pl
3827
from polars import PolarsDataType
@@ -633,18 +622,32 @@ class PolarsSpreadsheetWriter(DataSaver):
633622
Should map to https://pola-rs.github.io/polars/py-polars/html/reference/api/polars.DataFrame.write_excel.html
634623
"""
635624

625+
# importing here because this is where it's used. Can move later.
626+
# but yeah the polars type aliases weren't resolving well in python 3.9
627+
# so stripped/reduced them appropriately.
628+
from polars.datatypes import DataType, DataTypeClass
629+
from polars.type_aliases import ColumnTotalsDefinition, RowTotalsDefinition
630+
636631
workbook: Union[Workbook, BytesIO, Path, str]
637632
worksheet: Union[str, None] = None
638633
# kwargs:
639634
position: Union[Tuple[int, int], str] = "A1"
640635
table_style: Union[str, Dict[str, Any], None] = None
641636
table_name: Union[str, None] = None
642-
column_formats: Union[ColumnFormatDict, None] = None
643-
dtype_formats: Union[Dict[OneOrMoreDataTypes, str], None] = None
644-
conditional_formats: Union[ConditionalFormatDict, None] = None
637+
column_formats: Union[
638+
Mapping[Union[str, Tuple[str, ...]], Union[str, Mapping[str, str]]], None
639+
] = None
640+
dtype_formats: Union[Dict[Union[DataType, DataTypeClass], str], None] = None
641+
conditional_formats: Union[
642+
Mapping[
643+
Union[str, Collection[str]],
644+
Union[str, Union[Mapping[str, Any], Sequence[Union[str, Mapping[str, Any]]]]],
645+
],
646+
None,
647+
] = None
645648
header_format: Union[Dict[str, Any], None] = None
646649
column_totals: Union[ColumnTotalsDefinition, None] = None
647-
column_widths: Union[ColumnWidthsDefinition, None] = None
650+
column_widths: Union[Mapping[str, Union[Tuple[str, ...], int]], int, None] = None
648651
row_totals: Union[RowTotalsDefinition, None] = None
649652
row_heights: Union[Dict[Union[int, Tuple[int, ...]], int], int, None] = None
650653
sparklines: Union[Dict[str, Union[Sequence[str], Dict[str, Any]]], None] = None
@@ -653,7 +656,7 @@ class PolarsSpreadsheetWriter(DataSaver):
653656
include_header: bool = True
654657
autofilter: bool = True
655658
autofit: bool = False
656-
hidden_columns: Union[Sequence[str], SelectorType, None] = None
659+
hidden_columns: Union[Sequence[str], str, None] = None
657660
hide_gridlines: bool = None
658661
sheet_zoom: Union[int, None] = None
659662
freeze_panes: Union[

tests/plugins/test_polars_extensions.py

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import io
12
import pathlib
23
import sys
4+
import typing
35

46
import polars as pl # isort: skip
57
import pytest # isort: skip
@@ -168,3 +170,9 @@ def test_polars_spreadsheet(df: pl.DataFrame, tmp_path: pathlib.Path) -> None:
168170
assert write_kwargs["include_header"] is True
169171
assert "raise_if_empty" in read_kwargs
170172
assert read_kwargs["raise_if_empty"] is True
173+
174+
175+
def test_getting_type_hints_spreadsheetwriter():
176+
"""Tests that types can be resolved at run time."""
177+
type_hints = typing.get_type_hints(PolarsSpreadsheetWriter)
178+
assert type_hints["workbook"] == typing.Union[typing.Type, io.BytesIO, pathlib.Path, str]

0 commit comments

Comments
 (0)