Add type checks on Intersection, Union, Complement (#2685)

This commit is contained in:
Paul Romano 2023-09-13 12:49:38 -05:00 committed by GitHub
parent 1c82dfcc1c
commit 0cf29f1459
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 2 deletions

View file

@ -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

View file

@ -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)