-
Notifications
You must be signed in to change notification settings - Fork 37
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
KeyError when declaring an enumeration #171
Comments
My short-term solution has been adding this line to every affected source file: __protobuf__ = proto.module(package=__name__) If this behavior is intended, wouldn't be nice to default |
Yes, when writing tests and defining proto-plus types inline in those tests, you've found the necessary workaround. An alternative would be to make the enum a nested type of the message type. E.g. def test_squid():
class Squid(proto.Message):
class Color(proto.Enum):
COLOR_UNSPECIFIED = 0
RED = 1
GREEN = 2
BLUE = 3
BROWN = 4
class Chromatophore(proto.Message):
color = proto.Field(Color, number=1)
chromatophores = proto.RepeatedField(Chromatophore, number=1)
s = Squid() This is an unfortunate but hopefully minor wart that's part of how proto plus defines and organizes manifests. The manifests are necessary because we need to make sure that all message and enum types have been defined before we send that data to the underlying protobuf implementation to generate types. |
Thank you very much, @software-dov! As per the style guide, it's «[preferible to prefix] enum values instead of surrounding them in an enclosing message», so that minor wart is good enough for me. Nevertheless, I think that it would be nice to, either:
|
That's a very good point. I personally would argue that it's less important when writing tests, but on the other hand constant consistency with the style guide is no bad thing. Whether or not it's a good idea to define proto types from a top-level file is a discussion for another day. I don't think that making this change is likely to break anything there. Defining message/enum types from an interactive prompt already has a few unexpected side effects that complicate interactive development. I'm less concerned with breaking changes to this workflow since by definition it isn't part of a codebase. Making the module name the default package name seems reasonable. I'll make a new bugfix tracking issue and reference this in that in order to preserve history without cluttering the description. |
Agreed! In my particular case, it matters because of readability and deduplication, but it wouldn't otherwise.
I wouldn't mind doing so for a quick sanity check, but it doesn't look like one of the the best practices for any minimally stable piece of software; we can postpone that debate ad æternum.
Great! I've opened #176 to fix #175.
Yes, it looks like it's impossible to redefine a protocol buffer class once it has been registered and there might be similar state-related issues for other interactive-specific behaviors. Nevertheless, solving them doesn't look like a priority right now. |
Feel free to reopen this issue if you deem it necessary. |
Error
Environment
Steps to reproduce
Configure a virtual environment
Install the package
Run the official example
Possible solutions
Placing the enumeration declaration inside a package and setting
__protobuf__
, as done in marshalling tests:proto-plus-python/tests/test_marshal_types_enum.py
Line 21 in 50b87af
Defining enumeration names as a strings inside message fields prior to the actual enumeration declaration:
proto-plus-python/tests/test_fields_enum.py
Lines 19 to 27 in 50b87af
The text was updated successfully, but these errors were encountered: