Skip to content

Commit

Permalink
SuffixFinder: return default for out-of-scope components (not an exce…
Browse files Browse the repository at this point in the history
…ption)
  • Loading branch information
jsiirola committed Aug 14, 2024
1 parent f1c6ff1 commit c0eef17
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 28 deletions.
8 changes: 4 additions & 4 deletions pyomo/core/base/suffix.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,10 +473,10 @@ def find(self, component_data):
try:
suffixes = self._get_suffix_list(_block)
except AttributeError:
raise ValueError(
f"Component '{component_data.name}' not found in the SuffixFinder "
f"context (Block hierarchy rooted at {self._context.name})"
) from None
# Component was outside the context (eventually parent
# becomes None and parent.parent_block() raises an
# AttributeError): we will return the default value
return self.default
# Pass 1: look for the component_data, working root to leaf
for s in suffixes:
if component_data in s:
Expand Down
28 changes: 4 additions & 24 deletions pyomo/core/tests/unit/test_suffix.py
Original file line number Diff line number Diff line change
Expand Up @@ -1849,28 +1849,13 @@ def test_suffix_finder(self):
# Make sure we don't find default suffixes at lower levels
self.assertEqual(_suffix_finder.find(m.b1.v2), 1)
self.assertEqual(_suffix_b1_finder.find(m.b1.v2), None)
with self.assertRaisesRegex(
ValueError,
r"Component 'b1.v2' not found in the SuffixFinder context "
r"\(Block hierarchy rooted at b1.b2\)",
):
_suffix_b2_finder.find(m.b1.v2)
self.assertEqual(_suffix_b2_finder.find(m.b1.v2), None)

# Make sure we don't find specific suffixes at lower levels
m.b1.b2.suffix[m.v1] = 5
self.assertEqual(_suffix_finder.find(m.v1), 1)
with self.assertRaisesRegex(
ValueError,
r"Component 'v1' not found in the SuffixFinder context "
r"\(Block hierarchy rooted at b1\)",
):
_suffix_b1_finder.find(m.v1)
with self.assertRaisesRegex(
ValueError,
r"Component 'v1' not found in the SuffixFinder context "
r"\(Block hierarchy rooted at b1.b2\)",
):
_suffix_b2_finder.find(m.v1)
self.assertEqual(_suffix_b1_finder.find(m.v1), None)
self.assertEqual(_suffix_b2_finder.find(m.v1), None)

# Make sure we can look up Blocks and that they will match
# suffixes that they hold
Expand All @@ -1880,12 +1865,7 @@ def test_suffix_finder(self):

self.assertEqual(_suffix_finder.find(m.b1), 1)
self.assertEqual(_suffix_b1_finder.find(m.b1), None)
with self.assertRaisesRegex(
ValueError,
r"Component 'b1' not found in the SuffixFinder context "
r"\(Block hierarchy rooted at b1.b2\)",
):
_suffix_b2_finder.find(m.b1)
self.assertEqual(_suffix_b2_finder.find(m.b1), None)


if __name__ == "__main__":
Expand Down

0 comments on commit c0eef17

Please sign in to comment.