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

[REF-2302] When a Var points to a model, prefer access to model fields. #2893

Merged
merged 4 commits into from
Mar 27, 2024

Conversation

masenf
Copy link
Collaborator

@masenf masenf commented Mar 20, 2024

When a Var points to a model, and fields of the model share the same name as Var operations, access to the model fields is now preferred to avoid having Var operation names shadow model fields. Since most Var operations do not actually work against Models, this does not really block any functionality.

import reflex as rx


class BadModel(rx.Base):
    split: str = "This string just says split"
    reverse: str = "This string just says reverse"
    nice_name: str = "nice.job"


class State(rx.State):
    """The app state."""
    m: BadModel = BadModel()

    @rx.var
    def n(self) -> BadModel:
        return BadModel()


def index() -> rx.Component:
    return rx.fragment(
        rx.color_mode.button(rx.color_mode.icon(), float="right"),
        rx.vstack(
            rx.heading("Welcome to Reflex!", font_size="2em"),
            rx.text(State.m.split),
            rx.text(State.m.reverse),
            rx.text(State.m.nice_name.split(".")[0]),
            rx.text(State.n.split),
            rx.text(State.n.reverse),
            rx.text(State.n.nice_name.split(".")[0]),
            gap="1.5em",
            font_size="2em",
            padding_top="10%",
        ),
    )


# Create app instance and add index page.
app = rx.App()
app.add_page(index)

When a Var points to a model, and fields of the model share the same name as
Var operations, access to the model fields is now preferred to avoid having Var
operation names shadow model fields. Since most Var operations do not actually
work against Models, this does not really block any functionality.
Since fget is a "slot" on property, normal __getattribute__ access cannot find it.
Copy link

linear bot commented Mar 20, 2024

In python 3.9 and 3.10, the `isinstance(list[...], type)` returns True, but
it's not a valid class for use in issubclass
@masenf masenf merged commit b23859a into main Mar 27, 2024
66 checks passed
@masenf masenf deleted the masenf/var-field-access-escape-hatch branch March 27, 2024 23:23
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.

None yet

2 participants