Remove Guardrail from NumberedObjectCollection#915
Remove Guardrail from NumberedObjectCollection#915digvijay-y wants to merge 12 commits intoidaholab:developfrom
Conversation
When an MCNP object is created from scratch without a number assigned (obj.number is None), append_renumber() would raise: TypeError: '>' not supported between instances of 'NoneType' and 'int' because the guard 'obj.number > 0' does not handle None. Fix: treat None the same as an invalid (<=0) number — start renumber search from 1, assign it to the object so __internal_append can proceed, then let the existing conflict-resolution logic find the actual next available number via request_number() if needed. Also adds a regression test that constructs a bare montepy.Cell() (number == None) and verifies append_renumber assigns a valid positive number and adds the cell to the collection.
MicahGale's review noted that line 488 (the obj.number < 0 ValueError guard in __internal_append) was no longer covered after the fix, because append_renumber now pre-assigns a positive number before calling append(). Add an explicit test that bypasses the property-level validator via _number.value to hit the internal guard directly. 97/97 tests pass.
- Remove verbose comment block explaining obj.number pre-assignment - Collapse 'number = 1; obj.number = number' to 'obj.number = 1' - Add 'assert new_cell in cells' to test per MicahGale's suggestion
There was a problem hiding this comment.
Pull request overview
This PR removes the “guardrail” fallback behavior in NumberedObjectCollection by relying on the internal number cache for lookups/conflict checks, and updates related logic/tests to match. It also fixes append_renumber() to handle objects created without an assigned number (number is None), and updates documentation to reflect the changes.
Changes:
- Remove the O(N) fallback scan/recache behavior from
NumberedObjectCollection.get()and simplifycheck_number()to rely on__num_cache. - Fix
append_renumber()to support objects whosenumberisNone(regression coverage added). - Simplify
verify_clone_formattest to avoid redundant/fragile temporary surface number mutation.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
montepy/numbered_object_collection.py |
Removes guardrail fallback logic from get() and updates check_number()/append_renumber() behavior. |
tests/test_numbered_collection.py |
Adds regression coverage for append_renumber() with number is None and covers an internal append guard. |
tests/test_cell_problem.py |
Updates clone formatting verification to avoid unnecessary surface renumbering. |
doc/source/changelog.rst |
Adds changelog entries for issue #880 and #895. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
MicahGale
left a comment
There was a problem hiding this comment.
Some code simplification, and I want to think more about the test changes.
Side note: I am trying out having Copilot do a first review. Does it seem helpful to you @digvijay-y?
| surf = list(cell.surfaces)[0] | ||
| old_num = surf.number | ||
| num = 1000 | ||
| surf.number = num |
There was a problem hiding this comment.
I'll have to think if this is the best way to update the test.
It helps. But let's test it with large changes. |
I think it would also be a lot more helpful if I gave it instructions from the contributing guide, and developer documentation, etc. |
Co-authored-by: Micah Gale <mgale@fastmail.com>
Pull Request Checklist for MontePy
Description
Removed Guardrail from
NumberedObjectCollectionSee this guardrail that is no longer covered because of this.
check_number()inNumberObjectCollectionto follow changesThe problem is that test intent is already covered by the fact that
clone_region=Trueclones the surface and assigns it a brand new number anyway. So by the timeverify_clone_formatis called, surf.number is already a freshly assigned number, not 2 anymore. The temporary mutation to 1000 was redundant and just added fragility.verify_clone_formattest intest_cell_problemFixes #895
General Checklist
blackversion 25 or 26.LLM Disclosure
Are you?
Were any large language models (LLM or "AI") used in to generate any of this code?
Documentation Checklist
First-Time Contributor Checklist
pyproject.tomlif you wish to do so.Additional Notes for Reviewers
Ensure that:
📚 Documentation preview 📚: https://montepy--915.org.readthedocs.build/en/915/