Skip to content

Fix type annotations and improve error reporting - #457

Merged
masenf merged 1 commit into
mainfrom
claude/eventhandler-typing-diagnostics-8vb8cg
Aug 4, 2026
Merged

Fix type annotations and improve error reporting#457
masenf merged 1 commit into
mainfrom
claude/eventhandler-typing-diagnostics-8vb8cg

Conversation

@masenf

@masenf masenf commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR addresses three separate issues: correcting EventHandler type annotations to use Annotated for type-checker compatibility, removing unnecessary cast() calls that mask type information, and improving error messages when user input contains non-comparable keys.

Key Changes

  • EventHandler type annotations (python/reflex_xy/component.py):

    • Changed from rx.EventHandler[<args spec>] (runtime-only DSL) to Annotated[rx.EventHandler, <args spec>] (valid type expression)
    • Added explanatory comment: EventHandler is not generic, so subscripting it is invalid in type expressions and rejected by type checkers
    • Reflex reads the spec back out of __metadata__, so both forms produce the same runtime object
    • Affects: on_point_hover, on_point_click, on_select_end, on_view_change, on_animation_start, on_animation_end, on_hover
  • Removed unnecessary casts (python/xy/pyplot/_axes.py):

    • Removed cast(np.ma.MaskedArray, ...) calls in _plot_series for masked array handling
    • np.ma.asarray() already returns the correct type; explicit casts were redundant and obscured intent
    • Removed unused cast import
  • Improved error reporting (python/xy/_validate.py):

    • Fixed mark_fill validation to handle user-supplied dicts with non-comparable keys
    • Changed from sorted(set(value) - {...}) to sorted(str(key) for key in set(value) - {...})
    • Prevents bare TypeError when dict contains mixed types (e.g., {1: "x", "mode": "y"})
    • Now correctly reports the unknown key via ValueError as per spec/api/styling.md
    • Added test case in tests/test_figure.py to verify the fix

Implementation Details

The EventHandler change is purely about type-checker compliance. The runtime behavior is identical because EventHandler.__class_getitem__ returns the Annotated form, and Reflex extracts the spec from __metadata__. The comment documents why the shorthand cannot be used in type expressions.

The cast removal simplifies code by trusting NumPy's type system. The error reporting fix ensures user-facing validation errors are consistent and informative, even when input violates assumptions about key comparability.

https://claude.ai/code/session_01VhynvUCNjLBHYVfmHCR2tX

Review in cubic

Summary by CodeRabbit

  • Bug Fixes

    • Improved validation for chart fill settings with mixed-type keys, providing a clear error for unknown options instead of an internal sorting error.
    • Improved compatibility and consistency for chart event-handler configuration.
  • Tests

    • Added regression coverage for invalid fill dictionaries with incomparable key types.

The dependency bumps in #428 landed from a branch that was behind main, so
these ten diagnostics never appeared on that PR and now trip every branch
built on top of it.

reflex_xy/component.py: spell the event-trigger annotations as
`Annotated[rx.EventHandler, <args spec>]` instead of the shorthand
`rx.EventHandler[<args spec>]`. The two are the same object — reflex's
`EventHandler.__class_getitem__` returns exactly this Annotated form and its
trigger discovery reads the spec back out of `__metadata__` — but only the
shorthand is a runtime-only DSL: `EventHandler` is not a generic class, so
subscripting it is invalid in a type expression. All seven triggers still
resolve with the same arg-spec parameter names.

xy/_validate.py: sort the rendered form of unrecognized `mark_fill` keys.
`value` is user input whose keys need not be mutually comparable, so
`mark_fill({1: ..., "mode": ...})` raised a bare `TypeError` out of `sorted`
instead of naming the unknown key. Closed grammars are specified to raise
`ValueError` (spec/api/styling.md), so this makes the implementation match.

xy/pyplot/_axes.py: drop the now-redundant `cast` around `np.ma.asarray`,
which the newer numpy stubs already type as `MaskedArray`.

Verified against the interpreter CI resolves (3.12 / numpy 2.5.1):
`ty check python tests/typing_pep561_consumer.py` is clean.
@coderabbitai

coderabbitai Bot commented Aug 4, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: bd5ce7c5-9e9e-4c90-8201-38ce3cc203cf

📥 Commits

Reviewing files that changed from the base of the PR and between d21b7bb and 648191b.

📒 Files selected for processing (4)
  • python/reflex_xy/component.py
  • python/xy/_validate.py
  • python/xy/pyplot/_axes.py
  • tests/test_figure.py

📝 Walkthrough

Walkthrough

Changes

XY corrections

Layer / File(s) Summary
XYChart event annotations
python/reflex_xy/component.py
XYChart event handlers now use Annotated[rx.EventHandler, ...] while preserving existing payload specifications.
Fill-key validation
python/xy/_validate.py, tests/test_figure.py
mark_fill handles incomparable unknown keys and raises the expected ValueError. Regression coverage verifies the error message.
Masked-array conversion cleanup
python/xy/pyplot/_axes.py
_plot_series removes redundant cast usage and retains masked-array conversion behavior.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: alek99

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the two primary changes: fixing type annotations and improving error reporting.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/eventhandler-typing-diagnostics-8vb8cg

Comment @coderabbitai help to get the list of available commands.

@greptile-apps

greptile-apps Bot commented Aug 4, 2026

Copy link
Copy Markdown

Greptile Summary

This PR corrects Reflex event-handler annotations, simplifies masked-array handling, and ensures mixed-type unknown fill keys produce a useful validation error.

  • Replaces runtime-only EventHandler subscripting with Annotated metadata.
  • Removes redundant masked-array casts without changing runtime operations.
  • Sorts rendered unknown keys and adds regression coverage for non-comparable dictionary keys.

Confidence Score: 5/5

The PR appears safe to merge, with no concrete changed-code defect identified.

The validation change preserves strict rejection while avoiding heterogeneous-key sorting failures, the removed casts have no runtime effect, and the event annotations retain the metadata shape Reflex expects.

Important Files Changed

Filename Overview
python/reflex_xy/component.py Rewrites seven event annotations into type-checker-compatible Annotated forms while retaining their Reflex argument metadata.
python/xy/_validate.py Converts unknown fill keys to strings before sorting so heterogeneous keys are rejected through the documented validation path.
python/xy/pyplot/_axes.py Removes runtime-no-op typing casts around masked-array conversion with no observable behavioral change.
tests/test_figure.py Adds regression coverage confirming mixed-type unknown fill keys raise ValueError without partially mutating the figure.

Reviews (1): Last reviewed commit: "Fix ty diagnostics surfaced by the depen..." | Re-trigger Greptile

@codspeed-hq

codspeed-hq Bot commented Aug 4, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 109 untouched benchmarks
⏩ 2 skipped benchmarks1


Comparing claude/eventhandler-typing-diagnostics-8vb8cg (648191b) with main (d21b7bb)

Open in CodSpeed

Footnotes

  1. 2 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No issues found across 4 files

Tip: cubic could auto-approve low-risk PRs like this, if it thinks it's safe to merge. Learn more

Re-trigger cubic

@masenf
masenf merged commit 37c3d91 into main Aug 4, 2026
50 checks passed
@masenf
masenf deleted the claude/eventhandler-typing-diagnostics-8vb8cg branch August 4, 2026 23:46
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.

1 participant