Skip to content

Commit a17f7d5

Browse files
committed
PROTON-2895: [Python] Use __new__ when subclassing immutable types
1 parent 3f423fa commit a17f7d5

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

python/proton/_data.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ class ulong(long):
6363
An unsigned 64 bit integer in the range :math:`0` to :math:`2^{64} - 1` inclusive.
6464
"""
6565

66-
def __init__(self, u64: int) -> None:
66+
def __new__(self, u64: int) -> ulong:
6767
if u64 < 0:
68-
raise AssertionError("initializing ulong with negative value")
69-
super().__new__(ulong, u64)
68+
raise ValueError("initializing ulong with negative value")
69+
return super().__new__(ulong, u64)
7070

7171
def __repr__(self) -> str:
7272
return "ulong(%s)" % long.__repr__(self)
@@ -148,10 +148,10 @@ class ubyte(int):
148148
An 8 bit unsigned integer in the range :math:`0` to :math:`2^8 - 1` inclusive.
149149
"""
150150

151-
def __init__(self, i: int) -> None:
151+
def __new__(self, i: int) -> ubyte:
152152
if i < 0:
153-
raise AssertionError("initializing ubyte with negative value")
154-
super().__new__(ubyte, i)
153+
raise ValueError("initializing ubyte with negative value")
154+
return super().__new__(ubyte, i)
155155

156156
def __repr__(self) -> str:
157157
return "ubyte(%s)" % int.__repr__(self)
@@ -164,10 +164,10 @@ class ushort(int):
164164
A 16 bit unsigned integer in the range :math:`0` to :math:`2^{16} - 1` inclusive.
165165
"""
166166

167-
def __init__(self, i: int) -> None:
167+
def __new__(self, i: int) -> ushort:
168168
if i < 0:
169-
raise AssertionError("initializing ushort with negative value")
170-
super().__new__(ushort, i)
169+
raise ValueError("initializing ushort with negative value")
170+
return super().__new__(ushort, i)
171171

172172
def __repr__(self) -> str:
173173
return "ushort(%s)" % int.__repr__(self)
@@ -180,10 +180,10 @@ class uint(long):
180180
A 32 bit unsigned integer in the range :math:`0` to :math:`2^{32} - 1` inclusive.
181181
"""
182182

183-
def __init__(self, u32: int) -> None:
183+
def __new__(self, u32: int) -> uint:
184184
if u32 < 0:
185-
raise AssertionError("initializing uint with negative value")
186-
super().__new__(uint, u32)
185+
raise ValueError("initializing uint with negative value")
186+
return super().__new__(uint, u32)
187187

188188
def __repr__(self) -> str:
189189
return "uint(%s)" % long.__repr__(self)

python/tests/proton_tests/codec.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -396,28 +396,28 @@ def testByte(self):
396396

397397
def testUbyte(self):
398398
self._test_int("ubyte")
399-
self.assertRaises(AssertionError, ubyte, -1)
399+
self.assertRaises(ValueError, ubyte, -1)
400400

401401
def testShort(self):
402402
self._test_int("short")
403403

404404
def testUshort(self):
405405
self._test("ushort")
406-
self.assertRaises(AssertionError, ushort, -1)
406+
self.assertRaises(ValueError, ushort, -1)
407407

408408
def testInt(self):
409409
self._test_int("int")
410410

411411
def testUint(self):
412412
self._test_int("uint")
413-
self.assertRaises(AssertionError, uint, -1)
413+
self.assertRaises(ValueError, uint, -1)
414414

415415
def testLong(self):
416416
self._test_int("long")
417417

418418
def testUlong(self):
419419
self._test_int("ulong")
420-
self.assertRaises(AssertionError, ulong, -1)
420+
self.assertRaises(ValueError, ulong, -1)
421421

422422
def testString(self):
423423
self._test("string", "one", "two", "three", "this is a test", "")

0 commit comments

Comments
 (0)