(qpid-proton) 03/03: PROTON-2895: [Python] Use __new__ when subclassing immutable types
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 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: commits-unsubscr...@qpid.apache.org For additional commands, e-mail: commits-h...@qpid.apache.org
(qpid-proton) 01/03: PROTON-2881: [Python] More idiomatic python 3 code.
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 5193b337468228f579a35191b9e55e7c4f0146fb Author: Andrew Stitcher AuthorDate: Fri May 2 15:20:06 2025 -0400 PROTON-2881: [Python] More idiomatic python 3 code. * Don't need to explicitly inherit from object * Can use just super() --- python/proton/_common.py| 2 +- python/proton/_data.py | 40 ++--- python/proton/_endpoints.py | 2 +- python/proton/_events.py| 4 ++-- python/proton/_handler.py | 4 ++-- python/proton/_handlers.py | 10 +- python/proton/_io.py| 4 ++-- python/proton/_message.py | 2 +- python/proton/_reactor.py | 30 ++-- python/proton/_selectable.py| 2 +- python/proton/_transport.py | 8 python/proton/_url.py | 4 ++-- python/proton/_utils.py | 12 +-- python/tests/proton_tests/common.py | 4 ++-- python/tests/proton_tests/ssl.py| 2 +- 15 files changed, 65 insertions(+), 65 deletions(-) diff --git a/python/proton/_common.py b/python/proton/_common.py index 134d398a8..1654c614f 100644 --- a/python/proton/_common.py +++ b/python/proton/_common.py @@ -20,7 +20,7 @@ from typing import Union -class Constant(object): +class Constant: def __init__(self, name: str) -> None: self.name = name diff --git a/python/proton/_data.py b/python/proton/_data.py index 821f296a6..11508d64a 100644 --- a/python/proton/_data.py +++ b/python/proton/_data.py @@ -71,7 +71,7 @@ class ulong(long): def __init__(self, u64: int) -> None: if u64 < 0: raise AssertionError("initializing ulong with negative value") -super(ulong, self).__new__(ulong, u64) +super().__new__(ulong, u64) def __repr__(self) -> str: return "ulong(%s)" % long.__repr__(self) @@ -156,7 +156,7 @@ class ubyte(int): def __init__(self, i: int) -> None: if i < 0: raise AssertionError("initializing ubyte with negative value") -super(ubyte, self).__new__(ubyte, i) +super().__new__(ubyte, i) def __repr__(self) -> str: return "ubyte(%s)" % int.__repr__(self) @@ -172,7 +172,7 @@ class ushort(int): def __init__(self, i: int) -> None: if i < 0: raise AssertionError("initializing ushort with negative value") -super(ushort, self).__new__(ushort, i) +super().__new__(ushort, i) def __repr__(self) -> str: return "ushort(%s)" % int.__repr__(self) @@ -188,7 +188,7 @@ class uint(long): def __init__(self, u32: int) -> None: if u32 < 0: raise AssertionError("initializing uint with negative value") -super(uint, self).__new__(uint, u32) +super().__new__(uint, u32) def __repr__(self) -> str: return "uint(%s)" % long.__repr__(self) @@ -238,7 +238,7 @@ class decimal128(bytes): return "decimal128(%s)" % bytes.__repr__(self) -class Described(object): +class Described: """ A described AMQP type. @@ -267,7 +267,7 @@ class Described(object): UNDESCRIBED = Constant("UNDESCRIBED") -class Array(object): +class Array: """ An AMQP array, a sequence of AMQP values of a single type. @@ -343,7 +343,7 @@ class RestrictedKeyDict(dict): raise_on_error: bool = True, **kwargs ) -> None: -super(RestrictedKeyDict, self).__init__() +super().__init__() self.validation_fn = validation_fn self.raise_on_error = raise_on_error self.update(e, **kwargs) @@ -351,7 +351,7 @@ class RestrictedKeyDict(dict): def __setitem__(self, key: Union[symbol, str], value: Any) -> None: """Checks if the key is a :class:`symbol` type before setting the value""" try: -return super(RestrictedKeyDict, self).__setitem__(self.validation_fn(key, self.raise_on_error), value) +return super().__setitem__(self.validation_fn(key, self.raise_on_error), value) except TypeError: pass # __setitem__() must raise a KeyError, not TypeError @@ -410,11 +410,11 @@ class PropertyDict(RestrictedKeyDict): """ def __init__(self, e: Optional[Any] = None, raise_on_error: bool = True, **kwargs) -> None: -super(PropertyDict, self).__init__(_check_is_symbol, e, raise_on_error, **kwargs) +super().__init__(_check_is_symbol, e, raise_on_error, **kwargs) def __repr__(self): ""&q
(qpid-proton) branch main updated (d1ce340cb -> a17f7d556)
This is an automated email from the ASF dual-hosted git repository. asf-gitbox-commits pushed a change to branch main in repository https://gitbox.apache.org/repos/asf/qpid-proton.git from d1ce340cb PROTON-2890: [Python examples] Broker: Improve transactional link handling new 5193b3374 PROTON-2881: [Python] More idiomatic python 3 code. new 3f423fa90 PROTON-2891: [Python] More type annotation improvements new a17f7d556 PROTON-2895: [Python] Use __new__ when subclassing immutable types The 3 revisions listed above as "new" are entirely new to this repository and will be described in separate emails. The revisions listed as "add" were already present in the repository and have only been added to this reference. Summary of changes: python/proton/_common.py| 2 +- python/proton/_condition.py | 6 ++- python/proton/_data.py | 84 +++-- python/proton/_delivery.py | 16 +++ python/proton/_endpoints.py | 30 ++--- python/proton/_events.py| 12 +++--- python/proton/_handler.py | 7 ++-- python/proton/_handlers.py | 14 --- python/proton/_io.py| 12 +++--- python/proton/_message.py | 10 +++-- python/proton/_reactor.py | 74 python/proton/_selectable.py| 12 +++--- python/proton/_tracing.py | 2 + python/proton/_transport.py | 28 +++-- python/proton/_url.py | 4 +- python/proton/_utils.py | 54 python/proton/_wrapper.py | 6 ++- python/tests/proton_tests/codec.py | 8 ++-- python/tests/proton_tests/common.py | 4 +- python/tests/proton_tests/ssl.py| 2 +- 20 files changed, 208 insertions(+), 179 deletions(-) - To unsubscribe, e-mail: commits-unsubscr...@qpid.apache.org For additional commands, e-mail: commits-h...@qpid.apache.org
(qpid-proton) 02/03: PROTON-2891: [Python] More type annotation improvements
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 3f423fa90aa73771387ebd203bd5d38a5811a0d8 Author: Andrew Stitcher AuthorDate: Fri May 2 15:29:17 2025 -0400 PROTON-2891: [Python] More type annotation improvements --- python/proton/_condition.py | 6 -- python/proton/_data.py | 28 +++- python/proton/_delivery.py | 16 +--- python/proton/_endpoints.py | 28 +++- python/proton/_events.py | 8 +--- python/proton/_handler.py| 3 ++- python/proton/_handlers.py | 4 +++- python/proton/_io.py | 8 +--- python/proton/_message.py| 8 +--- python/proton/_reactor.py| 44 +++- python/proton/_selectable.py | 10 ++ python/proton/_tracing.py| 2 ++ python/proton/_transport.py | 20 +++- python/proton/_utils.py | 42 ++ python/proton/_wrapper.py| 6 -- 15 files changed, 131 insertions(+), 102 deletions(-) diff --git a/python/proton/_condition.py b/python/proton/_condition.py index cf0c1c5e2..81e551f4d 100644 --- a/python/proton/_condition.py +++ b/python/proton/_condition.py @@ -17,6 +17,8 @@ # under the License. # +from __future__ import annotations + from typing import Optional, TYPE_CHECKING from cproton import pn_condition_clear, pn_condition_set_name, pn_condition_set_description, pn_condition_info, \ @@ -61,7 +63,7 @@ class Condition: self, name: str, description: Optional[str] = None, -info: Optional['PythonAMQPData'] = None +info: Optional[PythonAMQPData] = None ) -> None: self.name = name self.description = description @@ -72,7 +74,7 @@ class Condition: (self.name, self.description, self.info) if x]) -def __eq__(self, o: 'Condition') -> bool: +def __eq__(self, o: Condition) -> bool: if not isinstance(o, Condition): return False return self.name == o.name and \ diff --git a/python/proton/_data.py b/python/proton/_data.py index 11508d64a..d360eb25b 100644 --- a/python/proton/_data.py +++ b/python/proton/_data.py @@ -17,6 +17,8 @@ # under the License. # +from __future__ import annotations + import uuid from collections.abc import Iterable from typing import Callable, Union, Optional, Any, TypeVar @@ -42,15 +44,8 @@ from ._exceptions import DataException, EXCEPTIONS long = int unicode = str - _T = TypeVar('_T') -PythonAMQPData = Union[ -dict['PythonAMQPData', 'PythonAMQPData'], -list['PythonAMQPData'], -'Described', 'Array', int, str, 'symbol', bytes, float, None] -"""This type annotation represents Python data structures that can be encoded as AMQP Data""" - class UnmappedType: @@ -309,11 +304,18 @@ class Array: return False +PythonAMQPData = Union[ +dict['PythonAMQPData', 'PythonAMQPData'], +list['PythonAMQPData'], +Described, Array, int, str, symbol, bytes, float, None] +"""This type annotation represents Python data structures that can be encoded as AMQP Data""" + + def _check_type( s: _T, allow_ulong: bool = False, raise_on_error: bool = True -) -> Union[symbol, ulong, _T]: +) -> Union[symbol, _T]: if isinstance(s, symbol): return s if allow_ulong and isinstance(s, ulong): @@ -325,11 +327,11 @@ def _check_type( return s -def _check_is_symbol(s: _T, raise_on_error: bool = True) -> Union[symbol, ulong, _T]: +def _check_is_symbol(s: _T, raise_on_error: bool = True) -> Union[symbol, _T]: return _check_type(s, allow_ulong=False, raise_on_error=raise_on_error) -def _check_is_symbol_or_ulong(s: _T, raise_on_error: bool = True) -> Union[symbol, ulong, _T]: +def _check_is_symbol_or_ulong(s: _T, raise_on_error: bool = True) -> Union[symbol, _T]: return _check_type(s, allow_ulong=True, raise_on_error=raise_on_error) @@ -454,7 +456,7 @@ class AnnotationDict(RestrictedKeyDict): def __init__( self, -e: Optional[Union[dict, list, tuple, Iterable]] = None, +e: Union[dict, list, tuple, Iterable, None] = None, raise_on_error: bool = True, **kwargs ) -> None: @@ -529,7 +531,7 @@ class SymbolList(list): """ Insert a value v at index i """ return super().insert(i, _check_is_symbol(v, self.raise_on_error)) -def __add__(self, t: Iterable[Any]) ->