BUG: Return -1 from best_response_2p instead of falling through to None - #936
Conversation
When tol < 0, no action satisfies the tolerance condition and the function fell through to an implicit None despite documenting an int return. Return -1 in that case, as suggested by oyamad in the review discussion on #576, and document the behavior. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR hardens best_response_2p (a numba-jitted helper in quantecon.game_theory.normal_form_game) so it always returns an int, addressing the documented-but-previously-implicit fallthrough to None when tol < 0.
Changes:
- Update
best_response_2pto return-1if no action satisfies the tolerance condition (previously could fall through toNone). - Update the function docstring to document the sentinel return and correct the stated default for
tol. - Add a regression test covering the
tol < 0case.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
quantecon/game_theory/normal_form_game.py |
Ensures best_response_2p returns an int in all paths and updates docs for the new behavior. |
quantecon/game_theory/tests/test_normal_form_game.py |
Adds a regression test to lock in the tol < 0 sentinel return behavior. |
Suppressed comments (1)
quantecon/game_theory/normal_form_game.py:938
- The inline comment says this return is "Unreachable unless tol < 0", but the code can fall through for other inputs (e.g.,
tolbeing NaN makes all>=comparisons false). The comment should describe the actual condition for reaching this line rather than asserting it is unreachable.
return -1 # Unreachable unless tol < 0
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
The Returns section already documents the -1 sentinel for tol < 0, so the input restriction read as contradictory. Addresses Copilot review feedback on #936. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
With -1 now explicitly framed as an error condition in the Returns section, stating the valid domain in the parameter docs is complementary rather than contradictory: the parameter doc gives the precondition, the Returns doc gives the failure mode. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
Summary of this PR's final state, for reviewers: The fix (623e5e0): Docstring (9e9a6c7, 7556d02, 898bfb8): after iterating on Copilot's review comment, the final wording states the precondition in the parameter docs ( This extraction lets the fall-through fix land independently of the type-annotation policy decision in #897 that gates #576. 🤖 Generated with Claude Code |
Extracts the one standalone fix from the long-open PR #576 (see discussion there), so it can land independently of the type-annotation policy decision in #897.
best_response_2pdocuments anintreturn, but whentol < 0no action satisfies the tolerance condition and the final loop falls through to an implicitNone. Per @oyamad's suggestion in the #576 review discussion, the function now returns-1in that case and the docstring documents the behavior (and corrects the stated default fortol, which is1e-8, notNone).This is hardening rather than a live bug: no internal caller passes a negative
tol. A regression test covering thetol < 0case is included.🤖 Generated with Claude Code