Skip to content

When nothing matches

When no registered overload accepts a call, dispatch raises NoMethodError. Because NoMethodError is also a TypeError, code that already catches TypeError keeps working without modification. The error carries the function's name and the overloads that came closest to matching:

>>> from bagof.dispatchers import dispatch, NoMethodError, DispatchError
>>> @dispatch
... def area(width: int, height: int) -> int:
...     return width * height
>>> try:
...     area("triangle")
... except NoMethodError as error:
...     print(error.function, isinstance(error, DispatchError), isinstance(error, TypeError))
area True True

NoMethodError is also a DispatchError, the common base it shares with AmbiguousMethodError, so code that wants to catch either failure without distinguishing between them can catch DispatchError instead.