danielgtaylor / python-betterproto

Clean, modern, Python 3.6+ code generator & library for Protobuf 3 and async gRPC
MIT License
1.51k stars 214 forks source link

Namespaced enums yield incorrectly generated code #197

Open Teivaz opened 3 years ago

Teivaz commented 3 years ago

Given following proto file

syntax = "proto3";
package namespaced_enum;

message ns { enum Type {
    NONE = 0;
    option_one = 1;
}}

message Msg {
    ns.Type t = 1;
}

The command python -m grpc_tools.protoc -I . --python_betterproto_out=lib ./test.proto generates following code:

# Generated by the protocol buffer compiler.  DO NOT EDIT!
# sources: proto/test.proto
# plugin: python-betterproto
from dataclasses import dataclass

import betterproto

from .namespaced_enum import ns

class NsType(betterproto.Enum):
    NONE = 0
    option_one = 1

@dataclass
class Ns(betterproto.Message):
    pass

@dataclass
class Msg(betterproto.Message):
    t: ns.Type = betterproto.enum_field(1)

It contains line from .namespaced_enum import ns and later on uses this as type t: ns.Type = betterproto.enum_field(1) instead of NsType


Python 3.8.2 betterproto 1.2.5 grpcio_tools 1.34.1

Teivaz commented 3 years ago

Interestingly enough the code is generated correctly when ns is replaced with Ns

syntax = "proto3";
package namespaced_enum;

message Ns { enum Type {
    NONE = 0;
    option_one = 1;
}}

message Msg {
    Ns.Type t = 1;
}
#...
@dataclass
class Msg(betterproto.Message):
    t: "NsType" = betterproto.enum_field(1)