Skip to content

Added support for environments where the FloatOperation signal is trapped in the Decimal library#391

Closed
guspix wants to merge 1 commit into
lincolnloop:mainfrom
guspix:support-decimal-floatoperation-trap
Closed

Added support for environments where the FloatOperation signal is trapped in the Decimal library#391
guspix wants to merge 1 commit into
lincolnloop:mainfrom
guspix:support-decimal-floatoperation-trap

Conversation

@guspix

@guspix guspix commented Feb 15, 2025

Copy link
Copy Markdown

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

Magnie commented Mar 17, 2025

Copy link
Copy Markdown

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

bartTC commented Jul 23, 2025

Copy link
Copy Markdown
Member

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