8000 which_one_of returns incorrect value for nested assignments · Issue #305 · danielgtaylor/python-betterproto · GitHub
[go: up one dir, main page]

Skip to content
which_one_of returns incorrect value for nested assignments #305
Open
@girtsf

Description

@girtsf

If a oneof contains a submessage, and a field is assigned to this submessage, which_one_of does not report the submessage. Here is a proto file, generated code and code to demonstrate the issue.

Proto:

syntax = "proto3";

message Inner {
  int32 a = 1;
}

message Outer {
  oneof which {
    Inner inner = 1;
  }
}

Generated code + reproduction of the bug:

from dataclasses import dataclass

import betterproto


@dataclass(eq=False, repr=False)
class Inner(betterproto.Message):
    a: int = betterproto.int32_field(1)


@dataclass(eq=False, repr=False)
class Outer(betterproto.Message):
    inner: "Inner" = betterproto.message_field(1, group="which")


test = Outer()
test.inner.a = 42
print(test)  # Outer(inner=Inner(a=42))
print(betterproto.which_one_of(test, "which"))  # ('', None)   <-- WRONG

# Round trip encode / parse.
test2 = Outer().parse(bytes(test))
print(test2)  # Outer(inner=Inner(a=42))
print(betterproto.which_one_of(test2, "which"))  # ('inner', Inner(a=42))   <-- correct

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0