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

Make sure to use the default decimal context in our code #4231

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

antonpirker
Copy link
Member

Fixes #4213

@antonpirker antonpirker requested a review from a team as a code owner April 2, 2025 10:52
Copy link

codecov bot commented Apr 2, 2025

❌ 1 Tests Failed:

Tests completed Failed Passed Skipped
23809 1 23808 5877
View the top 1 failed test(s) by shortest run time
tests.integrations.grpc.test_grpc_aio test_grpc_server_abort
Stack Traces | 0.095s run time
.../integrations/grpc/test_grpc_aio.py:153: in test_grpc_server_abort
    assert len(events) == 1
E   assert 0 == 1
E    +  where 0 = len([])

To view more test analytics, go to the Test Analytics Dashboard
📋 Got 3 mins? Take this short survey to help us improve Test Analytics.

# to a float (True becomes 1.0 and False becomes 0.0)
self.sampled = self._sample_rand < self.sample_rate
# Now we roll the dice.
self.sampled = self._sample_rand < Decimal.from_float(self.sample_rate)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this? Just wondering if this can also potentially fail due to unforeseen circumstances 🙃

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when people set the FloatOperation trap (like the user in the linked issue did) then the old comparison fails, because then comparing Decimals to floats is not allowed anymore. I guess we also could add this around the comparison:

 with localcontext(DefaultContext) as ctx:
     ctx.prec = 6
     self.sampled = self._sample_rand < self.sample_rate

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really have a preference, but maybe wrapping stuff in a localcontext might be good, just to possibly avoid other unforeseen errors? I'm kinda afraid of the whole decimal API now so I'd make this as defensive as possible.

Copy link
Member

@szokeasaurusrex szokeasaurusrex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a small suggestion inline, but wanted to second what @sentrivana said about this whole Decimal API making me uneasy.

When I implemented sample_rand using Decimal, I thought it would make our lives easier by making it simple to ensure the correct precision for sample_rand.

However, now that multiple issues have come up related to using Decimal, I think it might have added more complexity than I realized, and perhaps we should consider revisiting the decision to use Decimal altogether (especially if another related issue comes up)

Decimal("0.000001"), rounding=ROUND_DOWN, context=Context(prec=6)
)
# in case the user has changed the default precision or set traps.
with localcontext(DefaultContext) as ctx:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small readability nit, I would import decimal then change this to

Suggested change
with localcontext(DefaultContext) as ctx:
with decimal.localcontext(DefaultContext) as ctx:

Otherwise, from just reading this code, it is unclear that the localcontext is a decimal-related thing

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.

Custom settings for decimal context broke the app
3 participants