-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add Polars to mypy environment and fix errors #20563
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
40cfcf0
Add polars to environment
vyasr f49e912
Fix polars DataType attribute access with explicit casts
vyasr 5148072
Fix time_unit literal type errors in polars datatype.py
vyasr 8621aa3
Fix Decimal precision TypedDict error with version-gated workaround
vyasr 15114ff
Widen DataType.__init__ parameter type to accept PolarsDataType
vyasr 8a9e710
Fix parameterized literal type errors in test_window_functions
vyasr 5573cb1
Fix 3 mypy literal type errors in tests (Category E)
vyasr cdb548a
Add type ignores for monkey-patching in pytest plugin (Category G)
vyasr 02a5941
Add type ignores for dict unpacking in test assertions (Category F)
vyasr 61669ad
Fix final 5 mypy errors in cudf_polars with version-gated ignores
vyasr 616d708
Minor fixes
vyasr 972f170
Merge branch 'main' into fix/typing_polars
vyasr 263d673
Merge branch 'main' into fix/typing_polars
vyasr 767802b
Merge branch 'main' into fix/typing_polars
vyasr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,17 +6,20 @@ | |
| from __future__ import annotations | ||
|
|
||
| from functools import cache | ||
| from typing import TYPE_CHECKING | ||
| from typing import TYPE_CHECKING, Literal, cast | ||
|
|
||
| from typing_extensions import assert_never | ||
|
|
||
| import polars as pl | ||
|
|
||
| import pylibcudf as plc | ||
|
|
||
| from cudf_polars.utils.versions import POLARS_VERSION_LT_136 | ||
|
|
||
| if TYPE_CHECKING: | ||
| from cudf_polars.typing import ( | ||
| DataTypeHeader, | ||
| PolarsDataType, | ||
| ) | ||
|
|
||
| __all__ = ["DataType"] | ||
|
|
@@ -46,7 +49,18 @@ def _dtype_to_header(dtype: pl.DataType) -> DataTypeHeader: | |
| if name in SCALAR_NAME_TO_POLARS_TYPE_MAP: | ||
| return {"kind": "scalar", "name": name} | ||
| if isinstance(dtype, pl.Decimal): | ||
| return {"kind": "decimal", "precision": dtype.precision, "scale": dtype.scale} | ||
| # Workaround for incorrect polars stubs where precision is typed as int | None | ||
| # Fixed upstream: https://github.com/pola-rs/polars/pull/25227 | ||
| # TODO: Remove this workaround when polars >= 1.36 | ||
| if POLARS_VERSION_LT_136: | ||
| assert ( | ||
| dtype.precision is not None | ||
| ) # Decimal always has precision at runtime | ||
| return { | ||
| "kind": "decimal", | ||
| "precision": cast(int, dtype.precision), | ||
| "scale": dtype.scale, | ||
| } | ||
| if isinstance(dtype, pl.Datetime): | ||
| return { | ||
| "kind": "datetime", | ||
|
|
@@ -56,12 +70,17 @@ def _dtype_to_header(dtype: pl.DataType) -> DataTypeHeader: | |
| if isinstance(dtype, pl.Duration): | ||
| return {"kind": "duration", "time_unit": dtype.time_unit} | ||
| if isinstance(dtype, pl.List): | ||
| return {"kind": "list", "inner": _dtype_to_header(dtype.inner)} | ||
| # isinstance narrows dtype to pl.List, but .inner returns DataTypeClass | DataType | ||
| return { | ||
| "kind": "list", | ||
| "inner": _dtype_to_header(cast(pl.DataType, dtype.inner)), | ||
| } | ||
| if isinstance(dtype, pl.Struct): | ||
| # isinstance narrows dtype to pl.Struct, but field.dtype returns DataTypeClass | DataType | ||
| return { | ||
| "kind": "struct", | ||
| "fields": [ | ||
| {"name": f.name, "dtype": _dtype_to_header(f.dtype)} | ||
| {"name": f.name, "dtype": _dtype_to_header(cast(pl.DataType, f.dtype))} | ||
| for f in dtype.fields | ||
| ], | ||
| } | ||
|
|
@@ -78,9 +97,14 @@ def _dtype_from_header(header: DataTypeHeader) -> pl.DataType: | |
| if header["kind"] == "decimal": | ||
| return pl.Decimal(header["precision"], header["scale"]) | ||
| if header["kind"] == "datetime": | ||
| return pl.Datetime(time_unit=header["time_unit"], time_zone=header["time_zone"]) | ||
| return pl.Datetime( | ||
| time_unit=cast(Literal["ns", "us", "ms"], header["time_unit"]), | ||
| time_zone=header["time_zone"], | ||
| ) | ||
| if header["kind"] == "duration": | ||
| return pl.Duration(time_unit=header["time_unit"]) | ||
| return pl.Duration( | ||
| time_unit=cast(Literal["ns", "us", "ms"], header["time_unit"]) | ||
| ) | ||
| if header["kind"] == "list": | ||
| return pl.List(_dtype_from_header(header["inner"])) | ||
| if header["kind"] == "struct": | ||
|
|
@@ -182,9 +206,14 @@ class DataType: | |
| polars_type: pl.datatypes.DataType | ||
| plc_type: plc.DataType | ||
|
|
||
| def __init__(self, polars_dtype: pl.DataType) -> None: | ||
| self.polars_type = polars_dtype | ||
| self.plc_type = _from_polars(polars_dtype) | ||
| def __init__(self, polars_dtype: PolarsDataType) -> None: | ||
| # Convert DataTypeClass to DataType instance if needed | ||
| # polars allows both pl.Int64 (class) and pl.Int64() (instance) | ||
| if isinstance(polars_dtype, type): | ||
| polars_dtype = polars_dtype() | ||
| # After conversion, it's guaranteed to be a DataType instance | ||
| self.polars_type = cast(pl.DataType, polars_dtype) | ||
| self.plc_type = _from_polars(self.polars_type) | ||
|
|
||
| def id(self) -> plc.TypeId: | ||
| """The pylibcudf.TypeId of this DataType.""" | ||
|
|
@@ -193,12 +222,16 @@ def id(self) -> plc.TypeId: | |
| @property | ||
| def children(self) -> list[DataType]: | ||
| """The children types of this DataType.""" | ||
| # these type ignores are needed because the type checker doesn't | ||
| # see that these equality checks passing imply a specific type for each child field. | ||
| # Type checker doesn't narrow polars_type through plc_type.id() checks | ||
| if self.plc_type.id() == plc.TypeId.STRUCT: | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could consider implementing some TypeGuard functions for our DataType classes so that we can get better narrowing if others prefer that approach to casting like this. |
||
| return [DataType(field.dtype) for field in self.polars_type.fields] | ||
| # field.dtype returns DataTypeClass | DataType, need to cast to DataType | ||
| return [ | ||
| DataType(cast(pl.DataType, field.dtype)) | ||
| for field in cast(pl.Struct, self.polars_type).fields | ||
| ] | ||
| elif self.plc_type.id() == plc.TypeId.LIST: | ||
| return [DataType(self.polars_type.inner)] | ||
| # .inner returns DataTypeClass | DataType, need to cast to DataType | ||
| return [DataType(cast(pl.DataType, cast(pl.List, self.polars_type).inner))] | ||
| return [] | ||
|
|
||
| def scale(self) -> int: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.