danielgtaylor / danielgtaylor/python-betterproto
Message with oneof called method .to_pydict/.from_pydict fails
- Vorherrschende Sprache
- Python
- Sterne
- 1.8k
- Forks
- 234
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beschreibung
### Summary
Serializing a message with oneof fails by `to_pydict` or `from_pydict`.
### Reproduction Steps
Define a message and compile:
```protobuf
message Foo {
oneof group1 {
int32 bar = 1;
int32 baz = 2;
}
}
```
then called `Foo(bar=1).to_pydict` or `Foo().from_pydict({"bar": 1})`
### Expected Results
The method called success like `to_dict` and `from_dict`
### Actual Results
There will raise AttributeError: `'group1' is set to 'xxxx', not 'baz' ` / `'group1' is set to None, not 'bar'`:
```
Traceback (most recent call last):
File "D:\Projects\adapter-kritor\exam.py", line 22, in
res = foo1.to_pydict()
File "D:\Projects\adapter-kritor\.venv\lib\site-packages\betterproto\__init__.py", line 1717, in to_pydict
value = getattr(self, field_name)
File "D:\Projects\adapter-kritor\.venv\lib\site-packages\betterproto\__init__.py", line 836, in __getattribute__
raise AttributeError(
AttributeError: 'group1' is set to 'bar', not 'baz'
```
### System Information
libprotoc 27.2
Python 3.9.13
2.0.0b7
### Checklist
- [X] I have searched the issues for duplicates.
- [X] I have shown the entire traceback, if possible.
- [X] I have verified this issue occurs on the latest prelease of betterproto which can be installed using `pip install -U --pre betterproto`, if possible.
Beitragsleitfaden
Rechercherichtung
Beginne mit den Traceback-Stellen in betterproto/__init__.py, insbesondere mit dem Pfad von to_pydict um Zeile 1717 und der Behandlung von oneof-Attributen um Zeile 836. Reproduziere das Problem mit der Foo-Nachricht und ihrem group1-oneof sowohl mit to_pydict als auch mit from_pydict. Als erledigt gilt die Aufgabe, wenn beide Methoden konsistent mit dem bestehenden Verhalten von to_dict und from_dict erfolgreich ausgeführt werden.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- backend-api-design
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100