Skip to content

Conversation

@guspix
Copy link

@guspix guspix commented Feb 15, 2025

From Python's Decimal module Quick-start Tutorial:

If the FloatOperation signal is trapped, accidental mixing of decimals and floats in constructors or ordering comparisons raises an exception

This is used to prevent loss of precision by using floats before converting to Decimal. This was happening in a couple of places in this library, which prevented me from using it in such an environment. The fix is pretty simple, presumably without changing any functionality for anyone.

@Magnie
Copy link

Magnie commented Mar 17, 2025

I was just about to make my own pull request for this.

I added the following to the top of qrcode/tests/test_qrcode.py:

import decimal
decimal.getcontext().traps[decimal.FloatOperation] = True

This would prevent future commits from reintroducing the issue.

@bartTC
Copy link
Member

bartTC commented Jul 23, 2025

Very good catch, thank you!

There is a ruff rule that takes care of it. https://docs.astral.sh/ruff/rules/decimal-from-float-literal/
#408 includes your chnages, and the rule will prevent future occurences.

@bartTC bartTC closed this Jul 23, 2025
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.

3 participants