diff --git a/doc/source/changelog.rst b/doc/source/changelog.rst index 8f317767..c943806b 100644 --- a/doc/source/changelog.rst +++ b/doc/source/changelog.rst @@ -1,6 +1,14 @@ MontePy Changelog ================= +#Next Version# +-------------- + +**Bug fixes** + +* Fixed bug with appending and renumbeing numbered objects from other MCNP problems (:issue:`466`). + + 0.3.2 -------------- diff --git a/montepy/numbered_object_collection.py b/montepy/numbered_object_collection.py index 253caceb..9656b2d9 100644 --- a/montepy/numbered_object_collection.py +++ b/montepy/numbered_object_collection.py @@ -230,6 +230,8 @@ def append_renumber(self, obj, step=1): if not isinstance(step, int): raise TypeError("The step number must be an int") number = obj.number + if self._problem: + obj.link_to_problem(self._problem) try: self.append(obj) except NumberConflictError: @@ -237,8 +239,6 @@ def append_renumber(self, obj, step=1): obj.number = number self.append(obj) - if self._problem: - obj.link_to_problem(self._problem) return number def request_number(self, start_num=1, step=1): diff --git a/tests/test_numbered_collection.py b/tests/test_numbered_collection.py index 623d6cc8..4b494ef7 100644 --- a/tests/test_numbered_collection.py +++ b/tests/test_numbered_collection.py @@ -115,6 +115,18 @@ def test_append_renumber(self): self.assertEqual(cell.number, 4) self.assertEqual(len(cells), size + 2) + def test_append_renumber_problems(self): + prob1 = copy.deepcopy(self.simple_problem) + prob2 = copy.deepcopy(self.simple_problem) + # Delete Material 2, making its number available. + prob2.materials.remove(prob2.materials[2]) + len_mats = len(prob2.materials) + mat1 = prob1.materials[1] + new_num = prob2.materials.append_renumber(mat1) + assert new_num == 2, "Material not renumbered correctly." + assert len(prob2.materials) == len_mats + 1, "Material not appended" + assert prob2.materials[2] is mat1, "Material 2 is not the new material" + def test_request_number(self): cells = self.simple_problem.cells self.assertEqual(cells.request_number(6), 6)