diff --git a/CHANGELOG.md b/CHANGELOG.md index 53518fc99481141f099146d63a2f8d813a975d85..d236a2a68d11d5ad5296c57dcb1e6b3be3e4237a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,3 +6,4 @@ * fixed get_own_cpu_usage() to work on Windows * added __len__ to Optional * major bugfix in Optional +* deprecated call_if_nnone diff --git a/satella/coding/optionals.py b/satella/coding/optionals.py index c7b56b4b9bc9a776f2abf28c7ac0133f08cc21db..ea754b5ea8d87ec59b77904edfe98a61b7b02167 100644 --- a/satella/coding/optionals.py +++ b/satella/coding/optionals.py @@ -1,4 +1,5 @@ import typing as tp +import warnings from satella.coding.structures import Proxy from satella.coding.typing import V, T @@ -22,6 +23,9 @@ def call_if_nnone(clbl: tp.Optional[tp.Callable[..., V]], *args, **kwargs) -> tp """ Call clbl with provided arguments, but only if clbl is not None. + .. deprecated:: 2.24.2 + Use Optional(clbl)(*args, **kwargs) instead + If it's None, then None will be returned. Else, the result of this callable will be returned. :param clbl: callable to call, or a None @@ -29,10 +33,8 @@ def call_if_nnone(clbl: tp.Optional[tp.Callable[..., V]], *args, **kwargs) -> tp :param kwargs: keyword arguments to provide to clbl :return: return value or None """ - if clbl is not None: - return clbl(*args, **kwargs) - else: - return None + warnings.warn('This is deprecated, use Optional(clbl)() instead', DeprecationWarning) + return clbl(*args, **kwargs) if clbl is not None else None class Optional(Proxy[T]): diff --git a/tests/test_coding/test_optionals.py b/tests/test_coding/test_optionals.py index aec61bca978af43152de7c3345c7d7e123ff9670..947692224198f6feb8a7211021f739aae5925ae7 100644 --- a/tests/test_coding/test_optionals.py +++ b/tests/test_coding/test_optionals.py @@ -21,7 +21,7 @@ class TestOptionals(unittest.TestCase): self.assertTrue(b) def test_optional_lambda(self): - Optional(None()) + Optional(None)() Optional(lambda: 5)() def test_object(self):