From 52b3288205b36db1e78ce29aab0def19c10a20f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Furga=C5=82a?= <83299832+00200200@users.noreply.github.com> Date: Sun, 11 Oct 2026 16:27:14 +0200 Subject: [PATCH] Cache None results in LazyProxy --- babel/support.py | 6 +++++- tests/test_support_lazy_proxy.py | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/babel/support.py b/babel/support.py index 8cc2492e8..6b1833926 100644 --- a/babel/support.py +++ b/babel/support.py @@ -289,6 +289,7 @@ class LazyProxy: '_kwargs', '_value', '_is_cache_enabled', + '_is_cached', '_attribute_error', ] @@ -297,6 +298,7 @@ class LazyProxy: _args: tuple[Any, ...] _kwargs: dict[str, Any] _is_cache_enabled: bool + _is_cached: bool _value: Any _attribute_error: AttributeError | None @@ -313,11 +315,12 @@ def __init__( object.__setattr__(self, '_kwargs', kwargs) object.__setattr__(self, '_is_cache_enabled', enable_cache) object.__setattr__(self, '_value', None) + object.__setattr__(self, '_is_cached', False) object.__setattr__(self, '_attribute_error', None) @property def value(self) -> Any: - if self._value is None: + if not self._is_cached: try: value = self._func(*self._args, **self._kwargs) except AttributeError as error: @@ -327,6 +330,7 @@ def value(self) -> Any: if not self._is_cache_enabled: return value object.__setattr__(self, '_value', value) + object.__setattr__(self, '_is_cached', True) return self._value def __contains__(self, key: object) -> bool: diff --git a/tests/test_support_lazy_proxy.py b/tests/test_support_lazy_proxy.py index 59cab1f58..3b0eb14ef 100644 --- a/tests/test_support_lazy_proxy.py +++ b/tests/test_support_lazy_proxy.py @@ -31,6 +31,23 @@ def add_one(): assert proxy.value == 2 +@pytest.mark.parametrize('enable_cache', [True, False]) +def test_proxy_none_result(enable_cache): + calls = [] + + def return_none(): + calls.append(None) + return None + + proxy = support.LazyProxy(return_none, enable_cache=enable_cache) + assert calls == [] + assert proxy.value is None + assert proxy.value is None + assert not proxy + assert str(proxy) == 'None' + assert len(calls) == (1 if enable_cache else 4) + + @pytest.mark.parametrize( ("copier", "expected_copy_value"), [