New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix graph crash with partial conditions #239
Fix graph crash with partial conditions #239
Conversation
Graph.rep() assumes that every callable has a `__name__` attribute. However, callables like `functools.partial` and other classes with a `__call__()` method have no `__name__`. This patch modifies `Graph.rep()` to first check whether the condition is a string and return it, then try to return `f.__name__` (that is the current behavior) and if that fails, then it calls `str()` on the argument. `str()` should never fail and it is now the responsibility of the user to return something meaningful from `__str__()`.
If you like, I can explicitly handle
|
Partials for condition checks haven't been tested anywhere else yet. If you call # [...]
m = self.machine_cls(states=['A', 'B', 'C'], initial= 'A', show_conditions=True,
title='a test')
m.add_state({'name': 'E'})
m.add_transition(trigger='fly', source='A', dest='B',
conditions=Check(False))
m.add_transition(trigger='fly', source='A', dest='C',
unless=functools.partial(check, False))
m.fly()
self.assertTrue(m.is_C())
# [...] |
I could test partial conditions in |
Sounds good. Currently, the 'coverage problem' is, that |
As discussed in pytransitions#239: - `rep()` is now a free function - `rep()` pretty-prints partial functions - `rep()` calls `str()` on its argument when everything else fails Also, the following cases are now tested: - `rep()` with a string argument returns the string - `rep()` with a function returns the name of the function - `rep()` with a partial function returns name + args + kwargs - `rep()` with anything else calls `str()`
Tests passed on my machine with 2.7.13, I'll try again... |
The CI build has shown that `partial().keyword` may be None in Python 2.7--3.4.
Looks good to me, one function name was misspelled, thats why the test hadnt been executed. |
Graph.rep() assumes that every callable has a
__name__
attribute.However, callables like
functools.partial
and other classes with a__call__()
method have no__name__
. This patch modifiesGraph.rep()
to first check whether the condition is a string andreturn it, then try to return
f.__name__
(that is the currentbehavior) and if that fails, then it calls
str()
on the argument.str()
should never fail and it is now the responsibility of the userto return something meaningful from
__str__()
.