Skip to content

Remove Guardrail from NumberedObjectCollection#915

Open
digvijay-y wants to merge 12 commits intoidaholab:developfrom
digvijay-y:guardrail
Open

Remove Guardrail from NumberedObjectCollection#915
digvijay-y wants to merge 12 commits intoidaholab:developfrom
digvijay-y:guardrail

Conversation

@digvijay-y
Copy link
Collaborator

@digvijay-y digvijay-y commented Feb 28, 2026

Pull Request Checklist for MontePy

Description

Removed Guardrail from NumberedObjectCollection
See this guardrail that is no longer covered because of this.

  • Updated check_number() in NumberObjectCollection to follow changes

The problem is that test intent is already covered by the fact that clone_region=True clones the surface and assigns it a brand new number anyway. So by the time verify_clone_format is called, surf.number is already a freshly assigned number, not 2 anymore. The temporary mutation to 1000 was redundant and just added fragility.

  • Updated verify_clone_format test in test_cell_problem

Fixes #895


General Checklist

  • I have performed a self-review of my own code.
  • The code follows the standards outlined in the development documentation.
  • I have formatted my code with black version 25 or 26.
  • I have added tests that prove my fix is effective or that my feature works (if applicable).

LLM Disclosure

  1. Are you?

    • A human user
    • A large language model (LLM), including ones acting on behalf of a human
  2. Were any large language models (LLM or "AI") used in to generate any of this code?

  • Yes
    • Model(s) used:
  • No

Documentation Checklist

  • I have documented all added classes and methods.
  • For infrastructure updates, I have updated the developer's guide.
  • For significant new features, I have added a section to the getting started guide.

First-Time Contributor Checklist

  • If this is your first contribution, add yourself to pyproject.toml if you wish to do so.

Additional Notes for Reviewers

Ensure that:

  • This PR fully addresses and resolves the referenced issue(s).
  • The submitted code is consistent with the merge checklist outlined here.
  • The PR covers all relevant aspects according to the development guidelines.
  • 100% coverage of the patch is achieved, or justification for a variance is given.

📚 Documentation preview 📚: https://montepy--915.org.readthedocs.build/en/915/

digvijay-y and others added 6 commits February 28, 2026 06:39
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
@digvijay-y digvijay-y added the performance 🐌 Issues related to speed and memory label Feb 28, 2026
Copy link

Copilot AI left a comment

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 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 simplify check_number() to rely on __num_cache.
  • Fix append_renumber() to support objects whose number is None (regression coverage added).
  • Simplify verify_clone_format test 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.

digvijay-y and others added 3 commits February 28, 2026 12:36
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Collaborator

@MicahGale MicahGale left a comment

Choose a reason for hiding this comment

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

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?

Comment on lines 277 to -280
surf = list(cell.surfaces)[0]
old_num = surf.number
num = 1000
surf.number = num
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'll have to think if this is the best way to update the test.

@digvijay-y
Copy link
Collaborator Author

I am trying out having Copilot do a first review. Does it seem helpful to you @digvijay-y?

It helps. But let's test it with large changes.

@MicahGale MicahGale added the code improvement A feature request that will improve the software and its maintainability, but be invisible to users. label Mar 2, 2026
@MicahGale
Copy link
Collaborator

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.

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

Labels

code improvement A feature request that will improve the software and its maintainability, but be invisible to users. performance 🐌 Issues related to speed and memory

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remove Guardrails from NumberedObjectCollection

3 participants