Repository navigation
Declare dataclasses.field as a field specifier for @control and @value - #7084
Merged
FeodorFitsner merged 3 commits intoNov 14, 2026
Merged
FeodorFitsner merged 3 commits into
FeodorFitsner merged 3 commits into
Conversation
Fixes flet-dev#6377. Without field_specifiers, type checkers ignore dataclasses.field() arguments such as init=False on classes built with @control and @value.
This was referenced Nov 13, 2026
ndonkoHenri
approved these changes
Nov 13, 2026
ndonkoHenri
left a comment
Contributor
There was a problem hiding this comment.
Thanks for your contribution! Looks good to me.
Please add a changelog entry as you suggested.
FeodorFitsner
approved these changes
Nov 14, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Fixes #6377.
@controland@valueuse@dataclass_transform()withoutfield_specifiers. Per the typing spec, type checkers then don't treatdataclasses.field()as a field specifier, so its arguments are ignored: a field declared withfield(init=False)is still accepted as an__init__argument. At runtime both decorators wrapdataclasses.dataclass, sofield()already works there; this only aligns the static view with the runtime.This PR adds
field_specifiers=(dataclasses.field,)to both decorators.skip_field()is intentionally not listed. It callsfield(default=None, ...)internally, but checkers only see a default when the call passesdefault=explicitly, so listing it would turn fields declared withskip_field()into required parameters.Test code
mypy before: no error on the last two lines.
mypy after:
Also added
test_dataclass_transforms_declare_field_specifierstotest_base_control.py; it fails without the fix and passes with it.Type of change
Checklist
website/sidebars.ymlfor breaking changes, removals, and deprecations, if applicable.Additional details
uv run --group test pytest packages/flet/tests: 507 passed, 8 skipped.ruff checkandruff format --checkare clean on the changed files.I didn't add a changelog entry because there's no section for the next release yet. Happy to add it where you prefer, e.g. under Bug fixes:
Side note, unrelated to this PR: with
@control("MyControl")mypy also reportsArgument 1 has incompatible type "type[MyControl]"; expected "type[Never]"on the class line. I can open a separate issue for it if useful.Summary by Sourcery
Align the static dataclass behavior of
@controland@valuewith their runtime behavior by declaringdataclasses.fieldas a supported field specifier.Bug Fixes:
dataclasses.field()options such asinit=Falsefor classes decorated with@controland@value.],Tests:
dataclasses.fieldas a dataclass field specifier.