Skip to content

Encode oneOfs as Python unions for reliable handling. #452

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

Closed
heimalne opened this issue Jan 10, 2023 · 1 comment
Closed

Encode oneOfs as Python unions for reliable handling. #452

heimalne opened this issue Jan 10, 2023 · 1 comment
Labels
duplicate This issue or pull request already exists

Comments

@heimalne
Copy link

heimalne commented Jan 10, 2023

Currently, betterproto encodes protobuf oneOfs as a attributes of the enclosing class with one attribute per oneOf alternative. This leads to an akward runtime-encoding, where all attributes get an object with dummy data and the actual oneOf has non-dummy data. To distinguish those cases we need helper functions like serialized_on_wire() or which_on_of(), and the mypy type checker can't help us to ensure we've covered all alternatives. A better encoding would be Python unions (a.k.a Choice types), which get full exhaustiveness-support mypy support with Python 3.11 (https://docs.python.org/3.11/library/typing.html#typing.assert_never) and backwards compatible workaround for older Python versions (https://hakibenita.com/python-mypy-exhaustive-checking)

Would this other union encoding be suitable for betterproto?

@Gobot1234
Copy link
Collaborator

Duplicate of #358

@Gobot1234 Gobot1234 marked this as a duplicate of #358 Jan 10, 2023
@Gobot1234 Gobot1234 added the duplicate This issue or pull request already exists label Jan 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants