Skip to content
Snippets Groups Projects
Commit baa4b569 authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

fix Optional

parent 995eef03
No related branches found
No related tags found
No related merge requests found
...@@ -5,3 +5,4 @@ ...@@ -5,3 +5,4 @@
* added a way to register an object to be cleaned up via MemoryPressureManager * added a way to register an object to be cleaned up via MemoryPressureManager
* fixed get_own_cpu_usage() to work on Windows * fixed get_own_cpu_usage() to work on Windows
* added __len__ to Optional * added __len__ to Optional
* major bugfix in Optional
...@@ -77,33 +77,36 @@ class Optional(Proxy[T]): ...@@ -77,33 +77,36 @@ class Optional(Proxy[T]):
return other == me return other == me
def __getattr__(self, item): 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): 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): 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): 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: def __setitem__(self, key, value) -> None:
if getattr(self, '_Proxy__obj') is None: obj = getattr(self, '_Proxy__obj')
return if obj is not None:
super().__setitem__(key, value) obj.__setitem__(key, value)
def __delitem__(self, key) -> None: def __delitem__(self, key) -> None:
if getattr(self, '_Proxy__obj') is None: obj = getattr(self, '_Proxy__obj')
return if obj is not None:
super().__delitem__(key) del obj[key]
def __len__(self) -> int: def __len__(self) -> int:
obj = getattr(self, '_Proxy__obj') obj = getattr(self, '_Proxy__obj')
return 0 if obj is None else len(obj) return 0 if obj is None else len(obj)
EMPTY_OPTIONAL = Optional(None) EMPTY_OPTIONAL = Optional(None)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment