Skip to content

BUG: Return -1 from best_response_2p instead of falling through to None - #936

Merged
mmcky merged 4 commits into
mainfrom
fix/best-response-2p-fallthrough
Aug 16, 2026
Merged

BUG: Return -1 from best_response_2p instead of falling through to None#936
mmcky merged 4 commits into
mainfrom
fix/best-response-2p-fallthrough

Conversation

@mmcky

@mmcky mmcky commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

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_2p documents an int return, but when tol < 0 no action satisfies the tolerance condition and the final loop falls through to an implicit None. Per @oyamad's suggestion in the #576 review discussion, the function now returns -1 in that case and the docstring documents the behavior (and corrects the stated default for tol, which is 1e-8, not None).

This is hardening rather than a live bug: no internal caller passes a negative tol. A regression test covering the tol < 0 case is included.

🤖 Generated with Claude Code

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>
Copilot AI lite review requested due to automatic review settings August 14, 2026 01:00

Copilot AI 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.

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_2p to return -1 if no action satisfies the tolerance condition (previously could fall through to None).
  • Update the function docstring to document the sentinel return and correct the stated default for tol.
  • Add a regression test covering the tol < 0 case.

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., tol being 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.

Comment thread quantecon/game_theory/normal_form_game.py
@coveralls

coveralls commented Aug 14, 2026

Copy link
Copy Markdown

Coverage Status

coverage: 90.571% (+0.001%) from 90.57% — fix/best-response-2p-fallthrough into main

mmcky and others added 3 commits August 14, 2026 11:07
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>
@mmcky

mmcky commented Aug 14, 2026

Copy link
Copy Markdown
Contributor Author

Summary of this PR's final state, for reviewers:

The fix (623e5e0): best_response_2p previously fell through to an implicit None when tol < 0, despite documenting an int return. It now returns -1, per the resolution originally agreed with @oyamad in the #576 review discussion. A regression test covers the tol < 0 case. This is hardening rather than a live bug — the function has no internal callers (it is exported public API only), and no meaningful input reaches the sentinel.

Docstring (9e9a6c7, 7556d02, 898bfb8): after iterating on Copilot's review comment, the final wording states the precondition in the parameter docs (tol must be nonnegative) and frames -1 explicitly as an error condition in the Returns section — the numba-friendly equivalent of a Raises entry, since jitted code avoids exceptions. The two statements are complementary: precondition where the parameter is defined, failure mode where the return is defined.

This extraction lets the fall-through fix land independently of the type-annotation policy decision in #897 that gates #576.

🤖 Generated with Claude Code

@mmcky
mmcky requested a review from oyamad August 14, 2026 01:12
@mmcky mmcky added the review label Aug 14, 2026

@oyamad oyamad left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@mmcky Thanks!

@oyamad oyamad added the ready label Aug 15, 2026
@mmcky
mmcky merged commit 5758d98 into main Aug 16, 2026
13 checks passed
@mmcky
mmcky deleted the fix/best-response-2p-fallthrough branch August 16, 2026 02:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants