Release/v0.5.0 #8

Merged
rus07tam merged 20 commits from release/v0.5.0 into main 2025-11-24 16:28:03 +03:00
7 changed files with 82 additions and 37 deletions
Showing only changes of commit e065b51490 - Show all commits

View file

@ -1,13 +1,16 @@
from .attr import get_attrs, get_or_set_attr
from .frame import frame
from .attrs import get_attrs, get_or_set_attr
from .calls import call, caller
from .exceptions import catch, throw
from .frames import frame
from .inherit import inherit
from .nolock import nolock
from .side import side, side_func
from .this import this
from .throw import throw
from .to_async import to_async
__all__ = [
"call",
"caller",
"get_or_set_attr",
"get_attrs",
"frame",
@ -17,5 +20,6 @@ __all__ = [
"side_func",
"this",
"throw",
"catch",
"to_async",
]

12
src/snakia/utils/calls.py Normal file
View file

@ -0,0 +1,12 @@
from typing import Callable, ParamSpec, TypeVar
P = ParamSpec("P")
T = TypeVar("T")
def call(f: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T:
return f(*args, **kwargs)
def caller(f: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> Callable[..., T]:
return lambda *_, **__: f(*args, **kwargs)

View file

@ -0,0 +1,62 @@
import contextlib
from typing import Any, Callable, NoReturn, TypeVar, overload
from exceptiongroup import ExceptionGroup
from snakia.types.unset import Unset
E = TypeVar("E", bound=Exception)
T = TypeVar("T")
D = TypeVar("D")
@overload
def throw(
*exceptions: E, # pyright: ignore[reportInvalidTypeVarUse]
from_: Unset | BaseException = Unset(),
) -> NoReturn: ...
@overload
def throw(
exception: BaseException, from_: Unset | BaseException = Unset(), /
) -> NoReturn: ...
def throw(*exceptions: Any, from_: Unset | BaseException = Unset()) -> NoReturn:
"""Throw an exception."""
if isinstance(from_, Unset):
if len(exceptions) == 1:
raise exceptions[0]
raise ExceptionGroup("", exceptions)
if len(exceptions) == 1:
raise exceptions[0] from from_
raise ExceptionGroup("", exceptions) from from_
contextlib.suppress()
@overload
def catch(
func: Callable[[], T],
*exceptions: type[Exception] | type[BaseException],
default: None = None,
) -> T | None: ...
@overload
def catch(
func: Callable[[], T],
*exceptions: type[Exception] | type[BaseException],
default: D,
) -> T | D: ...
def catch(
func: Callable[[], T],
*exceptions: type[Exception] | type[BaseException],
default: Any = None,
) -> T | Any:
try:
return func()
except BaseException as e:
if any(isinstance(e, exc) for exc in exceptions):
return default
raise

View file

@ -2,7 +2,7 @@ import gc
from types import FunctionType, MethodType
from typing import Any
from .frame import frame
from .frames import frame
def this() -> Any:

View file

@ -1,33 +0,0 @@
from typing import Any, NoReturn, TypeVar, overload
from exceptiongroup import ExceptionGroup
from snakia.types.unset import Unset
T = TypeVar("T", bound=Exception)
@overload
def throw(
*exceptions: T, # pyright: ignore[reportInvalidTypeVarUse]
from_: Unset | BaseException = Unset(),
) -> NoReturn: ...
@overload
def throw(
exception: BaseException, from_: Unset | BaseException = Unset(), /
) -> NoReturn: ...
def throw(
*exceptions: Any, from_: Unset | BaseException = Unset()
) -> NoReturn:
"""Throw an exception."""
if isinstance(from_, Unset):
if len(exceptions) == 1:
raise exceptions[0]
raise ExceptionGroup("", exceptions)
if len(exceptions) == 1:
raise exceptions[0] from from_
raise ExceptionGroup("", exceptions) from from_