-
Notifications
You must be signed in to change notification settings - Fork 0
[do not merge] Enum and import fixes (based on v2.0.0b4) #2
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
base: master
Are you sure you want to change the base?
Conversation
global_vars = {} | ||
for base in inspect.getmro(cls): | ||
module = inspect.getmodule(base) | ||
global_vars.update(vars(module)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is the bug that original implementation didn't loop over base classes of the cls
? why loop?
not much info in globalns here https://docs.python.org/3/library/typing.html#typing.get_type_hints
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the problem appears because betterproto generates code that uses type aliases, like:
class Msg:
device_type: '__common__.DeviceType'
from ... import common as __common__
when we inherit that class, by default python doesn't know anything about __common__
and we need that trick
Actually that's Andrey's fix from previous PR, I just moved it above new version
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah thanks for explaining! nice trick
@@ -1226,9 +1251,9 @@ def from_dict(self: T, value: Dict[str, Any]) -> T: | |||
elif meta.proto_type == TYPE_ENUM: | |||
enum_cls = self._betterproto.cls_by_field[field_name] | |||
if isinstance(v, list): | |||
v = [enum_cls.from_string(e) for e in v] | |||
elif isinstance(v, str): | |||
v = enum_cls.from_string(v) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice catch!
Fix inheritance from betterproto messages (cover cases when it tries to resolve module by name in the module the class sits).
Serialize enum by value, instead of by name to be comapartible with DomainModel/json which serializes by value