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

Đang mở
#6,574 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
58/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Ít trao đổi
Công nghệ
python
Lĩnh vực
frontend, web-dev

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Python
Star
29k
Fork
1.8k
Merge trung bình
3 ngày 5 giờ
Pull request đã merge (30 ngày)
182

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của reflex-dev/reflex

Tất cả issue của reflex-dev/reflex

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.