Skip to content

Commit 3ccc1b8

Browse files
committed
other fixes
Signed-off-by: Matt Richards <[email protected]>
1 parent 3aa8d71 commit 3ccc1b8

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

pandera/engines/polars_engine.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import warnings
88
from typing import (
99
Any,
10-
Dict,
1110
Iterable,
1211
Literal,
1312
Mapping,
@@ -16,12 +15,14 @@
1615
Tuple,
1716
Type,
1817
Union,
18+
TypedDict,
1919
)
2020

2121
import polars as pl
2222
from packaging import version
2323
from polars._typing import ColumnNameOrSelector, PythonDataType
2424
from polars.datatypes import DataTypeClass
25+
from typing_extensions import NotRequired
2526

2627
from pandera import dtypes, errors
2728
from pandera.api.polars.types import PolarsData
@@ -38,7 +39,6 @@
3839
pl.exceptions.ComputeError,
3940
)
4041

41-
4242
SchemaDict = Mapping[str, PolarsDataType]
4343

4444

@@ -565,6 +565,11 @@ def from_parametrized_dtype(cls, polars_dtype: pl.Duration):
565565
###############################################################################
566566

567567

568+
class _ArrayKwargs(TypedDict):
569+
shape: NotRequired[Union[int | tuple[int, ...]]]
570+
width: NotRequired[Union[int, None]]
571+
572+
568573
@Engine.register_dtype(equivalents=[pl.Array])
569574
@immutable(init=True)
570575
class Array(DataType):
@@ -580,13 +585,15 @@ def __init__( # pylint:disable=super-init-not-called
580585
width: Optional[int] = None,
581586
) -> None:
582587

583-
kwargs: Dict[str, Union[int, Tuple[int, ...]]] = {}
584-
if width is not None:
588+
kwargs: _ArrayKwargs = {}
589+
if (
590+
width is not None
591+
): # width deprecated in polars 0.20.31, replaced by shape
585592
kwargs["shape"] = width
586593
elif shape is not None:
587594
kwargs["shape"] = shape
588595

589-
if inner or shape or width:
596+
if inner and (shape or width):
590597
object.__setattr__(self, "type", pl.Array(inner=inner, **kwargs))
591598

592599
@classmethod
@@ -677,11 +684,9 @@ class Categorical(DataType):
677684

678685
type = pl.Categorical
679686

680-
ordering = None
681-
682687
def __init__( # pylint:disable=super-init-not-called
683688
self,
684-
ordering: Literal["physical", "lexical"] = "physical",
689+
ordering: Optional[Literal["physical", "lexical"]] = "physical",
685690
) -> None:
686691
object.__setattr__(self, "ordering", ordering)
687692
object.__setattr__(self, "type", pl.Categorical(ordering=ordering))
@@ -706,8 +711,9 @@ def __init__( # pylint:disable=super-init-not-called
706711
self,
707712
categories: Union[pl.Series, Iterable[str], None] = None,
708713
) -> None:
709-
object.__setattr__(self, "categories", categories)
710-
object.__setattr__(self, "type", pl.Enum(categories=categories))
714+
if categories is not None:
715+
object.__setattr__(self, "categories", categories)
716+
object.__setattr__(self, "type", pl.Enum(categories=categories))
711717

712718
@classmethod
713719
def from_parametrized_dtype(cls, polars_dtype: pl.Enum):
@@ -781,8 +787,10 @@ def try_coerce(self, data_container: PolarsDataContainer) -> pl.LazyFrame:
781787
return self.coerce(data_container)
782788
except Exception as exc: # pylint:disable=broad-except
783789
is_coercible: pl.LazyFrame = polars_object_coercible(
784-
data_container, self.type
785-
) & self.__belongs_to_categories(
790+
data_container,
791+
self.type,
792+
# TODO this is incorrect, appears not to be covered by tests
793+
) & self.__belongs_to_categories( # type: ignore # TEMP ONLY!
786794
data_container.lazyframe, key=data_container.key
787795
)
788796

@@ -800,7 +808,9 @@ def __belongs_to_categories(
800808
lf: pl.LazyFrame,
801809
key: Optional[str] = None,
802810
) -> pl.LazyFrame:
803-
return lf.select(pl.col(key or "*").is_in(self.categories))
811+
# self.categories not None here.
812+
expr = pl.col(key or "*").is_in(self.categories) # type: ignore[arg-type]
813+
return lf.select(expr)
804814

805815
def __str__(self):
806816
return "Category"

0 commit comments

Comments
 (0)