Repository navigation
Serialize Decimal, Fraction and NumPy scalars as Int, Float and Boolean - #281
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
GraphQL-core 3.2 serialized values like
Decimal,Fractionor NumPy scalars asIntandFloat, because it converted them withint()andfloat(). That was never tested though. When the scalar coercion was streamlined for 3.3.0, explicitisinstancechecks replaced the conversion, and these values started raisingGraphQLError. This affected values returned by database drivers and ORMs (e.g. SQLAlchemyNumeric, DjangoDecimalField,SUM/AVGaggregates) and by NumPy/pandas (numpy.int64,numpy.float32, ...). Onlynumpy.float64kept working, because it subclassesfloat.GraphQL.js also unwraps objects via
valueOf()before coercing output values. Python's numeric protocols are the equivalent, so this change stays in line with the reference implementation.Changes to output coercion only (input coercion stays strict):
Intaccepts numbers of non-builtin types if they represent 32-bit integers exactly. Unlike 3.2, non-integer values such asDecimal("2.5")are rejected, not truncated.Floatconverts them withfloat()and rejects NaN, infinite and overflowing values. They may lose precision; use a custom scalar type to preserve it.Booleanalso accepts them (includingnumpy.bool), and now always returns a Pythonbool. Before,numpy.float64values produced anumpy.bool, whichjson.dumpscannot serialize.bytesorcomplexare still rejected.GraphQLInt,GraphQLFloatandGraphQLBooleanmention the accepted types.The tests use
Decimal,Fractionand NumPy-like stub classes, so NumPy is not needed as a test dependency. The behavior was also checked manually with NumPy 2.5.3.This fixes #280 - thanks for reporting!