Skip to content

Check that public Python functions do not modify inputs (fix #8) - #103

Open
M-Colley wants to merge 3 commits into
multi-objective:mainfrom
M-Colley:fix/issues-8-11
Open

M-Colley wants to merge 3 commits into
multi-objective:mainfrom
M-Colley:fix/issues-8-11

Conversation

@M-Colley

@M-Colley M-Colley commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Closes #8

The allocation checks for #11 that were part of this PR are now in #108.

Changes

python/tests/test_immutability.py now calls every public function that takes arrays through immutable_call(). Functions with a maximise argument run with maximise=False, True and a mixed boolean array, since the maximisation paths are where an input copy is most often missing. whv_rect, total_whv_rect and largest_eafdiff only support minimisation, so they run once. Functions without array inputs (generate_ndset, generate_sequence, get_dataset, get_dataset_path, read_datasets) are not covered.

The new test found a bug. whv_rect() and total_whv_rect() modified both points and rectangles. The C function rect_weighted_hv2d() sorted the points in place, and whv_preprocess_rectangles() clipped and sorted the rectangles in place. The Python wrapper passes the NumPy buffer directly when the input is a C-contiguous float64 array, so the caller's arrays were reordered and clipped. The R wrapper passes t(x) and t(rectangles), which are copies, so R was not affected.

Fix: rect_weighted_hv2d() now takes const inputs and sorts a copy of the points. whv_preprocess_rectangles() writes the clipped rectangles into a new array, which also removes the skip array.

Overlap with #108: both PRs change whv_preprocess_rectangles() in c/whv.c and the NEWS files c/NEWS.md and python/doc/source/whatsnew/index.rst, so whichever PR merges second needs a rebase that keeps the rewritten whv_preprocess_rectangles() from this PR.

How I verified it

  • Before the C fix, the new test fails with Function 'whv_rect' mutated positional arg 0 and the same for total_whv_rect. All other functions pass. After the fix, test_immutability.py passes (98 passed, 1 existing xfail).
  • Full Python suite (pytest --doctest-modules tests src/moocore/_moocore.py): 195 passed, 2 xfailed. The extension was built with MSVC.
  • rect_weighted_hv2d() is also used by R whv_rect(). R testthat with NOT_CRAN=true: 0 failures, 0 errors, 0 skipped.
  • c/whv.c compiles without warnings with GCC 14.3 and -Wall -Wextra -Wpedantic -Werror, with DEBUG=0 and DEBUG=1.

Environment: Windows 11, Python 3.13, R 4.6.1, GCC 14.3 from Rtools 4.5, MSVC from Visual Studio 18. I have not tested on Linux or macOS locally, so CI covers those.

AI assistance was used to prepare and review this patch.

🤖 Generated with Claude Code

@MLopez-Ibanez

This comment was marked as outdated.

@M-Colley M-Colley changed the title Check that Python functions keep inputs unchanged (fix #8) and check all allocations (fix #11) Check that public Python functions do not modify inputs (fix #8) Nov 24, 2026
@codecov

codecov Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.52542% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.72%. Comparing base (fa8bdfd) to head (bc2fe53).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #103      +/-   ##
==========================================
+ Coverage   75.93%   76.72%   +0.78%     
==========================================
  Files          70       70              
  Lines        7429     7453      +24     
  Branches     1476     1469       -7     
==========================================
+ Hits         5641     5718      +77     
+ Misses       1257     1224      -33     
+ Partials      531      511      -20     
Flag Coverage Δ
R 82.02% <ø> (ø)
python 76.04% <91.52%> (+0.89%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
C 71.25% <77.27%> (+0.44%) ⬆️
R 82.02% <ø> (ø)
Python tests 100.00% <100.00%> (ø)
Python src 84.50% <ø> (+2.71%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions

Copy link
Copy Markdown

📚 R docs preview for this PR

@github-actions

Copy link
Copy Markdown

📚 Python docs preview for this PR

@MLopez-Ibanez

Copy link
Copy Markdown
Contributor

Could you please separate the two fixes from the new tests to separate commits? They can stay in the same PR but it will be easier to bisect a bug if not everything is in the same commit.

@M-Colley

M-Colley commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

@MLopez-Ibanez Thanks for the review. I rebased the branch onto main and resolved the conflicts in c/NEWS.md and python/doc/source/whatsnew/index.rst. I also split the changes into three commits:

  1. c/whv.c (rect_weighted_hv2d), c/whv.h, and python/src/moocore/libmoocore.h: sort a copy of the input points array and mark the input pointer const.
  2. c/whv.c (whv_preprocess_rectangles, rect_weighted_hv2d), c/whv.h, python/src/moocore/libmoocore.h, c/NEWS.md, and python/doc/source/whatsnew/index.rst: clip rectangles into a newly allocated array and mark the input pointer const.
  3. python/tests/test_immutability.py: add immutability tests covering the public array-taking functions.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

How to test / annotate / decorate Python function arguments as read-only, specially arrays?

2 participants