From 5da7bcab1f131a9f92acd3523acbfa67c2396961 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <piotr.maslanka@henrietta.com.pl> Date: Tue, 5 Mar 2024 09:00:06 +0100 Subject: [PATCH] deprecated call_if_nnone --- CHANGELOG.md | 1 + satella/coding/optionals.py | 10 ++++++---- tests/test_coding/test_optionals.py | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 53518fc9..d236a2a6 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 c7b56b4b..ea754b5e 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 aec61bca..94769222 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): -- GitLab