Skip to content

Conversation

maximagupov
Copy link

@maximagupov maximagupov commented Jul 22, 2022

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

@maximagupov maximagupov changed the title Enum and import fixes [do not merge] Enum and import fixes Jul 25, 2022
@maximagupov maximagupov changed the title [do not merge] Enum and import fixes [do not merge] Enum and import fixes (based on v2.0.0b4) Jul 25, 2022
global_vars = {}
for base in inspect.getmro(cls):
module = inspect.getmodule(base)
global_vars.update(vars(module))
Copy link

@yinnie yinnie Jul 25, 2022

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

Copy link
Author

@maximagupov maximagupov Jul 26, 2022

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

Copy link

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)
Copy link

Choose a reason for hiding this comment

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

nice catch!

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