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

Type Mismatch Warning When Setting CustomVar in State Using Built-in Setter #4565

Closed
ruhz3 opened this issue Dec 20, 2024 · 1 comment · Fixed by #4726
Closed

Type Mismatch Warning When Setting CustomVar in State Using Built-in Setter #4565

ruhz3 opened this issue Dec 20, 2024 · 1 comment · Fixed by #4726
Assignees

Comments

@ruhz3
Copy link
Contributor

ruhz3 commented Dec 20, 2024

Description

When attempting to set a CustomVar that inherits from rx.Base in State using the built-in setter, a mismatch warning occurs.
A simple example is provided below.

Warning occured but it is initialized properly without issues using a dictionary.
Only when the first time I clicked the button, warning occured.

If this is considered a problematic situation, I am willing to work on a solution.

To Reproduce
Code

from typing import List
import reflex as rx


class Fruit(rx.Base):
    name: str
    brix: int
    edible: bool


class FruitState(rx.State):
    fruits: List[Fruit] = [
        Fruit(name="banana", brix=1, edible=True),
        Fruit(name="apple", brix=2, edible=True)
    ]
    favorite_fruit: Fruit = None


def fruit_button(fruit: Fruit) -> rx.Component:
    return rx.button(
        fruit.name,
        on_click=FruitState.set_favorite_fruit(fruit)
    )


def index() -> rx.Component:
    return rx.box(
        rx.heading(f"Favorite Fruit : {FruitState.favorite_fruit.name}"),
        rx.foreach(
            FruitState.fruits,
            fruit_button
        ),
    )


app = rx.App()
app.add_page(index)

Log

DeprecationWarning: mismatched-type-assignment has been deprecated in version 0.6.5 Tried to assign value {'name': 'apple', 'brix': 2, 'edible': True} of type <class 'dict'> to field FruitState.favorite_fruit of type 
typing.Optional. This might lead to unexpected behavior. It will be completely removed in 0.7.0

Copy link

linear bot commented Dec 20, 2024

@adhami3310 adhami3310 self-assigned this Feb 1, 2025
@adhami3310 adhami3310 linked a pull request Feb 1, 2025 that will close this issue
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 a pull request may close this issue.

2 participants