danielgtaylor / danielgtaylor/python-betterproto

import name collisions

Aperta
#420 1 commento 0 reazioni 0 assegnatari Vedi su GitHub
compiler-bug
Lingua principale
Python
Stelle
1.8k
Fork
234
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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'
```

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Riproduci il problema con la directory proto/ mostrata e il comando protoc, quindi esamina gli import generati in gen/proto/baz.py. Traccia il modo in cui il generatore assegna i nomi agli import per thing1/core e thing2/core. Il lavoro è completato quando baz.py può fare riferimento sia a Foo sia a Bar senza che un import di core sovrascriva l’altro.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.