Skip to content

ImmutableVar.guess_type() hides the original var type on fields derived from integral types #3890

Closed

Description

@abulvenz

Describe the bug
In the example below you can see my problem, currently the bottom assertion fails.

To Reproduce

class Email(str):
    ... # validation and stuff ...
    
class State(rx.State):
  email: Email = Email("[email protected]")

assert State.email._original_var._var_type == Email  # passes
assert State.email._var_type == Email  # fails

Expected behavior
I would expect the type information to be preserved.

Specifics (please complete the following information):

  • Python Version: 3.12.5
  • Reflex Version: main
  • OS: archlinux
  • Browser (Optional): Firefox

Additional context
Maybe something like this could help (Would also be needed for the other integral types)

class ToStringOperation(...):

   def create(...):
        var_type = (
            original_var._var_type if issubclass(original_var._var_type, str) else str
        )

        return cls(
            _var_name="",
            _var_type=var_type,
            _var_data=ImmutableVarData.merge(_var_data),
            _original_var=original_var,
        )

Activity

  1. added
    bug
    on Sep 5, 2024
  2. abulvenz commented on Sep 5, 2024

    @abulvenz
    ContributorAuthor

    Or maybe

            var_type = (
                original_var._var_type 
                if original_var is not None 
                and isclass(original_var._var_type) 
                and issubclass(original_var._var_type, str) 
                else str
            )
  3. abulvenz commented on Sep 5, 2024

    @abulvenz
    ContributorAuthor

    Failing test:

    def test_to_string_operation():
        class Email(str): ...
    
        class TestState(BaseState):
            optional_email: Optional[Email] = None
            email: Email = "[email protected]"
    
        assert (
            str(TestState.optional_email) == f"{TestState.get_full_name()}.optional_email"
        )
        my_state = TestState()
        assert my_state.optional_email is None
        assert str(my_state.email) == "[email protected]"
    
        assert cast(ImmutableVar, TestState.email)._var_type == Email
        assert cast(ImmutableVar, TestState.optional_email)._var_type == Optional[Email]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions