danielgtaylor / python-betterproto

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

Invalid code generated when package name contains uppercase letter #562

Open paskozdilar opened 3 months ago

paskozdilar commented 3 months ago

Summary

Title

Reproduction Steps

This protofile works:

// proto/test.proto
syntax = "proto3";

package p;

service Service {
  rpc Method(Empty) returns (Empty) {}
}

message Empty {}

This doesn't:

// proto/test.proto
syntax = "proto3";

package P;

service Service {
  rpc Method(Empty) returns (Empty) {}
}

message Empty {}

With the latter, betterproto compiles to this:

# Generated by the protocol buffer compiler.  DO NOT EDIT!
# sources: test.proto
# plugin: python-betterproto
# This file has been @generated

from dataclasses import dataclass
from typing import (
    TYPE_CHECKING,
    Dict,
    Optional,
)

import betterproto
import grpclib
from betterproto.grpc.grpclib_server import ServiceBase

from .. import PEmpty as _PEmpty__

# [...]

The from .. import PEmpty is invalid import as nothing exists in the parent package.

Command used:

python3 -m grpc_tools.protoc --proto_path=./proto/ --python_betterproto_out=./proto/ proto/*.proto

Python environment (output of pip freeze):

betterproto @ git+https://github.com/danielgtaylor/python-betterproto@5666393f9d10e13609d8eeac8d1ab3815dce5fd6
black==24.2.0
click==8.1.7
grpcio==1.62.1
grpcio-tools==1.62.1
grpclib==0.4.7
h2==4.1.0
hpack==4.0.0
hyperframe==6.0.1
isort==5.13.2
Jinja2==3.1.3
MarkupSafe==2.1.5
multidict==6.0.5
mypy-extensions==1.0.0
nodeenv==1.8.0
packaging==24.0
pathspec==0.12.1
platformdirs==4.2.0
protobuf==4.25.3
pyright==1.1.353
python-dateutil==2.9.0.post0
six==1.16.0
typing_extensions==4.10.0

Expected Results

Correct code generated

Actual Results

Incorrect code generated

System Information

libprotoc 3.12.4 Python 3.11.0rc1 Name: betterproto Version: 2.0.0b6 Summary: A better Protobuf / gRPC generator & library Home-page: https://github.com/danielgtaylor/python-betterproto Author: Daniel G. Taylor Author-email: danielgtaylor@gmail.com License: MIT Location: Requires: grpclib, python-dateutil, typing-extensions Required-by:

Checklist

gbmhunter commented 1 month ago

I have got the same issue. I'm running the generation on Linux with betterproto v2.0.0b6. Adding any uppercase letter to any part of the package name in the .proto causes the same import error, where there are imports that don't exist in the parent __init__.py. Problem goes away if I use all lowercase letters.