Skip to content

Reject NaN as a MockClock rate - #3562

Open
56steve wants to merge 3 commits into
python-trio:mainfrom
56steve:fix/mockclock-nan-rate
Open

56steve wants to merge 3 commits into
python-trio:mainfrom
56steve:fix/mockclock-nan-rate

Conversation

@56steve

@56steve 56steve commented Oct 27, 2026

Copy link
Copy Markdown

MockClock accepted a NaN rate because the check was new_rate < 0, which is false for NaN, so MockClock(rate=float('nan')).current_time() returned nan. I changed the check so NaN raises the same ValueError as a negative rate, and added tests for both the constructor and the setter.

@codecov

codecov Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00000%. Comparing base (354708e) to head (f1c7da7).

Additional details and impacted files
@@               Coverage Diff               @@
##                 main        #3562   +/-   ##
===============================================
  Coverage   100.00000%   100.00000%           
===============================================
  Files             128          128           
  Lines           19479        19484    +5     
  Branches         1323         1323           
===============================================
+ Hits            19479        19484    +5     
Files with missing lines Coverage Δ
src/trio/_core/_mock_clock.py 100.00000% <100.00000%> (ø)
src/trio/_core/_tests/test_mock_clock.py 100.00000% <100.00000%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@A5rocks A5rocks left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please don't respond with LLM-generated comments or I will close this PR!

Comment thread src/trio/_core/_mock_clock.py Outdated
@rate.setter
def rate(self, new_rate: float) -> None:
if new_rate < 0:
if not new_rate >= 0: # also rejects NaN

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please make this explicit. math.isnan exists, we don't have to do stupid transformations on math to check for NaN! Also a better error message is possible.

This branch has not been deployed

No deployments
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.

2 participants