Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

bug: `use_state` with a callable initializer is inferred ambiguously by ty and mypy

オープン 初心者向け
#6,960 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
78/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python
領域
testing-qa

調査の方向性

use_stateの定義と型チェックテストから始めてください。issueにはソースファイルもテストファイルも記載されていないため、まず実装と既存の型チェック範囲を見つけてください。現在のunionアノテーションと提案されたoverloadを比較し、次に、呼び出し可能な初期化子が戻り値の型を推論し、値による初期化子がty、mypy、pyrightで引き続き値の型を推論することを確認してください。

索引モデルが issue の本文から書いたものです。

説明

bug
Duplicate Check
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
  1. Save the code sample as app.py in an environment with flet==1.0.4.
  2. Run ty check app.py: the revealed type is (() -> FormState) | FormState, and state.clear() is reported as unresolved-attribute.
  3. Run mypy app.py: Argument 1 to "use_state" has incompatible type "Callable[[], FormState]"; expected "Callable[[], Never]", and the revealed type is Any.
  4. Run pyright app.py: the revealed type is FormState, 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

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

flet-dev/flet のほかの issue

flet-dev/flet の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。