This is an automated email from the ASF dual-hosted git repository. asf-gitbox-commits pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/qpid-proton.git
commit a17f7d556fb2f71bb14037d126ba77260c7d5873 Author: Andrew Stitcher <[email protected]> AuthorDate: Fri May 2 15:37:27 2025 -0400 PROTON-2895: [Python] Use __new__ when subclassing immutable types --- python/proton/_data.py | 24 ++++++++++++------------ python/tests/proton_tests/codec.py | 8 ++++---- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/python/proton/_data.py b/python/proton/_data.py index d360eb25b..a50d4162e 100644 --- a/python/proton/_data.py +++ b/python/proton/_data.py @@ -63,10 +63,10 @@ class ulong(long): An unsigned 64 bit integer in the range :math:`0` to :math:`2^{64} - 1` inclusive. """ - def __init__(self, u64: int) -> None: + def __new__(self, u64: int) -> ulong: if u64 < 0: - raise AssertionError("initializing ulong with negative value") - super().__new__(ulong, u64) + raise ValueError("initializing ulong with negative value") + return super().__new__(ulong, u64) def __repr__(self) -> str: return "ulong(%s)" % long.__repr__(self) @@ -148,10 +148,10 @@ class ubyte(int): An 8 bit unsigned integer in the range :math:`0` to :math:`2^8 - 1` inclusive. """ - def __init__(self, i: int) -> None: + def __new__(self, i: int) -> ubyte: if i < 0: - raise AssertionError("initializing ubyte with negative value") - super().__new__(ubyte, i) + raise ValueError("initializing ubyte with negative value") + return super().__new__(ubyte, i) def __repr__(self) -> str: return "ubyte(%s)" % int.__repr__(self) @@ -164,10 +164,10 @@ class ushort(int): A 16 bit unsigned integer in the range :math:`0` to :math:`2^{16} - 1` inclusive. """ - def __init__(self, i: int) -> None: + def __new__(self, i: int) -> ushort: if i < 0: - raise AssertionError("initializing ushort with negative value") - super().__new__(ushort, i) + raise ValueError("initializing ushort with negative value") + return super().__new__(ushort, i) def __repr__(self) -> str: return "ushort(%s)" % int.__repr__(self) @@ -180,10 +180,10 @@ class uint(long): A 32 bit unsigned integer in the range :math:`0` to :math:`2^{32} - 1` inclusive. """ - def __init__(self, u32: int) -> None: + def __new__(self, u32: int) -> uint: if u32 < 0: - raise AssertionError("initializing uint with negative value") - super().__new__(uint, u32) + raise ValueError("initializing uint with negative value") + return super().__new__(uint, u32) def __repr__(self) -> str: return "uint(%s)" % long.__repr__(self) diff --git a/python/tests/proton_tests/codec.py b/python/tests/proton_tests/codec.py index bfc6913c2..92caf1a16 100644 --- a/python/tests/proton_tests/codec.py +++ b/python/tests/proton_tests/codec.py @@ -396,28 +396,28 @@ class DataTest(Test): def testUbyte(self): self._test_int("ubyte") - self.assertRaises(AssertionError, ubyte, -1) + self.assertRaises(ValueError, ubyte, -1) def testShort(self): self._test_int("short") def testUshort(self): self._test("ushort") - self.assertRaises(AssertionError, ushort, -1) + self.assertRaises(ValueError, ushort, -1) def testInt(self): self._test_int("int") def testUint(self): self._test_int("uint") - self.assertRaises(AssertionError, uint, -1) + self.assertRaises(ValueError, uint, -1) def testLong(self): self._test_int("long") def testUlong(self): self._test_int("ulong") - self.assertRaises(AssertionError, ulong, -1) + self.assertRaises(ValueError, ulong, -1) def testString(self): self._test("string", "one", "two", "three", "this is a test", "") --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
