Skip to content

Return Decimals instead of floats? #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
euresti opened this issue Nov 1, 2018 · 4 comments
Closed

Return Decimals instead of floats? #136

euresti opened this issue Nov 1, 2018 · 4 comments

Comments

@euresti
Copy link

euresti commented Nov 1, 2018

Hi. I was playing with the SDK and noticed I was getting float values back from the calls.
This means that if you try to use these numbers you might hit weird floating point math issues.

A quick solution is to add parse_float=Decimal to json.loads would that be possible, or at least configurable?

@euresti euresti changed the title Return Decimals instead of floats? Return Decimals instead of floats? Nov 1, 2018
zahanm pushed a commit to zahanm/collect-beans that referenced this issue Aug 31, 2019
Sadly, `plaid-python` parses these numbers as `float` plaid/plaid-python#136
@phoenixy1
Copy link
Contributor

Hi @euresti, long time no see!

Closing this as it's about the legacy clibs, which we aren't updating any more and we really need to fix this in the API, not the libraries. I'm going to try to get this out in the next api release (which may not be any time super soon).

@jfly
Copy link

jfly commented Oct 2, 2022

@phoenixy1 sorry for the necrobump, but I'd be curious to understand this a little more.

It looks like Plaid released a new "sync" api since your message, but as far as I can tell, it says it returns a double. I'm honestly not clear on what that means in the context of JSON, which does not have a double type, just a number type. AFAICT, it's actually perfectly safe for JSON to use JSON numbers as the API does (we're just sending characters over the wire, not floating point numbers), so it feels to me like maybe it's just wrong that the docs say "double" when they should instead say something like "this is a JSON number, be careful about how you parse it to avoid floating point issues".

image

@phoenixy1
Copy link
Contributor

@jfly ok so the deal is that the docs are generated from the OpenAPI file, which defines Plaid API. Within OpenAPI, double is an accepted format descriptor for a number type. OpenAPI is also used to generate the client libraries. So while pure JSON doesn't have a concept of a double, double is used as a hint to the client libraries to tell them which types to assign to values in the responses, meaning that the double-ness does flow through to client libraries that have a concept of double vs. number (such as Go). For more info on this you can check out https://swagger.io/docs/specification/data-models/data-types/

You're right about this being kind of weird when considered in the context of JSON, and we definitely could modify the docs to override displaying the format values from OpenAPI in favor of something else, or to not display them at all, but in general we haven't seen a lot of confusion on this point and I think what we're currently doing might be the clearest way to present this information.

@jfly
Copy link

jfly commented Oct 15, 2022

Thanks for the explanation @phoenixy1. I have 0 experience with OpenAPI, but after reading through their issue tracker, it sounds like an arbitrary precision "decimal" format is something that people keep asking for, and I believe does not exist:

It looks like some people have implemented various workarounds (I think maybe OpenAPI gives you a hook to extend the available formats? I don't know...), but either way, I can understand you not wanted to deal with something nonstandard.

in general we haven't seen a lot of confusion on this point

That is surprising to me. I'd expect anyone who has dealt with floating point imprecision in the past to freak out a little if they find floating point numbers anywhere near currency. I certainly did!

Would you be open to a PR to get this Python library to actually return Decimal instead of floats? It looks like #183 started to try to do that but fizzled out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants