Skip to content

Commit

Permalink
Rename apply_catch
Browse files Browse the repository at this point in the history
  • Loading branch information
DeviousStoat committed Mar 3, 2024
1 parent 8e54a75 commit 606c0b4
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/pydash/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@
)
from .objects import (
apply,
apply_catch,
apply_if,
apply_if_not_none,
apply_ignore_excs,
assign,
assign_with,
callables,
Expand Down Expand Up @@ -548,9 +548,9 @@
"variance",
"zscore",
"apply",
"apply_catch",
"apply_if",
"apply_if_not_none",
"apply_ignore_excs",
"assign",
"assign_with",
"callables",
Expand Down
12 changes: 6 additions & 6 deletions src/pydash/chaining/all_funcs.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2589,21 +2589,21 @@ class AllFuncs:
return self._wrap(pyd.apply_if_not_none)(func)

@t.overload
def apply_ignore_excs(
def apply_catch(
self: "Chain[T]",
func: t.Callable[[T], T2],
exceptions: t.Iterable[t.Type[Exception]],
fallback: T3,
default: T3,
) -> "Chain[t.Union[T2, T3]]": ...
@t.overload
def apply_ignore_excs(
def apply_catch(
self: "Chain[T]",
func: t.Callable[[T], T2],
exceptions: t.Iterable[t.Type[Exception]],
fallback: Unset = UNSET,
default: Unset = UNSET,
) -> "Chain[t.Union[T, T2]]": ...
def apply_ignore_excs(self, func, exceptions, fallback=UNSET):
return self._wrap(pyd.apply_ignore_excs)(func, exceptions, fallback)
def apply_catch(self, func, exceptions, default=UNSET):
return self._wrap(pyd.apply_catch)(func, exceptions, default)

@t.overload
def merge(
Expand Down
14 changes: 6 additions & 8 deletions src/pydash/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

__all__ = (
"apply",
"apply_catch",
"apply_if",
"apply_if_not_none",
"apply_ignore_excs",
"assign",
"assign_with",
"callables",
Expand Down Expand Up @@ -1325,23 +1325,21 @@ def apply_if_not_none(obj: t.Optional[T], func: t.Callable[[T], T2]) -> t.Option


@t.overload
def apply_ignore_excs(
def apply_catch(
obj: T, func: t.Callable[[T], T2], exceptions: t.Iterable[t.Type[Exception]], default: T3
) -> t.Union[T2, T3]:
...
) -> t.Union[T2, T3]: ...


@t.overload
def apply_ignore_excs(
def apply_catch(
obj: T,
func: t.Callable[[T], T2],
exceptions: t.Iterable[t.Type[Exception]],
default: Unset = UNSET,
) -> t.Union[T, T2]:
...
) -> t.Union[T, T2]: ...


def apply_ignore_excs(obj, func, exceptions, default=UNSET):
def apply_catch(obj, func, exceptions, default=UNSET):
"""
Tries to apply `func` to `obj` if any of the exceptions in `excs` are raised, return `default`
or `obj` if not set.
Expand Down
6 changes: 3 additions & 3 deletions tests/pytest_mypy_testing/test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,6 @@ def test_mypy_apply_if_not_none() -> None:


@pytest.mark.mypy_testing
def test_mypy_apply_ignore_excs() -> None:
reveal_type(_.apply_ignore_excs(5, lambda x: x / 0, [ZeroDivisionError])) # R: Union[builtins.int, builtins.float]
reveal_type(_.apply_ignore_excs(5, lambda x: x / 0, [ZeroDivisionError], "error")) # R: Union[builtins.float, builtins.str]
def test_mypy_apply_catch() -> None:
reveal_type(_.apply_catch(5, lambda x: x / 0, [ZeroDivisionError])) # R: Union[builtins.int, builtins.float]
reveal_type(_.apply_catch(5, lambda x: x / 0, [ZeroDivisionError], "error")) # R: Union[builtins.float, builtins.str]
4 changes: 2 additions & 2 deletions tests/test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -968,5 +968,5 @@ def test_apply_if_not_none(case, expected):
"case,expected",
[((5, lambda x: x * 2, [ValueError]), 10), ((5, lambda x: x / 0, [ZeroDivisionError]), 5)],
)
def test_apply_ignore_excs(case, expected):
assert _.apply_ignore_excs(*case) == expected
def test_apply_catch(case, expected):
assert _.apply_catch(*case) == expected

0 comments on commit 606c0b4

Please sign in to comment.