-
Notifications
You must be signed in to change notification settings - Fork 598
Support overridden dataclass defaults in control encoding #5861
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
Conversation
Added logic to retrieve default values from base dataclasses when encoding controls, ensuring overridden defaults in subclasses are correctly emitted. Includes new tests and example usage with custom button controls.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We've reviewed this pull request using the Sourcery rules engine
Deploying flet-docs with
|
| Latest commit: |
464cafc
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://30a8a486.flet-docs.pages.dev |
| Branch Preview URL: | https://override-control-props-fix.flet-docs.pages.dev |
ndonkoHenri
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice feature!
Can you please update docs too: https://docs.flet.dev/cookbook/custom-controls/ ?
| def configure_encode_object_for_msgpack(control_cls): | ||
| def encode_object_for_msgpack(obj): | ||
| if is_dataclass(obj): | ||
| r = {} | ||
| prev_lists = {} | ||
| prev_dicts = {} | ||
| prev_classes = {} | ||
| for field in fields(obj): | ||
| if "skip" in field.metadata: # or hasattr(obj, f"_prev_{field.name}"): | ||
| continue | ||
| v = getattr(obj, field.name) | ||
| if isinstance(v, list): | ||
| v = v[:] | ||
| if len(v) > 0: | ||
| r[field.name] = v | ||
| prev_lists[field.name] = v | ||
| elif isinstance(v, dict): | ||
| v = v.copy() | ||
| if len(v) > 0: | ||
| r[field.name] = v | ||
| prev_dicts[field.name] = v | ||
| elif field.name.startswith("on_") and field.metadata.get("event", True): | ||
| v = v is not None | ||
| if v: | ||
| r[field.name] = v | ||
| elif is_dataclass(v): | ||
| r[field.name] = v | ||
| prev_classes[field.name] = v | ||
| elif v is not None and ( | ||
| v != field.default or not isinstance(obj, control_cls) | ||
| ): | ||
| r[field.name] = v | ||
| else: | ||
| default_value = field.default | ||
| if isinstance(obj, control_cls): | ||
| root_field = _get_root_dataclass_field( | ||
| obj.__class__, field.name | ||
| ) | ||
| if root_field is not None: | ||
| default_value = root_field.default | ||
| if v is not None and ( | ||
| v != default_value or not isinstance(obj, control_cls) | ||
| ): | ||
| r[field.name] = v |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit;
Me going through this function: 😵💫
Little comments here and there will be awesome.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😈
This is how we can define custom control and their default property values:
Rules:
@dataclassor@ft.controldecorator on the inherited class - both methods works the same.expand: int = 1will override inherited property, butexpand = 1won't. Not sure which type to use? JustAnywill work, for exampleexpand: Any = 1.field(default_factory=lambda: <new_value>)for immutable types such as class, list, dict.init()method, but you won't be able to override if writeMyButton3(expand=False).Summary by Sourcery
Support correct encoding of dataclass-based controls with overridden default values and document custom control default configuration through examples.
New Features:
Bug Fixes:
Tests: