Repository navigation
Solve dataclass type variables from field defaults - #5672
sathariels wants to merge 2 commits into
Conversation
rchen152
left a comment
There was a problem hiding this comment.
Thanks for the PR! Would you mind rebasing and fixing the merge conflict? It looks like CI never ran (we've been working through some fallout from moving the pyrefly repo from the meta org to pyrefly-org, so the missing CI run might be from that); a rebase should trigger a run.
Meanwhile, here's some quick initial feedback. Take it with a grain of salt because I wasn't able to run the code and didn't have CI results to work off of either.
The core idea looks right, but I'd structure the code differently:
- Move the default handling in
get_requiredness(pyrefly/lib/alt/function.rs: the upper-bound check, the re-inference with a hint, and the fallback for incompatible defaults) into one helper. Call it for every field default, both plain assignments and field specifiers. Right nowfield(default=...)anddefault_factoryskip those steps. - Store the result in a type that tells "has a default of unknown type" apart from "default with a known type", so
Anyno longer stands for both. - Have
as_paramsimply convert the stored value toRequired.
The PR would still have to touch all of the places it currently does, but the logic would be less fragmented/duplicated.
A generated constructor recorded that a field had a default but dropped the value, so Generated() left T unsolved. The synthesized __init__ now keeps that default and uses it the same way a handwritten constructor does.
aa8d9a9 to
affd6a3
Compare
|
@rchen152 Thanks for the notes. I rebased onto current main and pushed the update, so CI should run this time. I pulled the default check out of get_requiredness into one helper and use it for plain assignments, field(default=...), and default_factory. The stored default is now either a known type or "optional, type unknown," and as_param just turns that into the parameter requirement. A known type is only used for solving when the parameter has a type variable, so ordinary signatures still show |
|
This pull request has been imported. If you are a Meta employee, you can view this in D132424886. (Because this pull request was imported automatically, there will not be any future comments.) |
This comment has been minimized.
This comment has been minimized.
A literal default of an IntVar is not a solution of that dimension, so a no-arg constructor stays compatible with a symbolic annotation.
|
Diff from mypy_primer, showing the effect of this PR on open source code: ============================================================
SUMMARY
============================================================
Total: +4 new errors, -0 fixed errors
By preset: +4/-0 (default), +4/-0 (strict)
Projects with changes (1):
jax: +4 -0
============================================================
FULL DIFF DETAILS
------------------------------------------------------------
jax (https://github.com/google/jax)
+ ERROR jax/_src/pallas/mosaic/interpret/shared_memory.py:845:21-857:6: Argument `defaultdict[object, int]` is not assignable to parameter `next_buffer_id` with type `dict[tuple[int, int], int]` in function `GenericSharedMemory.__init__` [bad-argument-type]
+ ERROR jax/_src/pallas/mosaic/interpret/shared_memory.py:845:21-857:6: Argument `defaultdict[object, list[Unknown]]` is not assignable to parameter `output_ranges` with type `dict[tuple[int, int], list[Unknown]]` in function `GenericSharedMemory.__init__` [bad-argument-type]
+ ERROR jax/_src/pallas/mosaic_gpu/interpret/shared_memory.py:550:21-562:6: Argument `defaultdict[object, int]` is not assignable to parameter `next_buffer_id` with type `dict[Thread, int]` in function `jax._src.pallas.mosaic.interpret.shared_memory.GenericSharedMemory.__init__` [bad-argument-type]
+ ERROR jax/_src/pallas/mosaic_gpu/interpret/shared_memory.py:550:21-562:6: Argument `defaultdict[object, list[Unknown]]` is not assignable to parameter `output_ranges` with type `dict[Thread, list[Unknown]]` in function `jax._src.pallas.mosaic.interpret.shared_memory.GenericSharedMemory.__init__` [bad-argument-type] |
Summary
Generated()leftTunsolved.__init__now keeps that default and uses it the same way a handwritten constructor does.Generated()isGenerated[int]when the default is5. A bareGeneratedstaysGenerated[Any].Fixed Dataclass constructors defaults don't solve type parameters #5601
Test plan
cargo test -p pyrefly --lib test_dataclass_default_solves_type_var