Used TypeDict for CachingVisitor.state#19135
Used TypeDict for CachingVisitor.state#19135rapids-bot[bot] merged 29 commits intorapidsai:branch-25.08from
Conversation
|
Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually. Contributors can view more details about this message here. |
35f9909 to
1f6bcac
Compare
This changes `CachingVisitor.state` to used a `TypedDict`. Previously, we used a `Mapping[str, Any]`, which had two problems: 1. Risks typos in the key names causing unexpected KeyErrors 2. The `Any` type for the values of `state` mean that, at least by default, you won't get any type checking on things using values looked up from `state`. Unit tests should catch all these, but this can provide some earlier feedback on any issues. The `total=False` keyword is used in the typeddict to indicate that these keys are all optional. `mypy` *doesn't* error if you do a lookup on an optional field of a TypedDict, so we do still need to handle missing keys like we have been.
1f6bcac to
cf2a77b
Compare
|
Hmm, failing on 3.10: https://github.com/rapidsai/cudf/actions/runs/15594634303/job/43923137155?pr=19135#step:10:7551 This might need to wait until we drop Python 3.10. |
|
Just seeing if CI passes with |
|
I'd like to add a dependency on diff --git a/conda/recipes/cudf-polars/recipe.yaml b/conda/recipes/cudf-polars/recipe.yaml
index 85740e5930..8133c7e8a5 100644
--- a/conda/recipes/cudf-polars/recipe.yaml
+++ b/conda/recipes/cudf-polars/recipe.yaml
@@ -50,6 +50,7 @@ requirements:
- python
- pylibcudf =${{ version }}
- polars >=1.24,<1.31
+ - typing-extensions # py==310
- ${{ pin_compatible("cuda-version", upper_bound="x", lower_bound="x") }}
ignore_run_exports:
by_name:
diff --git a/python/cudf_polars/pyproject.toml b/python/cudf_polars/pyproject.toml
index b3d28e027d..9589a8a3f9 100644
--- a/python/cudf_polars/pyproject.toml
+++ b/python/cudf_polars/pyproject.toml
@@ -21,6 +21,7 @@ requires-python = ">=3.10"
dependencies = [
"polars>=1.25,<1.31",
"pylibcudf==25.8.*,>=0.0.0a0",
+ "typing-extensions; python_version < '3.11'",
] # This list was generated by `rapids-dependency-file-generator`. To make changes, edit ../../dependencies.yaml and run `rapids-dependency-file-generator`.
classifiers = [
"Intended Audience :: Developers",I'd hoped that 4ed5168 would do the trick, but that gives the same output as what's on 25.08 (no change). |
|
Hey @TomAugspurger -- I'm not sure if the dependency file generator knows how to handle version-specific dependencies in a I think it's reasonable to do something like: @@ -834,13 +834,11 @@ dependencies:
packages:
- polars>=1.25,<1.31
specific:
- - output_types: [conda, requirements, pyproject]
+ - output_types: pyproject
matrices:
- - matrix: {py: "3.10"}
- packages:
- - typing-extensions
- matrix: null
- packages: null
+ packages:
+ - "typing-extensions; python_version < '3.11'"
run_cudf_polars_experimental:
common:
- output_types: [conda, requirements, pyproject]The other spot where As for the |
I'll defer to you on whether the extra effort is worth it, and I'll be happy to review. |
- Move State dicts to where they're used - Move Transformer subclasses to where they're used
wence-
left a comment
There was a problem hiding this comment.
I think this looks close to ready right now (hence approving).
I think we should be able to get away without explicitly constructing the TypedDict instances (see example).
|
/merge |
| fn: Callable[[U_contra, GenericTransformer[U_contra, V_co, StateT_co]], V_co], | ||
| *, | ||
| state: Mapping[str, Any] | None = None, | ||
| state: Mapping[str, Any], |
There was a problem hiding this comment.
Should have been StateT_co I think
There was a problem hiding this comment.
That errored with
python/cudf_polars/cudf_polars/dsl/traversal.py:128: error: Cannot use a covariant type variable as a parameter [misc]
Some discussion of this at python/mypy#7049, but I wasn't able to figure out a way around that.
There was a problem hiding this comment.
This is because mypy doesn't understand how we're using this state object and doesn't realise that this make_recursive is actually a type constructor rather than a regular function.
We could have fixed this by:
type: ignore (describing why)
defining a copy of CachingVisitor called make_recursive that is identically except for the lack of cache.
There was a problem hiding this comment.
Thanks, I'll fix get that fixed.
This changes
CachingVisitorto used aTypedDictfor state. Previously, we used aMapping[str, Any], which had two problems:Anytype for the values ofstatemeans that, at least by default, you won't get any type checking on things using values looked up fromstate.Unit tests should catch all these, but this can provide some earlier feedback on any issues.
Now, we'll declare a
StateTypedDictwith some specific fields for each transformer that usesCachingVisitor.