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

Aperta
#6,574 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
58/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Tranquilla
Stack tecnologico
python
Ambito
frontend, web-dev

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Python
Stelle
29k
Fork
1.8k
Merge medio
3g 5h
PR unite (30g)
182

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di reflex-dev/reflex

Tutte le issue di reflex-dev/reflex

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.