Skip to content
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

Add numbers as Decimal objects #7352

Closed
wants to merge 2 commits into from
Closed

Conversation

jtherrmann
Copy link

@jtherrmann jtherrmann commented Feb 17, 2024

Fixes #7349

TODO:

  • I've only updated the DynamoType.__add__ method, but there are other places where floating-point arithmetic is performed that should probably be updated as well, e.g. DynamoType.__sub__.
  • Add test cases for these changes?
  • Fix failing tests

On my machine, make test fails with:

moto/dynamodb/models/dynamo_type.py:109:48: error: Unsupported left operand type for + ("object")  [operator]
Found 1 error in 1 file (checked 900 source files)
make: *** [Makefile:30: lint] Error 1

@bblommers
Copy link
Collaborator

Hi @jtherrmann, thanks for getting this started. Do you want to tackle the TODO's yourself, or are you OK if someone else picks this up?

@jtherrmann
Copy link
Author

@bblommers I'd love it if someone else could finish it off!

@bblommers
Copy link
Collaborator

@jtherrmann No problem! I wasn't able to push to your branch, so I've created #7365 that contains your commit + some tests.

Regarding your TODO's:

but there are other places where floating-point arithmetic is performed that should probably be updated as well

Only ADD is supported/required for numerical values actually, so I think this is all that is necessary. From the docs:

If the existing attribute is a number, and if Value is also a number, then Value is mathematically added to the existing attribute. If Value is a negative number, then it is subtracted from the existing attribute.

The failing test was mypy complaining, because of a known issue where it can't find a suitable parent-type: python/mypy#3816.
As far as I can tell, the only way around this is to manually decide on a suitable type (Union[Decimal, int])

@bblommers bblommers closed this Feb 19, 2024
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 this pull request may close these issues.

DynamoDB's update_item performs floating-point arithmetic with mock table created via boto3
2 participants