fix(sparse): floor() returns a copy instead of mutating its input (#740)#894
Open
SaguaroDev wants to merge 1 commit into
Open
fix(sparse): floor() returns a copy instead of mutating its input (#740)#894SaguaroDev wants to merge 1 commit into
SaguaroDev wants to merge 1 commit into
Conversation
…sact#740) chainladder.utils.sparse.floor(x) reassigned x.data in place, mutating the caller's COO array. np.floor() returns a copy, so floor() should too. Copy the array before flooring its data, mirroring the existing nan_to_num() idiom in the same module. The one internal caller (core/correlation.py) already uses the return value, so behavior is preserved. Updated the test that asserted in-place mutation to assert the new copy semantics: the result is a new object and the input is left unchanged. Closes casact#740.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #894 +/- ##
=======================================
Coverage 86.90% 86.90%
=======================================
Files 87 87
Lines 4932 4933 +1
Branches 624 624
=======================================
+ Hits 4286 4287 +1
Misses 456 456
Partials 190 190
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Collaborator
|
since we are already touching this function, may I please trouble you to add type hinting for the input and output? |
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.
Closes #740.
chainladder.utils.sparse.floor(x)reassignedx.datain place, mutating the caller's COO array as a side effect:Since
np.floor()returns a copy, this makesfloor()match that contract by copying before flooring, using the same.copy()idiom already present innan_to_num()in the same module:Behavior now matches the issue's expectation:
The only internal caller (
chainladder/core/correlation.py) already consumes the return value (m = xp.floor((n - 1) / 2)), so the change is behavior-preserving there.Updated
test_floor_mutates_in_place(which assertedresult is a) totest_floor_returns_copy, asserting the result is a new object and the input is left unmodified.test_sparse.pyandtest_correlation.pypass (12 passed).Note
Low Risk
Small API-contract fix in a utility helper with a single internal caller that already uses the return value; tests cover the new behavior.
Overview
chainladder.utils.sparse.floorno longer mutates the input COO array: it copies first (same pattern asnan_to_num), then floorsx.data, aligning withnp.floor’s non-mutating contract (issue #740).Tests were renamed/updated from asserting in-place mutation (
result is a) to asserting a new object and an unchanged input.correlation.pyalready uses the return value only, so call sites are unchanged in practice.Reviewed by Cursor Bugbot for commit 55c55e8. Bugbot is set up for automated code reviews on this repo. Configure here.