Skip to content
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

Merged
merged 14 commits into from
Oct 10, 2021

Conversation

alysivji
Copy link
Owner

@alysivji alysivji commented Oct 10, 2021

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.

@codecov-commenter
Copy link

codecov-commenter commented Oct 10, 2021

Codecov Report

Merging #34 (ef3cba9) into main (09b1502) will increase coverage by 0.03%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            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              
Impacted Files Coverage Δ
finite_state_machine/draw_state_diagram.py 81.81% <100.00%> (ø)
finite_state_machine/state_machine.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 09b1502...ef3cba9. Read the comment docs.

for item in source:
if not isinstance(item, allowed_types):
raise ValueError("Source can be a bool, int, string, Enum, or list")
class transition:
Copy link
Owner Author

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):
Copy link
Owner Author

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):
Copy link
Owner Author

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(
Copy link
Owner Author

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")
Copy link
Owner Author

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

@alysivji alysivji changed the title Make @transition decorator function a class-based decorator Make @transition function-based decorator a class-based decorator Oct 10, 2021
@alysivji alysivji merged commit 16d93fe into main Oct 10, 2021
@alysivji alysivji deleted the 19/make-transition-decorator-a-class branch October 10, 2021 21:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants