Inaccurate Float-to-Decimal Conversion in `parse_value` of `Decimal` `Scalar`
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 55/100
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
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
- Call
Decimal.parse_value(0.01). - Observe that the resulting
Decimaldoes not accurately represent the value0.01instead gives0.01000000000000000020816681711721685132943093776702880859375which 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.
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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from graphql-python/graphene
-
Tutorial mistakes Open🐛 bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
graphql-python/graphene#1389 · 5 comments · 2 reactions ·
-
Python 3.14 support Open✨ enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 38/100
graphql-python/graphene#1601 · 2 comments ·
-
✨ enhancement
Difficulty 4/5 3-5 days Newbie friendliness 42/100
graphql-python/graphene#1600 ·
-
🐛 bug
Difficulty 4/5 3-5 days Newbie friendliness 25/100
graphql-python/graphene#1592 ·
-
✨ enhancement
Difficulty 5/5 Over a week Newbie friendliness 30/100
graphql-python/graphene#1577 ·
All issues in graphql-python/graphene
Similar issues
-
documentation help wanted
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
simonw/sqlite-utils#872 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100