From baa4b569245373d53746c8b17d18ad083ff8a75e 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 08:54:59 +0100 Subject: [PATCH] fix Optional --- CHANGELOG.md | 1 + satella/coding/optionals.py | 25 ++++++++++++++----------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d8fe903..53518fc9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,3 +5,4 @@ * added a way to register an object to be cleaned up via MemoryPressureManager * fixed get_own_cpu_usage() to work on Windows * added __len__ to Optional +* major bugfix in Optional diff --git a/satella/coding/optionals.py b/satella/coding/optionals.py index 92f17433..c7b56b4b 100644 --- a/satella/coding/optionals.py +++ b/satella/coding/optionals.py @@ -77,33 +77,36 @@ class Optional(Proxy[T]): return other == me def __getattr__(self, item): - return EMPTY_OPTIONAL if getattr(self, '_Proxy__obj') is None else super().__getattr__(item) + obj = getattr(self, '_Proxy__obj') + return EMPTY_OPTIONAL if obj is None else getattr(obj, item) def __call__(self, *args, **kwargs): - return EMPTY_OPTIONAL if getattr(self, '_Proxy__obj') is None else super().__call__(*args, **kwargs) + obj = getattr(self, '_Proxy__obj') + return EMPTY_OPTIONAL if obj is None else obj(*args, **kwargs) def __bool__(self): - return False if getattr(self, '_Proxy__obj') is None else super().__bool__() + obj = getattr(self, '_Proxy__obj') + return False if obj is None else bool(obj) def __getitem__(self, item): - return EMPTY_OPTIONAL if getattr(self, '_Proxy__obj') is None else super().__getattr__(item) + obj = getattr(self, '_Proxy__obj') + return EMPTY_OPTIONAL if getattr(self, '_Proxy__obj') is None else obj[item] def __setitem__(self, key, value) -> None: - if getattr(self, '_Proxy__obj') is None: - return - super().__setitem__(key, value) + obj = getattr(self, '_Proxy__obj') + if obj is not None: + obj.__setitem__(key, value) def __delitem__(self, key) -> None: - if getattr(self, '_Proxy__obj') is None: - return - super().__delitem__(key) + obj = getattr(self, '_Proxy__obj') + if obj is not None: + del obj[key] def __len__(self) -> int: obj = getattr(self, '_Proxy__obj') return 0 if obj is None else len(obj) - EMPTY_OPTIONAL = Optional(None) -- GitLab