reflex icon indicating copy to clipboard operation
reflex copied to clipboard

rx.input fails if default_value is typed Optional

Open gab23r opened this issue 2 years ago β€’ 3 comments

Describe the bug The following snippet fails

To Reproduce Steps to reproduce the behavior:

import reflex as rx

class State(rx.State):
    """The app state."""

    @rx.var
    def default_value(slef) -> str | None:
        return ''

def index() -> rx.Component:
    return rx.input(default_value = State.default_value)

# TypeError: Invalid var passed for prop default_value, expected type <class 'str'>, got value ${state.default_value} of type str | None.

Expected behavior No error as None is already the default value if nothing is specified

Specifics (please complete the following information):

  • Python Version: 3.10
  • Reflex Version:0.3.2
  • OS: linux

gab23r avatar Nov 08 '23 16:11 gab23r

Hey @gab23r I believe the issue is really just about the possibility of None being the response on a default value function. Nothing to do with optionality. I tested a default value function that returns only None and got a similar error.

TypeError: Invalid var passed for prop default_value, expected type <class 'str'>, got value default_value of type <class 'NoneType'>.

Your statement about expected behavior is not true. The default_value parameter on the input component takes a function as an input which by default is None but that does not necessarily mean that you can replace it with a function that returns None. Totally different constructs.

So, the first core question-- is the default default value of an input an empty string? If so, then reflex has no infrastructure for having None as the default and this is a deeper issue. Or if the default default value of an input is None, then this is more of a parameter passing issue. I can't figure this out at the moment.

This also begs the question of how someone would set the default value of a numeric input. Because I am pretty sure everything is being enforced to a string. EDIT -- Nevermind there is a dedicated number input component.

dapomeranz avatar Mar 26 '24 04:03 dapomeranz

I did some more digging and I do think there is a broad issue that components cannot have values passed in as None. Not just defaults. This is most of the time logical, but I can imagine that in the future someone might want to override a default value of a component with None.

However, this is some foundational code that requires changing and as such I did not submit a PR.

dapomeranz avatar Mar 26 '24 19:03 dapomeranz

@dapomeranz Good catch - I think we should allow passing in None as an argument (and treat that the same as if they have not passed an argument at all). Created this ticket to track that: https://github.com/reflex-dev/reflex/issues/2932

picklelo avatar Mar 26 '24 19:03 picklelo