graphql-python / graphql-python/graphene

Inaccurate Float-to-Decimal Conversion in `parse_value` of `Decimal` `Scalar`

Aperta
#1,593 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
🐛 bug
Lingua principale
Python
Stelle
8.2k
Fork
818
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

## Current Behavior
The `parse_value` function currently converts the input using `_Decimal(value)`. When a float is passed as input, this conversion may lead to precision loss due to the inherent imprecision of floating-point representations.

## Steps to Reproduce
1. Call `Decimal.parse_value(0.01)`.
2. Observe that the resulting `Decimal` does not accurately represent the value `0.01` instead gives`0.01000000000000000020816681711721685132943093776702880859375` which is inaccurate.

## Expected Behavior
The function should convert the input to a string before creating the `Decimal` object (i.e., using `_Decimal(str(value))`). This approach ensures that decimal values are accurately converted, even if not provided as string, preserving their intended precision.

see [Strawberry implementation](https://github.com/strawberry-graphql/strawberry/blob/2af374359e9722980f5bc15b0d7158c8d54367b6/strawberry/schema/types/base_scalars.py#L25)

## Suggested Fix
Modify the `parse_value` function as follows:

```python
@staticmethod
def parse_value(value):
try:
return _Decimal(str(value))
except Exception:
return Undefined
```

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Direzione di ricerca

Individua l’implementazione di parse_value di Decimal scalar nel codice di Graphene e confronta la conversione attuale con l’implementazione collegata di Strawberry. Verifica che il parsing di 0.01 preservi il valore decimale previsto e che l’input non valido restituisca ancora Undefined; aggiungi o aggiorna una copertura mirata se i test circostanti individuano una posizione appropriata.

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

Valutazione

Stack tecnologico
python
Ambito
api
Tipo di issue
Bug
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
55/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.