Return None from next_available_time when key has never been used#359
Merged
Conversation
next_available_time returned datetime.datetime.min as a sentinel for "never used", but the return type was datetime.datetime with no documentation. Callers could not distinguish "never used" from a real past timestamp, and mixing the sentinel with timezone-aware datetimes raised TypeError. Change the return type to datetime.datetime | None and return None for keys with no usage history. Update wait_time_for to treat None as zero wait. Update the Protocol declaration and the existing test accordingly.
22237b6 to
c23e1c4
Compare
…n-as-sentinel-001 # Conflicts: # tests/test_simple.py # throttle_controller/simple.py
Code Metrics Report
Details | | main (02cd315) | #359 (364e152) | +/- |
|---------------------|----------------|----------------|------|
| Coverage | 100.0% | 100.0% | 0.0% |
| Files | 5 | 5 | 0 |
| Lines | 120 | 125 | +5 |
+ | Covered | 120 | 125 | +5 |
+ | Code to Test Ratio | 1:0.0 | 1:0.0 | +0.0 |
| Code | 319523 | 319531 | +8 |
+ | Test | 357 | 360 | +3 |
| Test Execution Time | 1s | 1s | 0s |Code coverage of files in pull request scope (100.0% → 100.0%)
Reported by octocov |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
next_available_timereturneddatetime.datetime.minas an implicit sentinelfor "this key has never been used." The return type was
datetime.datetimewithno documentation, so callers could not distinguish a "never used" key from a key
that was genuinely last used near the epoch. Mixing the sentinel with a
timezone-aware datetime also raised
TypeError.Changes
throttle_controller/simple.py/throttle_controller/protocol.py:Change the return type of
next_available_timefromdatetime.datetimetodatetime.datetime | None. ReturnNonewhen the key has never been used.throttle_controller/simple.py: Updatewait_time_forto treatNoneaszero wait (i.e. the key is immediately available).
tests/test_simple.py: Updatetest_next_available_timeto assertis Nonefor an unused key and to check the non-None path with a type-safe assertion.
Validation
uv run poe format— reformatteduv run poe check— all checks pass (ruff + mypy)uv run pytest— all tests pass(pre-existing
test_throttlingtiming flakiness is unrelated to this change)Trade-offs
This is a breaking change for callers that compare
next_available_time(key)against
datetime.datetime.minor treat the return value as always non-None.Type checkers will surface these sites as errors. The default
wait_if_neededpath is unaffected because it already guards on
_has_ever_used.