Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pyright cannot use rx.event.EventSpec in a type definition #4354

Open
masenf opened this issue Nov 11, 2024 · 2 comments
Open

pyright cannot use rx.event.EventSpec in a type definition #4354

masenf opened this issue Nov 11, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@masenf
Copy link
Collaborator

masenf commented Nov 11, 2024

Describe the bug
error: Variable not allowed in type expression (reportInvalidTypeForm)

To Reproduce

import reflex as rx
from reflex.event import EventSpec


class State(rx.State):
    @rx.event
    def bad_toast(self) -> rx.event.EventSpec | None:
        return rx.toast.error("🥨!")

    @rx.event
    def good_toast(self) -> EventSpec | None:
        return rx.toast.success("🍞!")


def index() -> rx.Component:
    return rx.button("Toast 🥂", on_click=[State.good_toast, State.bad_toast])


app = rx.App()
app.add_page(index)
masenf@minicone repro-type-variable % pyright repro_type_variable/repro_type_variable.py
/Users/masenf/code/reflex-dev/repro-type-variable/repro_type_variable/repro_type_variable.py
  /Users/masenf/code/reflex-dev/repro-type-variable/repro_type_variable/repro_type_variable.py:7:28 - error: Variable not allowed in type expression (reportInvalidTypeForm)
1 error, 0 warnings, 0 informations

Expected behavior
Anything that needs to be used in a type expression must be defined statically at module level, NOT in a namespace like rx.event. We need to find another way to allow access to these types, like EventSpec, EventHandler, etc, without going through the EventNamespace

class EventNamespace(types.SimpleNamespace):
    """A namespace for event related classes."""

    Event = Event
    EventHandler = EventHandler
    EventSpec = EventSpec
    CallableEventSpec = CallableEventSpec
    EventChain = EventChain
    EventVar = EventVar
    LiteralEventVar = LiteralEventVar
    EventChainVar = EventChainVar
    LiteralEventChainVar = LiteralEventChainVar
    EventType = EventType
    EventCallback = EventCallback

☝️ this doesn't work with pyright.

There might be a way to hack this with a .pyi file, haven't tried. Although maybe it's better to just doc and move on?

Specifics (please complete the following information):

  • Python Version: 3.11
  • Reflex Version: 0.6.5a
  • OS: macos
@masenf masenf added the bug Something isn't working label Nov 11, 2024
Copy link

linear bot commented Nov 11, 2024

@benedikt-bartscher
Copy link
Contributor

We've had this issue in some of our repos and just imported EventSpec like this to mitigate it:

from reflex.event import EventSpec

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants