Skip to content

Conversation

@adhami3310
Copy link
Member

this prevents a weird error if you are yielding/returning a str

@codspeed-hq
Copy link

codspeed-hq bot commented Dec 10, 2025

CodSpeed Performance Report

Merging #6034 will not alter performance

Comparing do-not-treat-str-bytes-as-list (72ea2ae) with main (bf49c88)

Summary

✅ 8 untouched

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Dec 10, 2025

Greptile Overview

Greptile Summary

Fixed a bug in _check_valid where returning/yielding a str or bytes from an event handler would cause confusing iteration over individual characters.

In Python, str and bytes are instances of Sequence, so the original code would treat "hello" as ['h', 'e', 'l', 'l', 'o'] rather than wrapping it in a list. This resulted in unclear error messages when users accidentally returned strings from event handlers.

  • Modified the sequence check in reflex/state.py:1746 to explicitly exclude str and bytes types
  • Now strings/bytes are wrapped in a list before validation, producing clearer error messages

Confidence Score: 5/5

  • This PR is safe to merge - it's a minimal, correct bug fix with no risk of regressions.
  • Single-line change that correctly fixes a Python type-checking edge case. The logic is sound and follows established patterns for handling strings as non-sequences.
  • No files require special attention.

Important Files Changed

File Analysis

Filename Score Overview
reflex/state.py 5/5 Fixed _check_valid to exclude str and bytes from Sequence check, preventing confusing errors when event handlers return strings.

Sequence Diagram

sequenceDiagram
    participant EH as Event Handler
    participant CV as _check_valid()
    participant FE as fix_events()
    
    EH->>CV: yields/returns value
    
    alt value is str/bytes (OLD behavior)
        CV->>CV: isinstance("hello", Sequence) = True
        CV->>CV: Iterates over characters ['h','e','l','l','o']
        CV-->>EH: Confusing TypeError
    end
    
    alt value is str/bytes (NEW behavior)
        CV->>CV: Excludes str/bytes from Sequence check
        CV->>CV: Wraps in list: ["hello"]
        CV->>CV: Validates single element
        CV-->>EH: Clear TypeError for invalid return type
    end
    
    alt value is valid Sequence (list/tuple)
        CV->>CV: Validates each element
        CV->>FE: Pass validated events
    end
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 file reviewed, no comments

Edit Code Review Agent Settings | Greptile

@adhami3310 adhami3310 merged commit 1f255e8 into main Dec 11, 2025
47 checks passed
@adhami3310 adhami3310 deleted the do-not-treat-str-bytes-as-list branch December 11, 2025 22:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants