danielgtaylor / python-betterproto

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

Crash when field has the same name as a system type #53

Closed nyurik closed 4 years ago

nyurik commented 4 years ago

This oneof definition generates valid Python code, but later it is not possible to parse or create an instance of TileValue because the name of the field is the same as the type. Should betterproto rename such fields automatically, or at least throw an error during the code generation? I spent considerable time trying to understand why this line was saying t is not a class: https://github.com/danielgtaylor/python-betterproto/blob/3546f55146a4b41d6cfebb7872da0c17bd9277a1/betterproto/__init__.py#L594

message Tile {
        message Value {
                oneof val {
                        string string = 1;
                        float float = 2;
                        double double = 3;
                        int64 int = 4;
                        uint64 uint = 5;
                        sint64 sint = 6;
                        bool bool = 7;
                }
        }

        repeated Value values = 1;
}
@dataclass
class Tile(betterproto.Message):
    values: List["TileValue"] = betterproto.message_field(1)

@dataclass
class TileValue(betterproto.Message):
    string: str = betterproto.string_field(1, group="val")
    float: float = betterproto.float_field(2, group="val")
    double: float = betterproto.double_field(3, group="val")
    int: int = betterproto.int64_field(4, group="val")
    uint: int = betterproto.uint64_field(5, group="val")
    sint: int = betterproto.sint64_field(6, group="val")
    bool: bool = betterproto.bool_field(7, group="val")
boukeversteegh commented 4 years ago

Added testcase namespace_builtin_types

boukeversteegh commented 4 years ago

This should have been fixed now for all types mentioned.

70 documents the remaining unsupported names

boukeversteegh commented 4 years ago

Thank you for your report!