Pr0Ger / protobuf3

Protocol buffers library for Python 3
Other
5 stars 5 forks source link

Required fields in embedded messages #3

Open getkey opened 9 years ago

getkey commented 9 years ago

When I set a field in an embedded message and there are required fields left to set, for example, in that case:

message Parent {
    optional Children children = 1;
}
message Children {
    required string name = 1;
    required uint32 age = 2;
}
import test
mother = test.Parent()

mother.children.age = 12
mother.children.name = "Camille"

This exception is thrown:

Traceback (most recent call last):
  File "use_it.py", line 5, in <module>
    mother.children.age = 12
  File "/usr/local/lib/python3.4/dist-packages/protobuf3/fields/base.py", line 82, in __set__
    self._convert_to_wire_type(value))
  File "/usr/local/lib/python3.4/dist-packages/protobuf3/message.py", line 168, in _set_wire_values
    msg._set_wire_values(number, FIELD_VARIABLE_LENGTH, self.encode_to_bytes(), idx)
  File "/usr/local/lib/python3.4/dist-packages/protobuf3/message.py", line 184, in encode_to_bytes
    self._check_required_fields()
  File "/usr/local/lib/python3.4/dist-packages/protobuf3/message.py", line 144, in _check_required_fields
    raise KeyError("Some required fields are missing: " + ", ".join(missing_fields))
KeyError: 'Some required fields are missing: name'

When we set the age, the name isn't set yet, so this error is thrown. Maybe it can be fixed with a temporary object that hold the age while waiting for the name to be chosen, and when every required field has gotten its value, set them all at once?

Remboooo commented 7 years ago

+1 for this issue. I haven't found any workaround besides modifying the fields to be optional.