Skip to content

Conversation

@simonsben
Copy link
Owner

0.2.0

  • feature: support for weak diversion variants
    • a weak variant allows it to be caught by normal try-catches when using except Exception:

@simonsben simonsben added the enhancement New feature or request label Nov 3, 2023
@simonsben simonsben self-assigned this Nov 3, 2023
The minor performance hit is acceptable vs. having to maintain `WeakDiversion` instances for each extension.
"""

class WeakDiversion(cls, Exception):
Copy link

@lilatomic lilatomic Nov 4, 2023

Choose a reason for hiding this comment

The 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 make_weak is called.
given:

>>> def f():
...     class C(Exception): ...
...     return C
>>> A = f()
>>> B = f()
# If the same instance is used:
>>> try:
...     raise A()
... except A:
...     pass
>>>
# if a different instance is used:
>>> try:
...     raise A()
... except B:
...     pass
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
__main__.f.<locals>.C

You can get around this by caching the result of this function (though be careful to access the value on the class itself)

>>> class E(Exception):
...     weakened = None
...     @classmethod
...     def make_weak(cls):
...             if cls.weakened is None:
...                     class Weakened(cls, Exception): ...
...                     cls.weakened = Weakened
...             return cls.weakened
>>> A = E.make_weak()
>>> B = E.make_weak()
>>> try:
...     raise A()
... except B:
...     pass
>>>

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, ya, good call

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.

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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?

Weakened = type('Weakened', (Diversion, Exception,), {})

The minor performance hit is acceptable vs. having to maintain `WeakDiversion` instances for each extension.
"""

class WeakDiversion(cls, Exception):

Choose a reason for hiding this comment

The 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 __name__ and __qualname__ of the class

- name: Build
run: python -m build

publish-and-publish:

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

Copy link
Owner Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants