danielgtaylor / danielgtaylor/python-betterproto

Nested packages causes invalid imports

Open
#238 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

compiler-bug
Dominant language
Python
Stars
1.8k
Forks
234
PR merge metrics
No merged PRs in 30d

Description

Compilation of proto files using 2.0.0b3

protoc -I . --python_betterproto_out=prototest base.proto example.proto
# base.proto
syntax = "proto3";

package Foo;

message Bar {
    string value = 1;
}
# example.proto
syntax = "proto3";

import "base.proto";

package Foo.Example;

message Example {
    Bar value = 1;
}

returns Foo/__init__.py

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

import betterproto
from betterproto.grpc.grpclib_server import ServiceBase


@dataclass(eq=False, repr=False)
class Bar(betterproto.Message):
    value: str = betterproto.string_field(1)

Foo/Example/__init__.py

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

import betterproto
from betterproto.grpc.grpclib_server import ServiceBase


@dataclass(eq=False, repr=False)
class Example(betterproto.Message):
    value: "__FooBar__" = betterproto.message_field(1)


from ... import FooBar as __FooBar__

The last import in Foo/Example/__init__.py is invalid, since there is no FooBar module. It probably should have been from Foo import Bar as __FooBar__.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the shown protoc command and inspect the generated Foo/init.py and Foo/Example/init.py files. Trace how the imported Bar message is resolved during generation; done means the nested package output contains a valid import for Bar and the generated files compile successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.