This is an automated email from the ASF dual-hosted git repository. astitcher pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/qpid-proton.git
commit 5193b337468228f579a35191b9e55e7c4f0146fb Author: Andrew Stitcher <astitc...@apache.org> 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): """ Representation of PropertyDict """ - return 'PropertyDict(%s)' % super(PropertyDict, self).__repr__() + return 'PropertyDict(%s)' % super().__repr__() class AnnotationDict(RestrictedKeyDict): @@ -458,11 +458,11 @@ class AnnotationDict(RestrictedKeyDict): raise_on_error: bool = True, **kwargs ) -> None: - super(AnnotationDict, self).__init__(_check_is_symbol_or_ulong, e, raise_on_error, **kwargs) + super().__init__(_check_is_symbol_or_ulong, e, raise_on_error, **kwargs) def __repr__(self): """ Representation of AnnotationDict """ - return 'AnnotationDict(%s)' % super(AnnotationDict, self).__repr__() + return 'AnnotationDict(%s)' % super().__repr__() class SymbolList(list): @@ -499,7 +499,7 @@ class SymbolList(list): t: Optional[list[Any]] = None, raise_on_error: bool = True ) -> None: - super(SymbolList, self).__init__() + super().__init__() self.raise_on_error = raise_on_error if isinstance(t, (str, symbol)): self.append(t) @@ -519,23 +519,23 @@ class SymbolList(list): def append(self, v: str) -> None: """ Add a single value v to the end of the list """ - return super(SymbolList, self).append(_check_is_symbol(v, self.raise_on_error)) + return super().append(_check_is_symbol(v, self.raise_on_error)) def extend(self, t: Iterable[str]) -> None: """ Add all elements of an iterable t to the end of the list """ - return super(SymbolList, self).extend(self._check_list(t)) + return super().extend(self._check_list(t)) def insert(self, i: int, v: str) -> None: """ Insert a value v at index i """ - return super(SymbolList, self).insert(i, _check_is_symbol(v, self.raise_on_error)) + return super().insert(i, _check_is_symbol(v, self.raise_on_error)) def __add__(self, t: Iterable[Any]) -> 'SymbolList': """ Handles list1 + list2 """ - return SymbolList(super(SymbolList, self).__add__(self._check_list(t)), raise_on_error=self.raise_on_error) + return SymbolList(super().__add__(self._check_list(t)), raise_on_error=self.raise_on_error) def __iadd__(self, t): """ Handles list1 += list2 """ - return super(SymbolList, self).__iadd__(self._check_list(t)) + return super().__iadd__(self._check_list(t)) def __eq__(self, other): """ Handles list1 == list2 """ @@ -543,11 +543,11 @@ class SymbolList(list): def __setitem__(self, i: int, t: Any) -> None: """ Handles list[i] = v """ - return super(SymbolList, self).__setitem__(i, _check_is_symbol(t, self.raise_on_error)) + return super().__setitem__(i, _check_is_symbol(t, self.raise_on_error)) def __repr__(self) -> str: """ Representation of SymbolList """ - return 'SymbolList(%s)' % super(SymbolList, self).__repr__() + return 'SymbolList(%s)' % super().__repr__() class Data: diff --git a/python/proton/_endpoints.py b/python/proton/_endpoints.py index 11087055a..588a54965 100644 --- a/python/proton/_endpoints.py +++ b/python/proton/_endpoints.py @@ -1262,7 +1262,7 @@ class Receiver(Link): return pn_link_draining(self._impl) -class Terminus(object): +class Terminus: """ A source or target for messages. """ diff --git a/python/proton/_events.py b/python/proton/_events.py index e9995b9dc..2d61d6d07 100644 --- a/python/proton/_events.py +++ b/python/proton/_events.py @@ -79,7 +79,7 @@ if "TypeExtender" not in globals(): self.number += 1 -class EventType(object): +class EventType: """ Connects an event number to an event name, and is used internally by :class:`Event` to represent all known @@ -129,7 +129,7 @@ def _dispatch(handler: Any, method: str, *args) -> None: handler.on_unhandled(method, *args) -class EventBase(object): +class EventBase: def __init__(self, type: EventType) -> None: self._type = type diff --git a/python/proton/_handler.py b/python/proton/_handler.py index 90aec7cdc..2da8a244a 100644 --- a/python/proton/_handler.py +++ b/python/proton/_handler.py @@ -22,7 +22,7 @@ from typing import Any, Callable, Optional, Union from types import TracebackType -class LazyHandlers(object): +class LazyHandlers: def __get__(self, obj: 'Handler', clazz: Any) -> Union['LazyHandlers', list[Any]]: if obj is None: return self @@ -31,7 +31,7 @@ class LazyHandlers(object): return ret -class Handler(object): +class Handler: """ An abstract handler for events which supports child handlers. """ diff --git a/python/proton/_handlers.py b/python/proton/_handlers.py index e2d194db2..09f958577 100644 --- a/python/proton/_handlers.py +++ b/python/proton/_handlers.py @@ -210,7 +210,7 @@ class Release(ProtonException): pass -class Acking(object): +class Acking: """ A class containing methods for handling received messages. """ @@ -967,7 +967,7 @@ class MessagingHandler(Handler, Acking): pass -class TransactionHandler(object): +class TransactionHandler: """ The interface for transaction handlers - ie objects that want to be notified of state changes related to a transaction. @@ -1048,7 +1048,7 @@ class TransactionalClientHandler(MessagingHandler, TransactionHandler): auto_settle: bool = True, peer_close_is_error: bool = False ) -> None: - super(TransactionalClientHandler, self).__init__(prefetch, auto_accept, auto_settle, peer_close_is_error) + super().__init__(prefetch, auto_accept, auto_settle, peer_close_is_error) def accept(self, delivery: Delivery, transaction: Optional['Transaction'] = None): """ @@ -1064,7 +1064,7 @@ class TransactionalClientHandler(MessagingHandler, TransactionHandler): if transaction: transaction.accept(delivery) else: - super(TransactionalClientHandler, self).accept(delivery) + super().accept(delivery) class FlowController(Handler): @@ -1414,7 +1414,7 @@ class ConnectSelectable(Selectable): transport: Transport, iohandler: IOHandler ) -> None: - super(ConnectSelectable, self).__init__(sock, reactor) + super().__init__(sock, reactor) self.writing = True self._addrs = addrs self._transport = transport diff --git a/python/proton/_io.py b/python/proton/_io.py index d33307711..80f026f91 100644 --- a/python/proton/_io.py +++ b/python/proton/_io.py @@ -30,7 +30,7 @@ if TYPE_CHECKING: PN_INVALID_SOCKET = -1 -class IO(object): +class IO: @staticmethod def _setupsocket(s: socket.socket) -> None: @@ -76,7 +76,7 @@ class IO(object): time.sleep(t) return - class Selector(object): + class Selector: def __init__(self) -> None: self._selectables = set() diff --git a/python/proton/_message.py b/python/proton/_message.py index 657962d7d..1dbec39df 100644 --- a/python/proton/_message.py +++ b/python/proton/_message.py @@ -44,7 +44,7 @@ if TYPE_CHECKING: from proton._data import Described, PythonAMQPData -class Message(object): +class Message: """The :py:class:`Message` class is a mutable holder of message content. :ivar instructions: delivery instructions for the message ("Delivery Annotations" in the AMQP 1.0 spec) diff --git a/python/proton/_reactor.py b/python/proton/_reactor.py index 3b4d71a7d..dea45d839 100644 --- a/python/proton/_reactor.py +++ b/python/proton/_reactor.py @@ -65,7 +65,7 @@ def _now() -> float: @total_ordering -class Task(object): +class Task: def __init__(self, reactor: 'Container', deadline: float, handler: Handler) -> None: self._deadline = deadline @@ -91,7 +91,7 @@ class Task(object): class TimerSelectable(Selectable): def __init__(self, reactor: 'Container') -> None: - super(TimerSelectable, self).__init__(None, reactor) + super().__init__(None, reactor) def readable(self) -> None: pass @@ -105,7 +105,7 @@ class TimerSelectable(Selectable): self.update() -class Reactor(object): +class Reactor: def __init__(self, *handlers, **kwargs) -> None: self._previous = PN_EVENT_NONE @@ -395,7 +395,7 @@ class Reactor(object): self._collector.put(obj, etype) -class EventInjector(object): +class EventInjector: """ Can be added to a :class:`Container` to allow events to be triggered by an external thread but handled on the event thread associated with @@ -484,7 +484,7 @@ class ApplicationEvent(EventBase): except KeyError: eventtype = EventType(typename) self.TYPES[typename] = eventtype - super(ApplicationEvent, self).__init__(eventtype) + super().__init__(eventtype) self.connection = connection self.session = session self.link = link @@ -509,7 +509,7 @@ class ApplicationEvent(EventBase): return "%s(%s)" % (self.type, ", ".join([str(o) for o in objects if o is not None])) -class Transaction(object): +class Transaction: """ Tracks the state of an AMQP 1.0 local transaction. In typical usage, this object is not created directly, but is obtained through the event returned @@ -637,7 +637,7 @@ class Transaction(object): self._clear_pending() -class LinkOption(object): +class LinkOption: """ Abstract interface for link configuration options """ @@ -785,7 +785,7 @@ class Selector(Filter): """ def __init__(self, value: str, name: str = 'selector') -> None: - super(Selector, self).__init__({symbol(name): Described( + super().__init__({symbol(name): Described( symbol('apache.org:selector-filter:string'), value)}) @@ -868,7 +868,7 @@ def _get_attr(target: Any, name: str) -> Optional[Any]: return None -class SessionPerConnection(object): +class SessionPerConnection: def __init__(self) -> None: self._default_session = None @@ -960,7 +960,7 @@ def delay_iter( delay = min(max_delay, factor * delay) -class Backoff(object): +class Backoff: """ A reconnect strategy involving an increasing delay between retries, up to a maximum or 10 seconds. Repeated calls @@ -985,7 +985,7 @@ def make_backoff_wrapper( wrap it in an iterable that returns an iterator suitable for the new backoff approach otherwise assume it is fine as it is! """ - class WrappedBackoff(object): + class WrappedBackoff: def __init__(self, backoff): self.backoff = backoff @@ -1001,7 +1001,7 @@ def make_backoff_wrapper( return backoff -class Urls(object): +class Urls: def __init__(self, values: list[Union[Url, str]]) -> None: self.values = [Url(v) for v in values] @@ -1117,7 +1117,7 @@ class _Connector(Handler): self._next_url = None -class SSLConfig(object): +class SSLConfig: def __init__(self) -> None: self.client = SSLDomain(SSLDomain.MODE_CLIENT) self.server = SSLDomain(SSLDomain.MODE_SERVER) @@ -1182,7 +1182,7 @@ class Container(Reactor): """ def __init__(self, *handlers, **kwargs) -> None: - super(Container, self).__init__(*handlers, **kwargs) + super().__init__(*handlers, **kwargs) if "impl" not in kwargs: try: self.ssl = SSLConfig() @@ -1550,7 +1550,7 @@ class Container(Reactor): if not _get_attr(context, '_txn_ctrl'): class InternalTransactionHandler(OutgoingMessageHandler): def __init__(self): - super(InternalTransactionHandler, self).__init__(auto_settle=True) + super().__init__(auto_settle=True) def on_settled(self, event): if hasattr(event.delivery, "transaction"): diff --git a/python/proton/_selectable.py b/python/proton/_selectable.py index 12645fde4..18010c34e 100644 --- a/python/proton/_selectable.py +++ b/python/proton/_selectable.py @@ -28,7 +28,7 @@ if TYPE_CHECKING: from socket import socket -class Selectable(object): +class Selectable: def __init__( self, diff --git a/python/proton/_transport.py b/python/proton/_transport.py index a4e41afbb..7f29ccec6 100644 --- a/python/proton/_transport.py +++ b/python/proton/_transport.py @@ -709,7 +709,7 @@ class SASL: pn_sasl_config_path(self._sasl, path) -class SSLDomain(object): +class SSLDomain: """ An SSL configuration domain, used to hold the SSL configuration for one or more SSL sessions. @@ -839,7 +839,7 @@ class SSLDomain(object): pn_ssl_domain_free(self._domain) -class SSL(object): +class SSL: """ An SSL session associated with a transport. A transport must have an SSL object in order to "speak" SSL over its connection. @@ -878,7 +878,7 @@ class SSL(object): if different_domain or different_session_details: raise SSLException("Cannot re-configure existing SSL object!") else: - obj = super(SSL, cls).__new__(cls) + obj = super().__new__(cls) obj._domain = domain obj._session_details = session_details session_id = None @@ -1161,7 +1161,7 @@ class SSL(object): self._check(pn_ssl_set_peer_hostname(self._ssl, hostname)) -class SSLSessionDetails(object): +class SSLSessionDetails: """ Unique identifier for the SSL session. Used to resume previous session on a new SSL connection. diff --git a/python/proton/_url.py b/python/proton/_url.py index 278b35fdd..5741afde3 100644 --- a/python/proton/_url.py +++ b/python/proton/_url.py @@ -22,7 +22,7 @@ import socket from urllib.parse import urlparse, urlunparse, quote, unquote -class Url(object): +class Url: """ **DEPRECATED** Simple URL parser/constructor. @@ -71,7 +71,7 @@ class Url(object): """ :param value: integer port number or string service name. """ - port = super(Url.Port, cls).__new__(cls, cls._port_int(value)) + port = super().__new__(cls, cls._port_int(value)) setattr(port, 'name', str(value)) return port diff --git a/python/proton/_utils.py b/python/proton/_utils.py index 1ef79d6b5..866bdf90f 100644 --- a/python/proton/_utils.py +++ b/python/proton/_utils.py @@ -99,7 +99,7 @@ class BlockingSender(BlockingLink): """ def __init__(self, connection: 'BlockingConnection', sender: 'Sender') -> None: - super(BlockingSender, self).__init__(connection, sender) + super().__init__(connection, sender) if self.link.target and self.link.target.address and self.link.target.address != self.link.remote_target.address: # this may be followed by a detach, which may contain an error condition, so wait a little... self._waitForClose() @@ -144,7 +144,7 @@ class Fetcher(MessagingHandler): """ def __init__(self, connection: 'Connection', prefetch: int): - super(Fetcher, self).__init__(prefetch=prefetch, auto_accept=False) + super().__init__(prefetch=prefetch, auto_accept=False) self.connection = connection self.incoming = collections.deque([]) self.unsettled = collections.deque([]) @@ -208,7 +208,7 @@ class BlockingReceiver(BlockingLink): fetcher: Optional[Fetcher], credit: int = 1 ) -> None: - super(BlockingReceiver, self).__init__(connection, receiver) + super().__init__(connection, receiver) if self.link.source and self.link.source.address and self.link.source.address != self.link.remote_source.address: # this may be followed by a detach, which may contain an error condition, so wait a little... self._waitForClose() @@ -310,7 +310,7 @@ class LinkDetached(LinkException): else: txt += " by peer" self.condition = None - super(LinkDetached, self).__init__(txt) + super().__init__(txt) class ConnectionClosed(ConnectionException): @@ -330,7 +330,7 @@ class ConnectionClosed(ConnectionException): else: txt += " by peer" self.condition = None - super(ConnectionClosed, self).__init__(txt) + super().__init__(txt) class BlockingConnection(Handler): @@ -581,7 +581,7 @@ class SyncRequestResponse(IncomingMessageHandler): correlation_id = AtomicCount() def __init__(self, connection: BlockingConnection, address: Optional[str] = None) -> None: - super(SyncRequestResponse, self).__init__() + super().__init__() self.connection = connection self.address = address self.sender = self.connection.create_sender(self.address) diff --git a/python/tests/proton_tests/common.py b/python/tests/proton_tests/common.py index 3aec7cc74..465a2ca92 100644 --- a/python/tests/proton_tests/common.py +++ b/python/tests/proton_tests/common.py @@ -190,7 +190,7 @@ class Skipped(SkipTest): skipped = True -class TestServer(object): +class TestServer: """ Base class for creating test-specific message servers. """ @@ -256,7 +256,7 @@ class TestServer(object): # -class MessengerApp(object): +class MessengerApp: """ Interface to control a MessengerApp """ def __init__(self): diff --git a/python/tests/proton_tests/ssl.py b/python/tests/proton_tests/ssl.py index a16adad1f..797f29117 100644 --- a/python/tests/proton_tests/ssl.py +++ b/python/tests/proton_tests/ssl.py @@ -55,7 +55,7 @@ class SslTest(common.Test): self.server_domain = None self.client_domain = None - class SslTestConnection(object): + class SslTestConnection: """ Represents a single SSL connection. """ --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@qpid.apache.org For additional commands, e-mail: commits-h...@qpid.apache.org