-
Notifications
You must be signed in to change notification settings - Fork 37
Closed
Labels
P2A nice-to-fix bugA nice-to-fix bugpriority: p2Moderately-important priority. Fix may not be included in next release.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.‘Nice-to-have’ improvement, new feature or different behavior or design.
Description
The = operator is recommended to use in place of CopyFrom for nested message fields, so for example:
# with proto-plus
message.nested = copy
# with raw protobuf messages
message.nested.CopyFrom(copy)Syntactically this is nice, however it introduces unexpected behavior because the = operation in the first statement implicitly makes a copy of the second message.
With other Python objects, and in other programming languages, that = operator would make a transitive assignment so that message.nested and copy point to the same object in memory. So for example when using a dict:
message = {}
copy = {"foo": "bar"}
message["copy"] = copy
message["copy"]["another"] = 1
print(copy)
>>> {"foo": "bar", "another": 1}It might be better for the = operator to behave as it would for a dict or other objects, and to either expose a new interface specifically for copying objects, or to rely on conventional Python patterns like using the copy module.
Metadata
Metadata
Assignees
Labels
P2A nice-to-fix bugA nice-to-fix bugpriority: p2Moderately-important priority. Fix may not be included in next release.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.‘Nice-to-have’ improvement, new feature or different behavior or design.