Skip to content

= should have transitive assignment for nested fields, not copying behavior #190

@BenRKarl

Description

@BenRKarl

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 bugpriority: p2Moderately-important priority. Fix may not be included in next release.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions