Prevent instantiation of ShardRangeOuterBound

Only the subclasses should be instantiated.

Co-Authored-By: Tim Burke <tim.burke@gmail.com>
Change-Id: I9b90863bb502ca8b95e626e83775475c352a7a4d
Related-Change: I21292e7991e93834b35cda6f5daea4c552a8e999
This commit is contained in:
Alistair Coles 2021-03-25 11:07:12 +00:00
parent b17dd7ec75
commit 4bc2b3ef97
2 changed files with 8 additions and 1 deletions

View File

@ -4972,11 +4972,15 @@ except TypeError:
class ShardRangeOuterBound(object): class ShardRangeOuterBound(object):
""" """
A custom singleton type used for the outer bounds of ShardRanges. A custom singleton type to be subclassed for the outer bounds of
ShardRanges.
""" """
_singleton = None _singleton = None
def __new__(cls): def __new__(cls):
if cls is ShardRangeOuterBound:
raise TypeError('ShardRangeOuterBound is an abstract class; '
'only subclasses should be instantiated')
if cls._singleton is None: if cls._singleton is None:
cls._singleton = super(ShardRangeOuterBound, cls).__new__(cls) cls._singleton = super(ShardRangeOuterBound, cls).__new__(cls)
return cls._singleton return cls._singleton

View File

@ -7732,6 +7732,9 @@ class TestShardRange(unittest.TestCase):
self.ts_iter = make_timestamp_iter() self.ts_iter = make_timestamp_iter()
def test_min_max_bounds(self): def test_min_max_bounds(self):
with self.assertRaises(TypeError):
utils.ShardRangeOuterBound()
# max # max
self.assertEqual(utils.ShardRange.MAX, utils.ShardRange.MAX) self.assertEqual(utils.ShardRange.MAX, utils.ShardRange.MAX)
self.assertFalse(utils.ShardRange.MAX > utils.ShardRange.MAX) self.assertFalse(utils.ShardRange.MAX > utils.ShardRange.MAX)