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

Message with message nested in map cannot be created with dict #161

Closed
dizcology opened this issue Nov 11, 2020 · 5 comments
Closed

Message with message nested in map cannot be created with dict #161

dizcology opened this issue Nov 11, 2020 · 5 comments
Assignees
Labels
priority: p3 Desirable enhancement or fix. May not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@dizcology
Copy link

Generate the python GAPIC library with the following proto:

// in googleapis/google/cloud/mypackge/v1/some.proto
syntax = "proto3";

package google.cloud.mypackage;

message Outer {
    AMap inner = 1;
}

message AMap {
    map<string, Stuff> some_mapping = 1;
}

message Stuff {}

The resulting library cannot be used to create certain Outer messages:

(1)

mypackage.Outer(inner={'some_mapping': {'a': {}}})

raises TypeError: Parameter to MergeFrom() must be instance of same class: expected google.cloud.mypackage.Stuff got dict..

(2)

mypackage.Outer(inner={'some_mapping': {'a': mypackage.Stuff()}})

raises TypeError: Parameter to MergeFrom() must be instance of same class: expected google.cloud.mypackage.Stuff got Stuff..

(3)

mypackage.Outer(inner=mypackage.AMap(some_mapping={'a': mypackage.Stuff()}))

works fine, and builds:

inner {
  some_mapping {
    key: "a"
    value {
    }
  }
}
@software-dov software-dov transferred this issue from googleapis/gapic-generator-python Nov 11, 2020
@software-dov
Copy link
Contributor

Minimal reproducing type:

class Boat(proto.Message): 
    class Net(proto.Message): 
        class Mollusc(proto.Message):                                                                                                                                                                                                   
            mass_kg = proto.Field(proto.INT32, number=1) 

        molluscs = proto.MapField(proto.STRING, Mollusc, number=1) 
          
    net = proto.Field(Net, number=1)

b = Boat(net={"molluscs": {"squid": {"mass_kg": 100}}})    # raises an exception

Workaround is to explicitly use types, e.g.

b = Boat(net=Boat.Net(molluscs={"squid": Boat.Net.Mollusc(mass_kg=20)}))

@yoshi-automation yoshi-automation added the triage me I really want to be triaged. label Nov 12, 2020
@software-dov software-dov added priority: p3 Desirable enhancement or fix. May not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. and removed triage me I really want to be triaged. labels Nov 12, 2020
@yoshi-automation yoshi-automation added 🚨 This issue needs some love. and removed 🚨 This issue needs some love. labels May 12, 2021
@busunkim96
Copy link
Contributor

@software-dov Do you have a sense of how easy/hard it would be to fix this? It seems natural for folks to mix mix dict/type instantiation (especially when the request object is big).

@kurtqq
Copy link

kurtqq commented Feb 13, 2023

@software-dov are there planes to address this issue?

@orby-fabian
Copy link

+1

@parthea
Copy link
Contributor

parthea commented Mar 16, 2023

I'm not able to reproduce this issue. Please open a new issue if you're still experiencing the problem.

import google.cloud.mypackage as mypackage
print(mypackage.Outer(inner={'some_mapping': {'a': {}}}))
print(mypackage.Outer(inner={'some_mapping': {'a': mypackage.Stuff()}}))
print(mypackage.Outer(inner=mypackage.AMap(some_mapping={'a': mypackage.Stuff()})))

Tested with protobuf==3.19.5 protobuf==3.20.1 protobuf==4.22.1
results in

inner {
  some_mapping {
    key: "a"
    value {
    }
  }
}

inner {
  some_mapping {
    key: "a"
    value {
    }
  }
}

inner {
  some_mapping {
    key: "a"
    value {
    }
  }
}
``

@parthea parthea closed this as completed Mar 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p3 Desirable enhancement or fix. May not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

7 participants