danielgtaylor / danielgtaylor/python-betterproto

import name collisions

Open
#420 1 comment 0 reactions 0 assignees View on GitHub
compiler-bug
Dominant language
Python
Stars
1.8k
Forks
234
PR merge metrics
No merged PRs in 30d

Description

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.
```sh
$ 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):
```python
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
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 "", line 1, in
File "/home/devin/workspace/sandbox/proto/gen/proto/baz.py", line 13, in
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'
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.