-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/weak diversions #1
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
base: main
Are you sure you want to change the base?
Changes from all commits
acba34f
414b1cb
658990b
6a0a12f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| 0.1.0 | ||
| 0.2.0 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TypeVar, Generic, Any | ||
|
|
||
| Payload = TypeVar("Payload") | ||
|
|
||
|
|
||
| class Diversion(Exception): | ||
| class Diversion(BaseException): | ||
| """Diversion from the normal execution flow.""" | ||
|
|
||
| def __init__(self, jumps: int = 1) -> None: | ||
|
|
@@ -16,6 +18,19 @@ def raise_again(self, *_: Any) -> bool: | |
| self._jumps_remaining -= 1 | ||
| return self._jumps_remaining > 0 | ||
|
|
||
| @classmethod | ||
| def make_weak(cls, *args: Any, **kwargs: Any) -> Diversion: | ||
| """Make weak variant of the diversion class. | ||
|
|
||
| By dynamically forming it provides support for generic extensions of the base diversions. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok hear me out instead of doing this you have a metaclass that generates this exactly once. It's so much machinery for about no payoff There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. while we're on the subject of dumb ways to implement this, how about the 3-arg form of type? |
||
| The minor performance hit is acceptable vs. having to maintain `WeakDiversion` instances for each extension. | ||
| """ | ||
|
|
||
| class WeakDiversion(cls, Exception): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. one weakness with dynamically creating this class is that it is unique every time You can get around this by caching the result of this function (though be careful to access the value on the class itself)
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True, ya, good call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. creating it inline also means that the classname is a little goofy. You can fix that by assigning to both |
||
| """Weak variation of the diversion, so it can be caught by *normal* user exceptions.""" | ||
|
|
||
| return WeakDiversion(*args, **kwargs) | ||
|
|
||
|
|
||
| class PayloadDiversion(Diversion, Generic[Payload]): | ||
| def __init__(self, payload: Payload, jumps: int = 1) -> 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.
you could also make this a separate WF and trigger it only on main
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.
Ya, I think I like how this logically groups it though, keeps the related things together