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

Open
#1,593 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
55/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
api

Research direction

Locate the Decimal scalar's parse_value implementation in the Graphene codebase and compare its current conversion with the linked Strawberry implementation. Verify that parsing 0.01 preserves the intended decimal value and that invalid input still returns Undefined; add or update focused coverage if the surrounding tests identify an appropriate location.

Written by the indexing model from the issue text.

Description

🐛 bug

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 gives0.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

Suggested Fix

Modify the parse_value function as follows:

@staticmethod
def parse_value(value):
    try:
        return _Decimal(str(value))
    except Exception:
        return Undefined
Dominant language
Python
Stars
8.2k
Forks
818
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from graphql-python/graphene

All issues in graphql-python/graphene

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.