fix: make q_learning choose_action doctest deterministic - #15459
Closed
ZainnQureshii wants to merge 1 commit into
Closed
ZainnQureshii wants to merge 1 commit into
ZainnQureshii wants to merge 1 commit into
Conversation
The doctest assigned EPSILON = 0.0 in doctest's copy of the module globals, so choose_action() still read the module's EPSILON = 0.2 and explored at random about one run in ten. Patch random.random instead so the greedy branch is always taken. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Member
|
Clearing all open pull requests to prepare for Hacktoberfest 2026 -- https://hacktoberfest.com |
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.
Describe your change
The
choose_actiondoctest inmachine_learning/q_learning.pyis flaky. It setsEPSILON = 0.0inside the doctest, but doctest runs examples in a copy of the module globals, sochoose_action()still reads the module'sEPSILON = 0.2and takes the random-exploration branch about one run in ten, printing2instead of1.Reproduced on master: repeated
python3 -m doctest machine_learning/q_learning.pyfailed within the first 5 runs (Expected: 1,Got: 2). CI runs pytest with--iterations=8, so this hits PRs at random.The fix patches
random.randominside the doctest (return_value=0.99, so0.99 < EPSILONis false and the greedy branch is always taken), the sameunittest.mock.patchpattern used inother/fischer_yates_shuffle.py. Doctest-only change; the function body is untouched.Verified: 200 consecutive
python3 -m doctest machine_learning/q_learning.pyruns pass,uvx pytest --doctest-modules machine_learning/q_learning.pypasses,uvx ruff checkandruff format --checkare clean.Checklist
AI disclosure: prepared with AI assistance (Claude Code) and reviewed with GitHub Copilot, so I left the "all my own work" box unticked; nothing is copied from elsewhere.
🤖 Generated with Claude Code