Skip to content

import name collisions #420

Open
Open
@devintrowbridge

Description

@devintrowbridge

I am trying to transform a fairly complex directory structure of .proto messages using betterproto. I'm running into an issue where subdirectories that share a name collide with each other when a single proto message uses both. Note that I have no control over the directory structure or the contents of the proto messages.

Here's the proto messages as I receive them. Note the common core folder across thing1 and thing2

$ tree proto/
proto/
├── baz.proto
├── thing1
│   └── core
│       └── foo.proto
└── thing2
    └── core
        └── bar.proto
4 directories, 3 files

Then I generate my python files using protoc and get the following output tree.

$ find ./proto/ -iname *.proto | xargs -I% protoc -I ./proto/--python_betterproto_out=./gen/ %
...
$ tree ./gen
./gen
├── __init__.py
└── proto
    ├── baz.py
    ├── __init__.py
    ├── thing1
    │   ├── core.py
    │   └── __init__.py
    └── thing2
        ├── core.py
        └── __init__.py

3 directories, 7 files

Opening baz.py reveals the following (comment mine):

from dataclasses import dataclass

import betterproto

from .proto.thing1 import core
from .proto.thing2 import core # Overwrites core above!

@dataclass
class Foo(betterproto.Message):
    foo: core.Foo = betterproto.message_field(1)
    bar: core.Bar = betterproto.message_field(2)

And trying to use it expectedly gives:

Python 3.6.8 (default, Sep 13 2022, 09:02:49) 
[GCC 8.5.0 20210514 (Red Hat 8.5.0-10)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from gen.proto import baz
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/devin/workspace/sandbox/proto/gen/proto/baz.py", line 13, in <module>
    class Foo(betterproto.Message):
  File "/home/devin/workspace/sandbox/proto/gen/proto/baz.py", line 14, in Foo
    foo: core.Foo = betterproto.message_field(1)
AttributeError: module 'gen.proto.thing2.core' has no attribute 'Foo'

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions