-
Notifications
You must be signed in to change notification settings - Fork 12
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
Make @transition
function-based decorator a class-based decorator
#34
Conversation
Codecov Report
@@ Coverage Diff @@
## main #34 +/- ##
==========================================
+ Coverage 97.40% 97.43% +0.03%
==========================================
Files 9 9
Lines 231 234 +3
==========================================
+ Hits 225 228 +3
Misses 6 6
Continue to review full report at Codecov.
|
for item in source: | ||
if not isinstance(item, allowed_types): | ||
raise ValueError("Source can be a bool, int, string, Enum, or list") | ||
class transition: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lowercase class name allows us to keep the interface the same
|
||
return _wrapper | ||
else: | ||
def __init__(self, source, target, conditions=None, on_error=None): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This way of doing things allows us to process decorator parameters inside of the __init__
function. This feels a bit cleaner than before.
raise ValueError("on_error needs to be a bool, int or string") | ||
self.on_error = on_error | ||
|
||
def __call__(self, func): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Decorators function take functions as input. Our class-based implementation will make our object instance callable and accept a function.
self.on_error = on_error | ||
|
||
def __call__(self, func): | ||
func._fsm = Transition( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed the internal location of Transition metadata from .__fsm
to ._fsm
since the double underscore version was mangling names when introspected in the draw_state_diagram
CLI
@@ -29,7 +29,7 @@ def generate_state_diagram_markdown(cls, initial_state): | |||
|
|||
class_fns = inspect.getmembers(cls, predicate=inspect.isfunction) | |||
state_transitions: List[Transition] = [ | |||
func.__fsm for name, func in class_fns if hasattr(func, "__fsm") | |||
func._fsm for name, func in class_fns if hasattr(func, "_fsm") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the corresponding change in the draw_state_diagram
CLI
@transition
function-based decorator a class-based decorator
To implement the functionality required to complete #19, we need a
@transition
decorator that holds internal state.This PR changes the underlying implementation of the
@transition
decorator from a function-based decorator to a class-based decorator.If tests still pass, this means the underlying implementation was changed correctly.