Skip to content

fix: use_memo/use_effect see in-place changes to observable lists; Icons[name]; use_dialog rejects non-dialogs - #7114

Open
FeodorFitsner wants to merge 2 commits into
mainfrom
fix/declarative-api-edges
Open

FeodorFitsner wants to merge 2 commits into
mainfrom
fix/declarative-api-edges

Conversation

@FeodorFitsner

@FeodorFitsner FeodorFitsner commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Description

Three edges of the declarative API that came up while building a reference app for the Flet Studio agent. Each one cost a debugging loop because it fails silently.

use_memo / use_effect and in-place changes

use_memo(fn, [state.items]) never recomputed after state.items.append(x), and use_effect with the same dependency never re-ran. Dependencies are compared with is / ==, and an observable list is the same object after an in-place change, so it always compared equal to itself.

Observables now keep a change counter per field (_notify(field) bumps it; notify() bumps a shared one). use_memo and use_effect record an observable list or dict dependency together with its field's counter (capture_dependencies()), so:

  • the same collection counts as changed when its field's counter moved, including changes to a collection nested in it;
  • different collections still compare by items, as before (state.items = [1, 2] over an equal list doesn't recompute);
  • other fields changing doesn't recompute.

Whole Observable objects passed as dependencies keep comparing by identity. Making them version-aware would make use_effect(state.load, [state]) loop forever.

@ft.memo components already subscribe to observable props, so they needed no change.

Icons["NAME"]

Material, Cupertino and Lucide icons are proxies with __getattr__, so ft.Icons["HOME"], the enum-style lookup, raised TypeError. The proxies now have __getitem__ (a KeyError for an unknown name) and __contains__. "HOME" in ft.Icons checks a name, and ft.Icons.HOME in ft.Icons still works. The stubs and their templates declare both on a metaclass, so mypy and pyright type ft.Icons["HOME"] as IconData.

use_dialog() and non-dialogs

A dialog returned from an @ft.component function is a Component, not a DialogControl, so use_dialog() added it to the overlay and it never opened, with no error. It now raises a TypeError that names the component and says to build the dialog inline or in a plain function. Any other non-dialog control gets a TypeError too. The declarative dialogs cookbook page gains a short "Building a dialog in a helper function" section.

Also

The typos pre-commit hook now skips the generated material and Cupertino icon stubs, which carry Flutter's spelling (FLOURESCENT, THREED_ROTATION), as it already did for the Lucide stub. Editing the material stub tripped it.

Test code

import flet as ft


@ft.observable
class Store:
    def __init__(self):
        self.items = [5, 10]


Ctx = ft.create_context(None)


@ft.component
def WrappedDialog():
    return ft.AlertDialog(title=ft.Text("Wrapped"))


@ft.component
def Panel():
    store = ft.use_context(Ctx)
    total = ft.use_memo(lambda: sum(store.items), [store.items])
    runs, set_runs = ft.use_state(0)
    ft.use_effect(lambda: set_runs(lambda n: n + 1), [store.items])
    wrapped, set_wrapped = ft.use_state(False)
    error = None
    try:
        ft.use_dialog(WrappedDialog() if wrapped else None)
    except TypeError as e:
        error = str(e)
    return ft.Column([
        ft.Icon(ft.Icons["ADD_SHOPPING_CART"]),
        ft.Text(f"total={total} effect_runs={runs}"),
        ft.Button("Append 7", on_click=lambda: store.items.append(7)),
        ft.Button("Wrapped dialog", on_click=lambda: set_wrapped(True)),
        ft.Text(error or "no error"),
    ])


@ft.component
def App():
    store, _ = ft.use_state(Store)
    return Ctx(store, Panel)


ft.run(lambda page: page.render(App))

Before: clicking "Append 7" twice leaves total=15 effect_runs=1; ft.Icons["ADD_SHOPPING_CART"] raises; "Wrapped dialog" does nothing. After: total=29 effect_runs=3, the icon renders, and the error explains the dialog. I checked this in web server mode in headless Chrome.

Tests

  • tests/test_hook_dependencies.py (new): memo and effect after append, item replace/pop, nested list, dict, notify(); no recompute for other fields or a new equal list.
  • tests/test_icons.py: lookup and membership by name and by value.
  • tests/test_use_dialog.py: TypeError for a component and for a non-dialog control.
  • All 10 new tests fail on main. Full suite: 570 passed, 9 skipped.

Summary by Sourcery

Make declarative state dependencies observe in-place collection changes, expand icon lookup, and fail clearly for unsupported dialog values.

New Features:

  • Add enum-style name lookup and membership checks for Material, Cupertino, and Lucide icons.
  • Reject invalid controls passed to use_dialog() with actionable TypeError messages.

Bug Fixes:

  • Detect in-place changes to observable list and dict dependencies in use_memo() and use_effect(), including nested collection changes and manual notifications.

Enhancements:

  • Document building dialogs in plain helper functions for use with the declarative API.

CI:

  • Exclude generated Material and Cupertino icon stubs from typo checks.

Documentation:

  • Add declarative dialog guidance for constructing dialogs in non-component helper functions.

Tests:

  • Add coverage for observable collection hook dependencies, icon lookup and membership, and use_dialog() validation.

… dicts; Icons[name]; use_dialog rejects non-dialogs

- use_memo() and use_effect() record observable list/dict dependencies with
  a per-field change counter, so state.items.append(x) recomputes the memo
  and re-runs the effect. Different collections still compare by items.
- Icons, CupertinoIcons and LucideIcons support ["NAME"] lookup and
  "NAME" in Icons; stubs (and their templates) type it as IconData.
- use_dialog() raises a TypeError for a component call or a non-dialog
  control instead of silently never opening it.
- Cookbook: building a dialog in a helper function.
- typos: skip the generated material and Cupertino icon stubs, which carry
  Flutter's spelling, like the Lucide stub already is.

@sourcery-ai sourcery-ai Bot 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.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

@FeodorFitsner
FeodorFitsner requested a balanced review from Copilot November 21, 2026 00:43

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Deploying flet-website-v2 with  Cloudflare Pages  Cloudflare Pages

Latest commit: a859b07
Status: ✅  Deploy successful!
Preview URL: https://b470b6ba.flet-website-v2.pages.dev
Branch Preview URL: https://fix-declarative-api-edges.flet-website-v2.pages.dev

View logs

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants