[util] extend CustomNone with an __index__ method (#6009)
- Make it compatible with functions expecting integer arguments - Simplify and reuse some method definitions
This commit is contained in:
@@ -520,14 +520,9 @@ class CustomNone():
|
|||||||
"""None-style type that supports more operations than regular None"""
|
"""None-style type that supports more operations than regular None"""
|
||||||
__slots__ = ()
|
__slots__ = ()
|
||||||
|
|
||||||
def __getattribute__(self, _):
|
__getattribute__ = identity
|
||||||
return self
|
__getitem__ = identity
|
||||||
|
__iter__ = identity
|
||||||
def __getitem__(self, _):
|
|
||||||
return self
|
|
||||||
|
|
||||||
def __iter__(self):
|
|
||||||
return self
|
|
||||||
|
|
||||||
def __call__(self, *args, **kwargs):
|
def __call__(self, *args, **kwargs):
|
||||||
return self
|
return self
|
||||||
@@ -536,10 +531,6 @@ class CustomNone():
|
|||||||
def __next__():
|
def __next__():
|
||||||
raise StopIteration
|
raise StopIteration
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def __bool__():
|
|
||||||
return False
|
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
return self is other
|
return self is other
|
||||||
|
|
||||||
@@ -550,6 +541,7 @@ class CustomNone():
|
|||||||
__le__ = true
|
__le__ = true
|
||||||
__gt__ = false
|
__gt__ = false
|
||||||
__ge__ = false
|
__ge__ = false
|
||||||
|
__bool__ = false
|
||||||
|
|
||||||
__add__ = identity
|
__add__ = identity
|
||||||
__sub__ = identity
|
__sub__ = identity
|
||||||
@@ -588,9 +580,8 @@ class CustomNone():
|
|||||||
def __len__():
|
def __len__():
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
@staticmethod
|
__hash__ = __len__
|
||||||
def __hash__():
|
__index__ = __len__
|
||||||
return 0
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def __format__(_):
|
def __format__(_):
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import sys
|
|||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
import io
|
import io
|
||||||
|
import time
|
||||||
import random
|
import random
|
||||||
import string
|
import string
|
||||||
import datetime
|
import datetime
|
||||||
@@ -741,6 +742,9 @@ def hash(value):
|
|||||||
|
|
||||||
self.assertFalse(obj)
|
self.assertFalse(obj)
|
||||||
self.assertEqual(len(obj), 0)
|
self.assertEqual(len(obj), 0)
|
||||||
|
self.assertEqual(int(obj), 0)
|
||||||
|
self.assertEqual(hash(obj), 0)
|
||||||
|
|
||||||
self.assertEqual(str(obj), str(None))
|
self.assertEqual(str(obj), str(None))
|
||||||
self.assertEqual(repr(obj), repr(None))
|
self.assertEqual(repr(obj), repr(None))
|
||||||
self.assertEqual(format(obj), str(None))
|
self.assertEqual(format(obj), str(None))
|
||||||
@@ -751,6 +755,7 @@ def hash(value):
|
|||||||
self.assertIs(obj(), obj)
|
self.assertIs(obj(), obj)
|
||||||
self.assertIs(obj(1, "a"), obj)
|
self.assertIs(obj(1, "a"), obj)
|
||||||
self.assertIs(obj(foo="bar"), obj)
|
self.assertIs(obj(foo="bar"), obj)
|
||||||
|
self.assertIs(iter(obj), obj)
|
||||||
self.assertEqual(util.json_dumps(obj), "null")
|
self.assertEqual(util.json_dumps(obj), "null")
|
||||||
|
|
||||||
self.assertLess(obj, "foo")
|
self.assertLess(obj, "foo")
|
||||||
@@ -797,6 +802,12 @@ def hash(value):
|
|||||||
mapping = {}
|
mapping = {}
|
||||||
mapping[obj] = 123
|
mapping[obj] = 123
|
||||||
self.assertIn(obj, mapping)
|
self.assertIn(obj, mapping)
|
||||||
|
self.assertEqual(mapping[obj], 123)
|
||||||
|
|
||||||
|
array = [1, 2, 3]
|
||||||
|
self.assertEqual(array[obj], 1)
|
||||||
|
|
||||||
|
self.assertTrue(time.localtime(obj))
|
||||||
|
|
||||||
i = 0
|
i = 0
|
||||||
for _ in obj:
|
for _ in obj:
|
||||||
|
|||||||
Reference in New Issue
Block a user