Skip to content

Commit b12fc94

Browse files
committed
[Python/Reactor] Check for attempts to assign unknown delegates
1 parent 4c3a8cc commit b12fc94

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

interfaces/cython/cantera/delegator.pyx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,9 @@ cdef int assign_delegates(obj, CxxDelegator* delegator) except -1:
274274
cdef string cxx_name
275275
cdef string cxx_when
276276
obj._delegates = []
277+
valid_names = set()
277278
for name, options in obj.delegatable_methods.items():
279+
valid_names.add(name)
278280
if len(options) == 3:
279281
# Delegate with pre-selected mode, without using prefix on method name
280282
when = options[2]
@@ -375,6 +377,14 @@ cdef int assign_delegates(obj, CxxDelegator* delegator) except -1:
375377
# collector
376378
obj._delegates.append(method)
377379

380+
# Check for methods on the base object that have no matching delegate
381+
for name in dir(obj):
382+
if (name.startswith("before_") or name.startswith("after_") or
383+
name.startswith("replace_")):
384+
base_name = name.split("_", 1)[1]
385+
if base_name not in valid_names:
386+
raise ValueError(f"'{base_name}' is not a delegatable method")
387+
378388
return 0
379389

380390

test/python/test_reactor.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3108,6 +3108,13 @@ def replace_component_index(self, name):
31083108
r2.component_index("H2")
31093109
assert r2.component_index("succeed") == 0
31103110

3111+
class DummyReactor3(ct.ExtensibleReactor):
3112+
def before_foobar(self, t):
3113+
pass
3114+
3115+
with pytest.raises(ValueError, match="'foobar' is not a delegatable method"):
3116+
DummyReactor3(self.gas)
3117+
31113118
def test_delegate_throws(self):
31123119
class TestException(Exception):
31133120
pass

0 commit comments

Comments
 (0)