danielgtaylor / danielgtaylor/python-betterproto
Return actual enum's from to_dict
- Lingua principale
- Python
- Stelle
- 1.8k
- Fork
- 234
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Descrizione
Perhaps I don't understand the intended use of the `to_dict` method but, at least for my use case, I would prefer it to return the actual enum instead of it's name as a string.
For example:
```
class EnumResultGeneric(betterproto.Enum):
RESULT_UNKNOWN = 0
RESULT_SUCCESS = 1
@dataclass
class ResponseGeneric(betterproto.Message):
result: "EnumResultGeneric" = betterproto.enum_field(1)
status = ResponseGeneric.FromString(bytes(bytearray([0x08, 0x01]))).to_dict()['result']
print(f"{status}: {type(status)}")
```
gives `RESULT_SUCCESS: ` instead of the actual `EnumResultGeneric`.
It is a simple change to get this desired behavior in the `to_dict` method:
```
elif meta.proto_type == TYPE_ENUM:
enum_values = list(
self._betterproto.cls_by_field[field.name]
) # type: ignore
if isinstance(v, list):
output[cased_name] = [enum_values[e] for e in v]
else:
output[cased_name] = enum_values[v]
```
Is this desirable for other people? Maybe allow this as a parameter to `to_dict`?
Guida per i contributori
Apri la guida per i contributori
Direzione di ricerca
Individua l’implementazione di `to_dict` di message e ispeziona la gestione di `TYPE_ENUM`, quindi riproduci il problema con l’esempio `ResponseGeneric`. Determina se le istanze enum debbano essere sempre restituite o se questo debba dipendere da un parametro; il lavoro è completato quando il comportamento scelto è specificato e verificato per i campi enum singoli e ripetuti.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- api, backend
- Tipo di issue
- Funzionalità
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100