Comparing an item-access var (e.g. state.dict["k"] >= 90) raises ObjectItemOperation error in 0.9.x

オープン
#6,574 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

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

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

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
58/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
静か
技術スタック
python
領域
frontend, web-dev

調査の方向性

Start with the minimal reproduction using State.metrics["current"] �>= 90 inside rx.cond, then trace how item access is evaluated when a var is used as a boolean during reflex run. Done means the comparison resolves to False for the shown values and renders "Below threshold" without raising ObjectItemOperation.

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

説明

Describe the bug

In Reflex 0.9.x, comparing a var that originated from a dictionary or list item-access against a Python literal raises an ObjectItemOperation error at render time. The same expression worked in 0.8.x.

The bug surfaces specifically inside rx.cond and other places where a var needs to be evaluated as a boolean.

To Reproduce
import reflex as rx

class State(rx.State):
    metrics: dict[str, int] = {"current": 75, "limit": 100}

def index() -> rx.Component:
    return rx.cond(
        State.metrics["current"] >= 90,
        rx.text("Above threshold"),
        rx.text("Below threshold"),
    )

app = rx.App()
app.add_page(index)

Running reflex run raises:

TypeError: '>=' not supported between instances of 'ObjectItemOperation' and 'int'
Expected behavior

The comparison should evaluate against the resolved value, returning False in this case (75 < 90), and render the second branch.

Workaround

Compute the comparison in the state and expose a derived var:

class State(rx.State):
    metrics: dict[str, int] = {"current": 75, "limit": 100}

    @rx.var
    def is_above_threshold(self) -> bool:
        return self.metrics["current"] >= 90

This works but is awkward for ad-hoc per-render checks.

Specifications
  • Reflex version: 0.9.3
  • Python version: 3.12
  • OS: macOS
Additional context

Possibly related to closed issues #4380 (ObjectVar __getitem__ typing issues), #5849 (make indexing more null friendly), and #4647 (recursive datatypes checking) but the comparison failure mode persists in 0.9.3.

主要言語
Python
スター
29k
フォーク
1.8k
平均マージ
3日 5時間
マージ済み PR(30日)
182

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

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

reflex-dev/reflex のほかの issue

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

似ている issue

Python の issue をもっと見る

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

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