Skip to content

Solve dataclass type variables from field defaults - #5672

Open
sathariels wants to merge 2 commits into
pyrefly-org:mainfrom
sathariels:dataclass-default-solves-tparam
Open

sathariels wants to merge 2 commits into
pyrefly-org:mainfrom
sathariels:dataclass-default-solves-tparam

Conversation

@sathariels

@sathariels sathariels commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • A generated dataclass constructor remembered that a field had a default, but not the value, so Generated() left T unsolved.
  • The synthesized __init__ now keeps that default and uses it the same way a handwritten constructor does. Generated() is Generated[int] when the default is 5. A bare Generated stays Generated[Any].
    Fixed Dataclass constructors defaults don't solve type parameters #5601

Test plan

  • cargo test -p pyrefly --lib test_dataclass_default_solves_type_var
  • dataclass, dataclass-transform, attrs, and pydantic lib tests
  • I am a human, and I fully understand the code I am submitting. See our AI policy.

@rchen152 rchen152 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. 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 now field(default=...) and default_factory skip those steps.
  2. Store the result in a type that tells "has a default of unknown type" apart from "default with a known type", so Any no longer stands for both.
  3. Have as_param simply convert the stored value to Required.

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.
@sathariels
sathariels force-pushed the dataclass-default-solves-tparam branch from aa8d9a9 to affd6a3 Compare November 19, 2026 19:35
@github-actions github-actions Bot added size/l and removed size/m labels Nov 19, 2026
@sathariels

sathariels commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor Author

@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 ....

@meta-codesync

meta-codesync Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

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.)

@github-actions

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.
@github-actions github-actions Bot added size/l and removed size/l labels Nov 19, 2026
@github-actions

Copy link
Copy Markdown

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]

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dataclass constructors defaults don't solve type parameters

2 participants