bug: `use_state` with a callable initializer is inferred ambiguously by ty and mypy
メンテナーはふだん 2 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 78/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- python
- 領域
- testing-qa
調査の方向性
use_stateの定義と型チェックテストから始めてください。issueにはソースファイルもテストファイルも記載されていないため、まず実装と既存の型チェック範囲を見つけてください。現在のunionアノテーションと提案されたoverloadを比較し、次に、呼び出し可能な初期化子が戻り値の型を推論し、値による初期化子がty、mypy、pyrightで引き続き値の型を推論することを確認してください。
索引モデルが issue の本文から書いたものです。
説明
Duplicate Check
- I have searched the opened issues and there are no duplicates
Describe the bug
ft.use_state accepts an initial value or a zero-argument function:
def use_state(
initial: StateT | Callable[[], StateT],
) -> tuple[StateT, Callable[[StateT | Updater], None]]:
When a function is passed, StateT appears on both sides of the union, and type checkers disagree on how to solve it. The typing spec does not define this case, so each checker picks its own strategy:
| Checker | Inferred type of state |
|---|---|
| pyright 1.1.414 | FormState (correct) |
| ty 0.0.86 | (() -> FormState) | FormState, then unresolved-attribute on every attribute access |
| mypy 2.4.0 | fails to solve: expected "Callable[[], Never]", and the state becomes Any |
The code is correct at runtime. Only the annotation is ambiguous, so the lazy-initializer form documented for use_state cannot be used without type errors outside pyright. Annotating the target variable does not help: ty reports invalid-assignment instead.
Code sample
from dataclasses import dataclass
from typing import reveal_type
import flet as ft
@ft.observable
@dataclass
class FormState:
name: str = ""
def clear(self) -> None:
self.name = ""
def create_state() -> FormState:
return FormState()
@ft.component
def Form() -> ft.Control:
state, _ = ft.use_state(create_state)
reveal_type(state)
return ft.TextButton("Clear", on_click=lambda _: state.clear())
def main(page: ft.Page) -> None:
page.render(Form)
ft.run(main)
To reproduce
- Save the code sample as
app.pyin an environment withflet==1.0.4. - Run
ty check app.py: the revealed type is(() -> FormState) | FormState, andstate.clear()is reported asunresolved-attribute. - Run
mypy app.py:Argument 1 to "use_state" has incompatible type "Callable[[], FormState]"; expected "Callable[[], Never]", and the revealed type isAny. - Run
pyright app.py: the revealed type isFormState, with no errors.
Expected behavior
state is inferred as FormState by every type checker, as it already is when an initial value (not a function) is passed.
Screenshots / Videos
Captures
[Upload media here]
Operating System
Windows
Operating system details
Windows 11 Pro 10.0.26200, Python 3.14.3
Flet version
1.0.4
Regression
I'm not sure / I don't know
Suggestions
Replace the union with two overloads, so checkers try the callable form first:
@overload
def use_state(initial: Callable[[], StateT]) -> tuple[StateT, Callable[[StateT | Updater], None]]: ...
@overload
def use_state(initial: StateT) -> tuple[StateT, Callable[[StateT | Updater], None]]: ...
I tested this shape on a minimal standalone copy of the signature (not on Flet itself): pyright, ty and mypy all infer the function's return type for a callable and int for use_state(0), with no errors.
Logs
No response
Additional details
Workaround: pass a ready value instead of a function, e.g. ft.use_state(create_state()). It type-checks everywhere, at the cost of building (and discarding) a new object on every render.
I searched the issues for use_state and found no report about the type of the initial value. #5741 added the StateT/Updater hints, but for the setter.
- 主要言語
- Python
- スター
- 17.4k
- フォーク
- 728
- 平均マージ
- 1日 9時間
- マージ済み PR(30日)
- 56
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
flet-dev/flet のほかの issue
-
feature request
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
メンテナーはふだん 2 日以内に返信
-
flet build: common [tool.flet.deep_linking] is ignored on Android and iOS対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープンbug packaging platform: mobile
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 2 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 2 日以内に返信
-
bug
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
メンテナーはふだん 2 日以内に返信
-
bug controls
難易度 4/5 3〜5日 初心者へのやさしさ 22/100
メンテナーはふだん 2 日以内に返信
似ている issue
-
難易度 1/5 1〜3時間 初心者へのやさしさ 88/100
fossasia/visdom-dev#69 ·
メンテナーはふだん 3 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
BUG: PROD: v1.3.0-beta Onboarding Profile Picture Misaligns When Name Validation Error Appears対応中かも @RitikaSen27 が今日担当しました。 オープンbug good first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
AOSSIE-Org/PictoPy#1647 · コメント 2 件 ·
メンテナーはふだん 2 日以内に返信
-
[oblt-aw][autodoc] Update workflow docs for gh-aw v0.92.5 and detector permission scope対応中かも @github-actions が今日担当しました。 オープン
難易度 1/5 1〜3時間 初心者へのやさしさ 88/100
メンテナーはふだん 1 日以内に返信