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-3009] type transforming serializers #3227

Merged
merged 10 commits into from
Jun 7, 2024

Conversation

benedikt-bartscher
Copy link
Contributor

No description provided.

@benedikt-bartscher benedikt-bartscher changed the title wip type transforming serializers WIP: type transforming serializers May 4, 2024
@benedikt-bartscher benedikt-bartscher mentioned this pull request May 14, 2024
1 task
@benedikt-bartscher
Copy link
Contributor Author

Instead of returning a tuple with the serialized type we could also add a kwarg to the serialize decorator.

@serialize(to=str)
def serialize_datetime(dt: datetime):
    return str(dt)

Serializers can also return a tuple of `(serialized_value, type)`, if both ways
are specified, then the returned value MUST match the `to` parameter.

When initializing a new rx.Var, if `_var_is_string` is not specified and the serializer returns a `str` type, then mark `_var_is_string=True` to indicate that the Var should be treated like a string literal.

Include datetime, color, types, and paths as "serializing to str" type.

Avoid other changes at this point to reduce fallout from this change:

  Notably, the `serialize_str` function does NOT cast to `str`, which
  would cause existing code to treat all Var initialized with a str as a
  str literal even though this was NOT the default before.

Update test cases to accomodate these changes.
In the future, we will treat strings as string literals in the JS code. To get
a Var that is not treated like a string, pass _var_is_string=False.

This will allow our serializers to automatically identify cast string literals
with less special cases (and the special cases need to be explicitly
identified).
@masenf masenf changed the title WIP: type transforming serializers [REF-3009] type transforming serializers Jun 3, 2024
@masenf masenf marked this pull request as ready for review June 3, 2024 23:56
Copy link
Contributor Author

@benedikt-bartscher benedikt-bartscher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@masenf your changes look good to me, thank you for jumping in!
I am thinking about dropping the tuple return in favor of the new to kwarg. If I am not missing any advantages of the tuple return, I think it would be better to have one way to do things.
The tuple approach would allow dynamic serialization types (type based on input value), but we can't use them in the frontend, so I see no real benefit.

Simplify the logic; instead of making a wrapper function that returns
a tuple, just save the type conversions in a separate global.
Copy link
Contributor Author

@benedikt-bartscher benedikt-bartscher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@masenf thanks for refactoring, looks good to me!

@masenf masenf merged commit e42d4ed into reflex-dev:main Jun 7, 2024
47 checks passed
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