Skip to content

Commit

Permalink
Default value is the object, use UNSET
Browse files Browse the repository at this point in the history
  • Loading branch information
DeviousStoat committed Mar 3, 2024
1 parent 77e13c3 commit c0f0915
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
12 changes: 6 additions & 6 deletions src/pydash/chaining/all_funcs.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2559,18 +2559,18 @@ class AllFuncs:
def apply_ignore_excs(
self: "Chain[T]",
func: t.Callable[[T], T2],
excs: t.Iterable[t.Type[Exception]],
exceptions: t.Iterable[t.Type[Exception]],
fallback: T3,
) -> "Chain[t.Union[T2, T3]]": ...
@t.overload
def apply_ignore_excs(
self: "Chain[T]",
func: t.Callable[[T], T2],
excs: t.Iterable[t.Type[Exception]],
fallback: None = None,
) -> "Chain[t.Union[T2, None]]": ...
def apply_ignore_excs(self, func, excs, fallback=None):
return self._wrap(pyd.apply_ignore_excs)(func, excs, fallback)
exceptions: t.Iterable[t.Type[Exception]],
fallback: 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)

@t.overload
def merge(
Expand Down
19 changes: 10 additions & 9 deletions src/pydash/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import pydash as pyd

from .helpers import UNSET, base_get, base_set, callit, getargcount, iterator, iteriteratee
from .helpers import UNSET, Unset, base_get, base_set, callit, getargcount, iterator, iteriteratee
from .types import IterateeObjT, PathT
from .utilities import PathToken, to_path, to_path_tokens

Expand Down Expand Up @@ -1405,14 +1405,15 @@ def apply_ignore_excs(
obj: T,
func: t.Callable[[T], T2],
exceptions: t.Iterable[t.Type[Exception]],
fallback: None = None,
) -> t.Union[T2, None]:
fallback: Unset = UNSET,
) -> t.Union[T, T2]:
...


def apply_ignore_excs(obj, func, exceptions, fallback=None):
def apply_ignore_excs(obj, func, exceptions, fallback=UNSET):
"""
Tries to apply `func` to `obj` if any of the exceptions in `excs` are raised, return `fallback`.
Tries to apply `func` to `obj` if any of the exceptions in `excs` are raised, return `fallback`
or `obj` if not set.
Args:
obj: Object to apply `func` to.
Expand All @@ -1421,23 +1422,23 @@ def apply_ignore_excs(obj, func, exceptions, fallback=None):
fallback: Value to return if exception is raised.
Returns:
Result of applying `func` to `obj` or ``None``.
Result of applying `func` to `obj` or ``fallback``.
Example:
>>> apply_ignore_excs(2, lambda x: x * 2, [ValueError])
4
>>> apply_ignore_excs(2, lambda x: x / 0, [ZeroDivisionError], "error")
'error'
>>> apply_ignore_excs(2, lambda x: x / 0, [ZeroDivisionError]) is None
True
>>> apply_ignore_excs(2, lambda x: x / 0, [ZeroDivisionError])
2
.. versionadded:: 8.0.0
"""
try:
return func(obj)
except tuple(exceptions):
return fallback
return obj if fallback is UNSET else fallback


@t.overload
Expand Down
2 changes: 1 addition & 1 deletion 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.float, 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]
2 changes: 1 addition & 1 deletion tests/test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,7 @@ def test_apply_if_not_none(case, expected):

@parametrize(
"case,expected",
[((5, lambda x: x * 2, [ValueError]), 10), ((5, lambda x: x / 0, [ZeroDivisionError]), None)],
[((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

0 comments on commit c0f0915

Please sign in to comment.