Introduce explicit ranked method base - #60
Conversation
Co-authored-by: Cursor <cursoragent@cursor.com>
Reviewer's GuideIntroduce an explicit RankedMethod base class that centralizes ranked-ballot construction and strategy logic, updates Borda, Plurality, IRNR, and Schulze to inherit from it, preserves public imports for RankedMethod/RatedMethod, and adds regression tests to validate inheritance, ballot behavior, strategy, and plurality results. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- In
RankedMethod.fillPrefOrder, theif whichCands:truthiness check means an empty iterable will be treated the same asNoneand ignore the filter; consider explicitly checkingwhichCands is not Noneto distinguish "no filter" from "empty filter". - In
RankedMethod.fillCands,iis only defined inside theforloop; ifnSlotsis 0 butremainderScoreis notNone, thei += 1path will raise an error—initialize a separate index (e.g.,idx = nSlots) for the remainder loop instead of reusingi.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `RankedMethod.fillPrefOrder`, the `if whichCands:` truthiness check means an empty iterable will be treated the same as `None` and ignore the filter; consider explicitly checking `whichCands is not None` to distinguish "no filter" from "empty filter".
- In `RankedMethod.fillCands`, `i` is only defined inside the `for` loop; if `nSlots` is 0 but `remainderScore` is not `None`, the `i += 1` path will raise an error—initialize a separate index (e.g., `idx = nSlots`) for the remainder loop instead of reusing `i`.
## Individual Comments
### Comment 1
<location path="src/vse_sim/methods/ranked.py" line_range="39-47" />
<code_context>
- nSlots=None, #again, None means "all"
- remainderScore=None #what to give candidates that don't fit in nSlots
- ):
- if nSlots is None:
- nSlots = len(whichCands)
- cur = lowSlot + nSlots - 1
- for i in range(nSlots):
- ballot[whichCands[i][0]] = cur
- cur -= 1
- if remainderScore is not None:
- i += 1
- while i < len(whichCands):
- ballot[whichCands[i][0]] = remainderScore
- i += 1
</code_context>
<issue_to_address>
**issue (bug_risk):** Avoid using `i` after the loop when `nSlots` can be zero to prevent undefined-variable behavior.
When `nSlots` ends up as 0 (e.g., `whichCands` empty and `nSlots` left as `None`) and `remainderScore` is set, the loop never runs, so `i` is never initialized and `i += 1` will raise an error. This mirrors legacy behavior but is still a latent bug. Consider guarding this block with `if remainderScore is not None and nSlots > 0:` or rewriting to avoid using `i` after the loop, such as iterating `whichCands[nSlots:]` directly.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Co-authored-by: Cursor <cursoragent@cursor.com>
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (7)
📝 WalkthroughWalkthroughThe change extracts shared ranked-ballot behavior into a new ChangesRanked method hierarchy
Estimated code review effort: 3 (Moderate) | ~20 minutes ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
RankedMethod = Bordaalias with an explicit ranked-ballot base classRankedMethodandRatedMethodpackage importsStack
Depends on #59, which depends on #58. Retarget this PR to
mainafter its parent merges.Validation
uv run python -m pytest(44 passed)trunk checkSummary by Sourcery
Introduce a dedicated ranked-ballot base class and update ranked methods to use it while preserving public imports and behaviour.
New Features:
Enhancements:
Tests:
Summary by CodeRabbit
New Features
Bug Fixes
Tests