Skip to content
This repository was archived by the owner on Feb 23, 2026. It is now read-only.
This repository was archived by the owner on Feb 23, 2026. It is now read-only.

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

Closed

Description

@dizcology

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 {
    }
  }
}

Activity

  1. software-dov commented on Nov 11, 2020

    @software-dov
    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)}))
  2. added
    priority: p3Desirable enhancement or fix. May not be included in next release.
    type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.
    and removed
    triage meI really want to be triaged.
    on Nov 12, 2020
  3. added
    🚨This issue needs some love.
    and removed
    🚨This issue needs some love.
    on May 12, 2021
  4. busunkim96 commented on Oct 6, 2021

    @busunkim96
    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).

  5. added
    🚨This issue needs some love.
    and removed
    🚨This issue needs some love.
    on Apr 4, 2022
  6. kurtqq commented on Feb 13, 2023

    @kurtqq

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

  7. orby-fabian commented on Mar 3, 2023

    @orby-fabian

    +1

  8. parthea commented on Mar 16, 2023

    @parthea
    Contributor

    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 {
        }
      }
    }
    ``
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

priority: p3Desirable enhancement or fix. May not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions