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 750d888 commit c7e2d16
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 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 @@ -2556,21 +2556,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: 7 additions & 7 deletions src/pydash/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"apply",
"apply_if",
"apply_if_not_none",
"apply_ignore_excs",
"apply_catch",
"assign",
"assign_with",
"callables",
Expand Down Expand Up @@ -1394,14 +1394,14 @@ 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.overload
def apply_ignore_excs(
def apply_catch(
obj: T,
func: t.Callable[[T], T2],
exceptions: t.Iterable[t.Type[Exception]],
Expand All @@ -1410,7 +1410,7 @@ def apply_ignore_excs(
...


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 All @@ -1426,11 +1426,11 @@ def apply_ignore_excs(obj, func, exceptions, default=UNSET):
Example:
>>> apply_ignore_excs(2, lambda x: x * 2, [ValueError])
>>> apply_catch(2, lambda x: x * 2, [ValueError])
4
>>> apply_ignore_excs(2, lambda x: x / 0, [ZeroDivisionError], "error")
>>> apply_catch(2, lambda x: x / 0, [ZeroDivisionError], "error")
'error'
>>> apply_ignore_excs(2, lambda x: x / 0, [ZeroDivisionError])
>>> apply_catch(2, lambda x: x / 0, [ZeroDivisionError])
2
.. versionadded:: 8.0.0
Expand Down
4 changes: 2 additions & 2 deletions tests/pytest_mypy_testing/test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,5 +368,5 @@ 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]
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]
2 changes: 1 addition & 1 deletion tests/test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -969,4 +969,4 @@ def test_apply_if_not_none(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
assert _.apply_catch(*case) == expected

0 comments on commit c7e2d16

Please sign in to comment.