should fix some import issues with python 2.7

This commit is contained in:
Mark Chackerian 2018-07-16 17:20:49 -04:00
parent de050fa6db
commit 0fdc2ca3eb
2 changed files with 9 additions and 3 deletions

View File

@ -1,4 +1,6 @@
import decimal from __future__ import absolute_import
from decimal import Decimal as _Decimal
from graphql.language import ast from graphql.language import ast
@ -11,7 +13,9 @@ class Decimal(Scalar):
""" """
@staticmethod @staticmethod
def serialize(dec): def serialize(dec):
assert isinstance(dec, decimal.Decimal), ( if isinstance(dec, str):
dec = _Decimal(dec)
assert isinstance(dec, _Decimal), (
'Received not compatible Decimal "{}"'.format(repr(dec)) 'Received not compatible Decimal "{}"'.format(repr(dec))
) )
return str(dec) return str(dec)
@ -24,6 +28,6 @@ class Decimal(Scalar):
@staticmethod @staticmethod
def parse_value(value): def parse_value(value):
try: try:
return decimal.Decimal(value) return _Decimal(value)
except ValueError: except ValueError:
return None return None

View File

@ -21,6 +21,7 @@ def test_decimal_string_query():
assert result.data == { assert result.data == {
'decimal': str(decimal_value) 'decimal': str(decimal_value)
} }
assert decimal.Decimal(result.data['decimal']) == decimal_value
def test_decimal_string_query_variable(): def test_decimal_string_query_variable():
@ -34,6 +35,7 @@ def test_decimal_string_query_variable():
assert result.data == { assert result.data == {
'decimal': str(decimal_value) 'decimal': str(decimal_value)
} }
assert decimal.Decimal(result.data['decimal']) == decimal_value
def test_bad_decimal_query(): def test_bad_decimal_query():