diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d8fe903d900b0175b9e62a509844eca85ef9000..53518fc99481141f099146d63a2f8d813a975d85 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 92f174338fb2aa17bd81b6aeaf149a9868d0b7ee..c7b56b4b9bc9a776f2abf28c7ac0133f08cc21db 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)