python-statemachine icon indicating copy to clipboard operation
python-statemachine copied to clipboard

Transition functions that return dictionaries result in an exception

Open ndaniell-srl opened this issue 4 years ago • 0 comments

  • Python State Machine version: 0.8.0
  • Python version: 3.9.6
  • Operating System: Ubuntu 20.04

Description

Ran into an issue where transition functions that returned a dictionary was causing an exception inside the statemachine.py transition class, in the method _get_destination_from_result. The issue is that the method inspects the return value to determine if there is any state classes returned to assign the destination class. However in the case where a dictionary is returned with 2 or more keys the len() test on the result returns true, but the object is not iterable. So when the following occurs:

if isinstance(result[-1], State): result, destination = result[:-1], result[-1] if len(result) == 1: result = result[0]

The -1 index in results raises an exception.

What I Did

To deal with the issue I've simple wrapped all transistion functions that return dictionary results with a method that does the following to the dictionary:

ret_val[-1] = None return ret_val

This creates a -1 key in the dictionary to avoid the exception.

It would probably be more advantageous if the library verified the return value was iterable before attempting to index into it.

ndaniell-srl avatar Aug 02 '21 21:08 ndaniell-srl