Skip to content
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

Can't set any_pb2.Any field via assignment or from constructor #290

Open
software-dov opened this issue Feb 7, 2022 · 2 comments
Open
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@software-dov
Copy link
Contributor

import proto
import protobuf.any_pb2 as any_pb2

class Container(proto.Message):
    contents = proto.Field(any_pb2.Any, number=1)
    
class Thing(proto.Message):
    name = proto.Field(proto.STRING, number=1)
    
a = any_pb2.Any()
a.Pack(Thing(name="Steve"))
c = Container(contents=a)   # This works
c = Container()
c.contents = a    # As does this

c = Container(contents=Thing(name="Alice"))    # This fails
c = Container()
c.contents = Thing(name="Alice")    # This also fails

Assignment to an Any field does not do automatic packing/unpacking. This is annoying and unintuitive.

@software-dov software-dov added type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. priority: p2 Moderately-important priority. Fix may not be included in next release. labels Feb 7, 2022
@maffoo
Copy link

maffoo commented Apr 6, 2022

I agree using any_pb2.Any with proto-plus is quite awkward, but I'm not sure that automatic packing and unpacking is the best solution, since sometimes one wants to preserve an Any message and work with it without repeated packing and unpacking. I think the root of the problem is that any_pb2.Any has an awkward API that is not very pythonic (can't pack or unpack in a single expression for example, instead one has to create the any message and call the Pack method as in the issue description). If proto-plus had it's own proto.Any type this could be improved significantly, something like:

import proto

class Container(proto.Message):
    contents = proto.Field(proto.Any, number=1)

class Thing(proto.Message):
    name = proto.Field(proto.String, number=1)

c = Container(contents=proto.Any(Thing(name="Steve")))
c.contents = proto.Any(Thing("Steph"))

thing = c.contents.unpack()  # unpacks the message in the Any

@vam-google vam-google added type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. and removed type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Aug 29, 2022
@vam-google
Copy link
Contributor

Changed the status of it from bug to feature request, since it is about usability and unintuitive behavior, which, nonetheless works by design now. Also, status of proto-plus may change in the future (we may remove it from the surface or get it integrated with protobuf more naively, because currently it causes a lot of compatibility and performance issues). Making major changes in proto-plus surface is not a priority this time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

No branches or pull requests

3 participants