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

improve object var symantics #4290

Merged
merged 10 commits into from
Nov 5, 2024
Merged

improve object var symantics #4290

merged 10 commits into from
Nov 5, 2024

Conversation

adhami3310
Copy link
Member

@benedikt-bartscher made some changes here, let me know if any more is needed

@benedikt-bartscher
Copy link
Contributor

benedikt-bartscher commented Nov 3, 2024

Hi @adhami3310 thanks!
If i read the code correctly, attribute access is still not allowed for bare classes like this one:

class Bare:
  @property
  def prop(self) -> int:
    return 4

@serializer(to=dict)
def serialize_bare(bare: Bare) -> dict[str, int]:
  return {"prop": bare.prop}

I would love to see a fallback to ObjectVar (a bit like #3911). At least if there is a serializer which serializes to dict. Btw, i once tried to add the to= argument to all serializers and determine if smth is an ObjectVar only based on that serializer arg. It just worked fine, all tests passed.
Alternatively we could add a typing stub class like this:

class IsObjectVar: ...

class Bare(IsObjectVar): ...

@adhami3310
Copy link
Member Author

attribute access is still not allowed for bare classes like this one

I believe so, I can add an additional check for has_serializer.

@benedikt-bartscher
Copy link
Contributor

benedikt-bartscher commented Nov 4, 2024

attribute access is still not allowed for bare classes like this one

I believe so, I can add an additional check for has_serializer.

I would suggest limiting this case to serializers which serialize to dict only.
Otherwise, it will be true for this:

class MyCls: ...

@serializer(to=str)
def serialize_my_cls(my_cls: MyCls) -> str:
  return "hi"

Btw, I still think we can narrow this down to serializers and only use the serializers to determine ObjectVar support (dataclasses and rx.Base also have serializers to dict) This will shrink down the checks, make the code cleaner by removing all those cases and potentially even improve performance

@benedikt-bartscher
Copy link
Contributor

Refs:
#3911
#4269
#4268 (maybe we should pick some of the tests from that PR)

@adhami3310
Copy link
Member Author

I would suggest limiting this case to serializers which serialize to dict only.

Should be done!

maybe we should pick some of the tests from that PR

we should, I will try to do that

Copy link
Contributor

@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.

Thanks, @adhami3310 this looks great! I will test it against our code now

Edit: works great!

tests/units/vars/test_object.py Outdated Show resolved Hide resolved
@adhami3310 adhami3310 merged commit b5d1e03 into main Nov 5, 2024
29 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.

3 participants