From 0cf29f1459562ab1278e115da27c989c0bb9df67 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Wed, 13 Sep 2023 12:49:38 -0500 Subject: [PATCH] Add type checks on Intersection, Union, Complement (#2685) --- openmc/region.py | 10 ++++++++-- tests/unit_tests/test_region.py | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/openmc/region.py b/openmc/region.py index a2d3b1d0cb..1d54bff8da 100644 --- a/openmc/region.py +++ b/openmc/region.py @@ -4,7 +4,6 @@ from copy import deepcopy import numpy as np -from .checkvalue import check_type from .bounding_box import BoundingBox @@ -362,6 +361,9 @@ class Intersection(Region, MutableSequence): def __init__(self, nodes): self._nodes = list(nodes) + for node in nodes: + if not isinstance(node, Region): + raise ValueError('Intersection operands must be of type Region') def __and__(self, other): new = Intersection(self) @@ -450,6 +452,9 @@ class Union(Region, MutableSequence): def __init__(self, nodes): self._nodes = list(nodes) + for node in nodes: + if not isinstance(node, Region): + raise ValueError('Union operands must be of type Region') def __or__(self, other): new = Union(self) @@ -566,7 +571,8 @@ class Complement(Region): @node.setter def node(self, node): - check_type('node', node, Region) + if not isinstance(node, Region): + raise ValueError('Complement operand must be of type Region') self._node = node @property diff --git a/tests/unit_tests/test_region.py b/tests/unit_tests/test_region.py index 086ce64010..2e517c9652 100644 --- a/tests/unit_tests/test_region.py +++ b/tests/unit_tests/test_region.py @@ -222,3 +222,21 @@ def test_translate_inplace(): # Translating a region in-place should *not* produce new surfaces region3 = region.translate((0.5, -6.7, 3.9), inplace=True) assert str(region) == str(region3) + + +def test_invalid_operands(): + s = openmc.Sphere() + z = 3 + + # Intersection with invalid operand + with pytest.raises(ValueError, match='must be of type Region'): + -s & +z + + # Union with invalid operand + with pytest.raises(ValueError, match='must be of type Region'): + -s | +z + + # Complement with invalid operand + with pytest.raises(ValueError, match='must be of type Region'): + openmc.Complement(z) +