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 <astitc...@apache.org> 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]) -> 'SymbolList': + def __add__(self, t: Iterable[Any]) -> SymbolList: """ Handles list1 + list2 """ return SymbolList(super().__add__(self._check_list(t)), raise_on_error=self.raise_on_error) @@ -1365,7 +1367,7 @@ class Data: """ return symbol(pn_data_get_symbol(self._data)) - def copy(self, src: 'Data') -> None: + def copy(self, src: Data) -> None: """ Copy the contents of another pn_data_t object. Any values in the data object will be lost. diff --git a/python/proton/_delivery.py b/python/proton/_delivery.py index 41c39d84a..91e4e28db 100644 --- a/python/proton/_delivery.py +++ b/python/proton/_delivery.py @@ -17,6 +17,8 @@ # under the License. # +from __future__ import annotations + from cproton import (PN_ACCEPTED, PN_MODIFIED, PN_RECEIVED, PN_REJECTED, PN_RELEASED, PN_TRANSACTIONAL_STATE, pn_delivery_abort, pn_delivery_aborted, pn_delivery_attachments, pn_delivery_link, pn_delivery_local, pn_delivery_local_state, @@ -182,7 +184,7 @@ class RemoteCustomDisposition(RemoteDisposition): r = self._data return r if r != [] else None - def apply_to(self, local_disposition: 'LocalDisposition'): + def apply_to(self, local_disposition: LocalDisposition): CustomDisposition(self._type, self._data).apply_to(local_disposition) @@ -205,7 +207,7 @@ class RemoteReceivedDisposition(RemoteDisposition): def section_offset(self) -> int: return self._section_offset - def apply_to(self, local_disposition: 'LocalDisposition'): + def apply_to(self, local_disposition: LocalDisposition): ReceivedDisposition(self._section_number, self._section_offset).apply_to(local_disposition) @@ -223,7 +225,7 @@ class RemoteRejectedDisposition(RemoteDisposition): def condition(self) -> Optional[Condition]: return self._condition - def apply_to(self, local_disposition: 'LocalDisposition'): + def apply_to(self, local_disposition: LocalDisposition): RejectedDisposition(self._condition).apply_to(local_disposition) @@ -251,7 +253,7 @@ class RemoteModifiedDisposition(RemoteDisposition): def annotations(self) -> Optional[dict['symbol', 'PythonAMQPData']]: return self._annotations - def apply_to(self, local_disposition: 'LocalDisposition'): + def apply_to(self, local_disposition: LocalDisposition): ModifiedDisposition(self._failed, self._undeliverable, self._annotations).apply_to(local_disposition) @@ -274,7 +276,7 @@ class RemoteTransactionalDisposition(RemoteDisposition): def outcome_type(self): return self._outcome_type - def apply_to(self, local_disposition: 'LocalDisposition'): + def apply_to(self, local_disposition: LocalDisposition): TransactionalDisposition(self._id, self._outcome_type).apply_to(local_disposition) @@ -695,14 +697,14 @@ class Delivery(Wrapper): return _endpoints.Link.wrap(pn_delivery_link(self._impl)) @property - def session(self) -> 'Session': + def session(self) -> Session: """ The :class:`Session` over which the delivery was sent or received. """ return self.link.session @property - def connection(self) -> 'Connection': + def connection(self) -> Connection: """ The :class:`Connection` over which the delivery was sent or received. """ diff --git a/python/proton/_endpoints.py b/python/proton/_endpoints.py index 588a54965..aabe2129e 100644 --- a/python/proton/_endpoints.py +++ b/python/proton/_endpoints.py @@ -21,6 +21,8 @@ The proton.endpoints module """ +from __future__ import annotations + import weakref from cproton import PN_CONFIGURATION, PN_COORDINATOR, PN_DELIVERIES, PN_DIST_MODE_COPY, PN_DIST_MODE_MOVE, \ @@ -200,7 +202,7 @@ class Connection(Endpoint): self._acceptor = None @property - def connection(self) -> 'Connection': + def connection(self) -> Connection: """ Get this connection. """ @@ -222,7 +224,7 @@ class Connection(Endpoint): return err # TODO: Blacklisted API call - def collect(self, collector: 'Collector') -> None: + def collect(self, collector: Collector) -> None: if collector is None: pn_connection_collect(self._impl, None) else: @@ -421,7 +423,7 @@ class Connection(Endpoint): s.terminate() s.update() - def session(self) -> 'Session': + def session(self) -> Session: """ Returns a new session on this connection. @@ -674,7 +676,7 @@ class Session(Endpoint): """ return self.connection.transport - def sender(self, name: str) -> 'Sender': + def sender(self, name: str) -> Sender: """ Create a new :class:`Sender` on this session. @@ -682,7 +684,7 @@ class Session(Endpoint): """ return Sender(pn_sender(self._impl, name)) - def receiver(self, name: str) -> 'Receiver': + def receiver(self, name: str) -> Receiver: """ Create a new :class:`Receiver` on this session. @@ -725,7 +727,7 @@ class Link(Endpoint): get_remote_condition = pn_link_remote_condition get_state = pn_link_state - def __new__(cls, impl) -> 'Link': + def __new__(cls, impl) -> Link: if pn_link_is_sender(impl): return super().__new__(Sender, impl) else: @@ -771,7 +773,7 @@ class Link(Endpoint): pn_link_close(self._impl) @property - def source(self) -> 'Terminus': + def source(self) -> Terminus: """ The source of the link as described by the local peer. The returned object is valid until the link is freed. @@ -779,7 +781,7 @@ class Link(Endpoint): return Terminus(pn_link_source(self._impl)) @property - def target(self) -> 'Terminus': + def target(self) -> Terminus: """ The target of the link as described by the local peer. The returned object is valid until the link is freed. @@ -787,7 +789,7 @@ class Link(Endpoint): return Terminus(pn_link_target(self._impl)) @property - def remote_source(self) -> 'Terminus': + def remote_source(self) -> Terminus: """ The source of the link as described by the remote peer. The returned object is valid until the link is freed. The remote @@ -798,7 +800,7 @@ class Link(Endpoint): return Terminus(pn_link_remote_source(self._impl)) @property - def remote_target(self) -> 'Terminus': + def remote_target(self) -> Terminus: """ The target of the link as described by the remote peer. The returned object is valid until the link is freed. The remote @@ -1171,9 +1173,9 @@ class Sender(Link): @overload def send(self, obj: bytes) -> int: ... @overload - def send(self, obj: 'Message', tag: Optional[str] = None) -> Delivery: ... + def send(self, obj: Message, tag: Optional[str] = None) -> Delivery: ... - def send(self, obj: Union[bytes, 'Message'], tag: Optional[str] = None) -> Union[int, Delivery]: + def send(self, obj: Union[bytes, Message], tag: Optional[str] = None) -> Union[int, Delivery]: """ A convenience method to send objects as message content. @@ -1419,7 +1421,7 @@ class Terminus: """ return Data(pn_terminus_filter(self._impl)) - def copy(self, src: 'Terminus') -> None: + def copy(self, src: Terminus) -> None: """ Copy another terminus object. diff --git a/python/proton/_events.py b/python/proton/_events.py index 2d61d6d07..bc12f0af6 100644 --- a/python/proton/_events.py +++ b/python/proton/_events.py @@ -17,6 +17,8 @@ # under the License. # +from __future__ import annotations + import threading from cproton import PN_CONNECTION_BOUND, PN_CONNECTION_FINAL, PN_CONNECTION_INIT, PN_CONNECTION_LOCAL_CLOSE, \ @@ -47,7 +49,7 @@ class Collector: def __init__(self) -> None: self._impl = pn_collector() - def put(self, obj: Any, etype: 'EventType') -> None: + def put(self, obj: Any, etype: EventType) -> None: pn_collector_put_pyref(self._impl, obj, etype) def peek(self) -> Optional['Event']: @@ -505,14 +507,14 @@ class Event(EventBase): return h @property - def reactor(self) -> 'Container': + def reactor(self) -> Container: """ **Deprecated** - The :class:`reactor.Container` (was reactor) associated with the event. """ return self.container @property - def container(self) -> 'Container': + def container(self) -> Container: """ The :class:`reactor.Container` associated with the event. """ diff --git a/python/proton/_handler.py b/python/proton/_handler.py index 2da8a244a..fcbbea963 100644 --- a/python/proton/_handler.py +++ b/python/proton/_handler.py @@ -17,13 +17,14 @@ # under the License. # +from __future__ import annotations from typing import Any, Callable, Optional, Union from types import TracebackType class LazyHandlers: - def __get__(self, obj: 'Handler', clazz: Any) -> Union['LazyHandlers', list[Any]]: + def __get__(self, obj: Handler, clazz: Any) -> Union[LazyHandlers, list[Any]]: if obj is None: return self ret = [] diff --git a/python/proton/_handlers.py b/python/proton/_handlers.py index 09f958577..c65076141 100644 --- a/python/proton/_handlers.py +++ b/python/proton/_handlers.py @@ -17,6 +17,8 @@ # under the License. # +from __future__ import annotations + import errno import logging import socket @@ -1409,7 +1411,7 @@ class ConnectSelectable(Selectable): def __init__( self, sock: socket.socket, - reactor: 'Container', + reactor: Container, addrs: list[Any], transport: Transport, iohandler: IOHandler diff --git a/python/proton/_io.py b/python/proton/_io.py index 80f026f91..c9b8bc29d 100644 --- a/python/proton/_io.py +++ b/python/proton/_io.py @@ -17,6 +17,8 @@ # under the License. # +from __future__ import annotations + import errno import socket import select @@ -84,7 +86,7 @@ class IO: self._writing = set() self._deadline = None - def add(self, selectable: 'Selectable') -> None: + def add(self, selectable: Selectable) -> None: self._selectables.add(selectable) if selectable.reading: self._reading.add(selectable) @@ -96,7 +98,7 @@ class IO: else: self._deadline = min(selectable.deadline, self._deadline) - def remove(self, selectable: 'Selectable') -> None: + def remove(self, selectable: Selectable) -> None: self._selectables.discard(selectable) self._reading.discard(selectable) self._writing.discard(selectable) @@ -114,7 +116,7 @@ class IO: else: self._deadline = min(sel.deadline, self._deadline) - def update(self, selectable: 'Selectable') -> None: + def update(self, selectable: Selectable) -> None: self._reading.discard(selectable) self._writing.discard(selectable) if selectable.reading: diff --git a/python/proton/_message.py b/python/proton/_message.py index 1dbec39df..f5f37e161 100644 --- a/python/proton/_message.py +++ b/python/proton/_message.py @@ -17,6 +17,8 @@ # under the License. # +from __future__ import annotations + from cproton import PN_DEFAULT_PRIORITY, PN_UUID, PN_OVERFLOW, pn_error_text, pn_message, \ pn_message_annotations, pn_message_body, pn_message_clear, pn_message_decode, \ pn_message_encode, pn_message_error, pn_message_free, pn_message_get_address, pn_message_get_content_encoding, \ @@ -490,7 +492,7 @@ class Message: self._check(pn_message_decode(self._msg, data)) self._post_decode() - def send(self, sender: 'Sender', tag: Optional[str] = None) -> 'Delivery': + def send(self, sender: Sender, tag: Optional[str] = None) -> Delivery: """ Encodes and sends the message content using the specified sender, and, if present, using the specified tag. Upon success, will @@ -509,10 +511,10 @@ class Message: return dlv @overload - def recv(self, link: 'Sender') -> None: + def recv(self, link: Sender) -> None: ... - def recv(self, link: 'Receiver') -> Optional['Delivery']: + def recv(self, link: Receiver) -> Optional[Delivery]: """ Receives and decodes the message content for the current :class:`Delivery` from the link. Upon success it will return the current delivery diff --git a/python/proton/_reactor.py b/python/proton/_reactor.py index dea45d839..bff42508b 100644 --- a/python/proton/_reactor.py +++ b/python/proton/_reactor.py @@ -17,6 +17,8 @@ # under the License. # +from __future__ import annotations + import heapq import json import logging @@ -67,13 +69,13 @@ def _now() -> float: @total_ordering class Task: - def __init__(self, reactor: 'Container', deadline: float, handler: Handler) -> None: + def __init__(self, reactor: Container, deadline: float, handler: Handler) -> None: self._deadline = deadline self._handler = handler self._reactor = reactor self._cancelled = False - def __lt__(self, rhs: 'Task') -> bool: + def __lt__(self, rhs: Task) -> bool: return self._deadline < rhs._deadline def cancel(self) -> None: @@ -84,13 +86,13 @@ class Task: return self._handler @property - def container(self) -> 'Container': + def container(self) -> Container: return self._reactor class TimerSelectable(Selectable): - def __init__(self, reactor: 'Container') -> None: + def __init__(self, reactor: Container) -> None: super().__init__(None, reactor) def readable(self) -> None: @@ -321,7 +323,7 @@ class Reactor: host: str, port: Union[str, Url.Port], handler: Optional[Handler] = None, - ) -> 'Acceptor': + ) -> Acceptor: impl = self._make_handler(handler) a = Acceptor(self, host, int(port), impl) if a: @@ -411,7 +413,7 @@ class EventInjector: self._transport = None self._closed = False - def trigger(self, event: 'ApplicationEvent') -> None: + def trigger(self, event: ApplicationEvent) -> None: """ Request that the given event be dispatched on the event thread of the container to which this EventInjector was added. @@ -498,7 +500,7 @@ class ApplicationEvent(EventBase): self.subject = subject @property - def context(self) -> 'ApplicationEvent': + def context(self) -> ApplicationEvent: """ A reference to this event. """ @@ -528,8 +530,8 @@ class Transaction: def __init__( self, - txn_ctrl: 'Sender', - handler: 'TransactionHandler', + txn_ctrl: Sender, + handler: TransactionHandler, settle_before_discharge: bool = False, ) -> None: self.txn_ctrl = txn_ctrl @@ -563,14 +565,14 @@ class Transaction: self.failed = failed self._discharge = self._send_ctrl(symbol('amqp:discharge:list'), [self.id, failed]) - def _send_ctrl(self, descriptor: 'PythonAMQPData', value: 'PythonAMQPData') -> Delivery: + def _send_ctrl(self, descriptor: PythonAMQPData, value: PythonAMQPData) -> Delivery: delivery = self.txn_ctrl.send(Message(body=Described(descriptor, value))) delivery.transaction = self return delivery def send( self, - sender: 'Sender', + sender: Sender, msg: Message, tag: Optional[str] = None, ) -> Delivery: @@ -698,7 +700,7 @@ class SenderOption(LinkOption): Abstract class for sender options. """ - def apply(self, sender: 'Sender') -> None: + def apply(self, sender: Sender) -> None: """ Set the option on the sender. @@ -715,7 +717,7 @@ class ReceiverOption(LinkOption): Abstract class for receiver options """ - def apply(self, receiver: 'Receiver') -> None: + def apply(self, receiver: Receiver) -> None: """ Set the option on the receiver. @@ -767,7 +769,7 @@ class Filter(ReceiverOption): def __init__(self, filter_set: dict[symbol, Described] = {}) -> None: self.filter_set = filter_set - def apply(self, receiver: 'Receiver') -> None: + def apply(self, receiver: Receiver) -> None: """ Set the filter on the specified receiver. @@ -797,7 +799,7 @@ class DurableSubscription(ReceiverOption): :const:`proton.Terminus.EXPIRE_NEVER`. """ - def apply(self, receiver: 'Receiver'): + def apply(self, receiver: Receiver): """ Set durability on the specified receiver. @@ -815,7 +817,7 @@ class Move(ReceiverOption): mode to :const:`proton.Terminus.DIST_MODE_MOVE`. """ - def apply(self, receiver: 'Receiver'): + def apply(self, receiver: Receiver): """ Set message move semantics on the specified receiver. @@ -832,7 +834,7 @@ class Copy(ReceiverOption): :const:`proton.Terminus.DIST_MODE_COPY`. """ - def apply(self, receiver: 'Receiver'): + def apply(self, receiver: Receiver): """ Set message copy semantics on the specified receiver. @@ -843,7 +845,7 @@ class Copy(ReceiverOption): def _apply_link_options( options: Optional[Union[LinkOption, list[LinkOption]]], - link: Union['Sender', 'Receiver'] + link: Union['Sender', Receiver] ) -> None: if options: if isinstance(options, list): @@ -898,7 +900,7 @@ class GlobalOverrides(Handler): class Acceptor(Handler): - def __init__(self, reactor: 'Container', host: str, port: int, handler: Optional[Handler] = None) -> None: + def __init__(self, reactor: Container, host: str, port: int, handler: Optional[Handler] = None) -> None: self._ssl_domain = None self._reactor = reactor self._handler = handler @@ -1419,7 +1421,7 @@ class Container(Reactor): handler: Optional[Handler] = None, tags: Optional[Callable[[], bytes]] = None, options: Optional[Union['SenderOption', list['SenderOption'], 'LinkOption', list['LinkOption']]] = None - ) -> 'Sender': + ) -> Sender: """ Initiates the establishment of a link over which messages can be sent. @@ -1481,7 +1483,7 @@ class Container(Reactor): dynamic: bool = False, handler: Optional[Handler] = None, options: Optional[Union[ReceiverOption, list[ReceiverOption], LinkOption, list[LinkOption]]] = None - ) -> 'Receiver': + ) -> Receiver: """ Initiates the establishment of a link over which messages can be received (aka a subscription). diff --git a/python/proton/_selectable.py b/python/proton/_selectable.py index 18010c34e..28366f06c 100644 --- a/python/proton/_selectable.py +++ b/python/proton/_selectable.py @@ -17,6 +17,8 @@ # under the License. # +from __future__ import annotations + from typing import Optional, Union, TYPE_CHECKING, Any from ._events import Event @@ -32,8 +34,8 @@ class Selectable: def __init__( self, - delegate: Optional[Union['EventInjector', 'socket']], - reactor: 'Container', + delegate: Optional[Union[EventInjector, socket]], + reactor: Container, ) -> None: self._delegate = delegate self.reading = False @@ -75,8 +77,8 @@ class Selectable: def push_event( self, - context: 'Selectable', - etype: 'EventType', + context: Selectable, + etype: EventType, ) -> None: self._reactor.push_event(context, etype) diff --git a/python/proton/_tracing.py b/python/proton/_tracing.py index 72d786d8c..cdc595047 100644 --- a/python/proton/_tracing.py +++ b/python/proton/_tracing.py @@ -17,6 +17,8 @@ # under the License. # +from __future__ import annotations + import atexit import os import sys diff --git a/python/proton/_transport.py b/python/proton/_transport.py index 7f29ccec6..5a1efc298 100644 --- a/python/proton/_transport.py +++ b/python/proton/_transport.py @@ -17,6 +17,8 @@ # under the License. # +from __future__ import annotations + from typing import Callable, Optional, Union, TYPE_CHECKING from cproton import PN_EOS, PN_SASL_AUTH, PN_SASL_NONE, PN_SASL_OK, PN_SASL_PERM, PN_SASL_SYS, PN_SASL_TEMP, \ @@ -99,7 +101,7 @@ class Transport(Wrapper): cls, mode: Optional[int] = None, impl=None, - ) -> 'Transport': + ) -> Transport: return super().__new__(cls, impl) def __init__( @@ -220,7 +222,7 @@ class Transport(Wrapper): """ return pn_transport_get_user(self._impl) - def bind(self, connection: 'Connection') -> None: + def bind(self, connection: Connection) -> None: """ Assign a connection to the transport. @@ -229,7 +231,7 @@ class Transport(Wrapper): """ self._check(pn_transport_bind(self._impl, connection._impl)) - def bind_nothrow(self, connection: 'Connection') -> None: + def bind_nothrow(self, connection: Connection) -> None: """ Assign a connection to the transport. Any failure is ignored rather than thrown. @@ -467,7 +469,7 @@ class Transport(Wrapper): """ return pn_transport_get_frames_input(self._impl) - def sasl(self) -> 'SASL': + def sasl(self) -> SASL: """ Get the :class:`SASL` object associated with this transport. @@ -477,7 +479,7 @@ class Transport(Wrapper): self._sasl = SASL(self) return self._sasl - def ssl(self, domain: Optional['SSLDomain'] = None, session_details: Optional['SSLSessionDetails'] = None) -> 'SSL': + def ssl(self, domain: Optional['SSLDomain'] = None, session_details: Optional['SSLSessionDetails'] = None) -> SSL: """ Get the :class:`SSL` session associated with this transport. If not set, then a new session will be created using ``domain`` and @@ -504,12 +506,12 @@ class Transport(Wrapper): return cond2obj(pn_transport_condition(self._impl)) @condition.setter - def condition(self, cond: 'Condition') -> None: + def condition(self, cond: Condition) -> None: pn_cond = pn_transport_condition(self._impl) obj2cond(cond, pn_cond) @property - def connection(self) -> 'Connection': + def connection(self) -> Connection: """The connection bound to this transport.""" from . import _endpoints return _endpoints.Connection.wrap(pn_transport_connection(self._impl)) @@ -549,7 +551,7 @@ class SASL: """ return pn_sasl_extended() - def __new__(cls, transport: Transport) -> 'SASL': + def __new__(cls, transport: Transport) -> SASL: if not transport._sasl: sasl = super().__new__(cls) sasl._sasl = pn_sasl(transport._impl) @@ -866,7 +868,7 @@ class SSL: transport: Transport, domain: SSLDomain, session_details: Optional['SSLSessionDetails'] = None - ) -> 'SSL': + ) -> SSL: """Enforce a singleton SSL object per Transport""" if transport._ssl: # unfortunately, we've combined the allocation and the configuration in a diff --git a/python/proton/_utils.py b/python/proton/_utils.py index 866bdf90f..c0b347e9d 100644 --- a/python/proton/_utils.py +++ b/python/proton/_utils.py @@ -17,6 +17,8 @@ # under the License. # +from __future__ import annotations + import collections import time import threading @@ -42,7 +44,7 @@ if TYPE_CHECKING: class BlockingLink: - def __init__(self, connection: 'BlockingConnection', link: Union['Sender', 'Receiver']) -> None: + def __init__(self, connection: BlockingConnection, link: Union[Sender, Receiver]) -> None: self.connection = connection self.link = link self.connection.wait(lambda: not (self.link.state & Endpoint.REMOTE_UNINIT), @@ -98,7 +100,7 @@ class BlockingSender(BlockingLink): :meth:`BlockingConnection.create_sender`. """ - def __init__(self, connection: 'BlockingConnection', sender: 'Sender') -> None: + def __init__(self, connection: BlockingConnection, sender: Sender) -> None: 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... @@ -109,9 +111,9 @@ class BlockingSender(BlockingLink): def send( self, - msg: 'Message', + msg: Message, timeout: Union[None, Literal[False], float] = False, - error_states: Optional[list['DispositionType']] = None, + error_states: Optional[list[DispositionType]] = None, ) -> Delivery: """ Blocking send which will return only when the send is complete @@ -143,23 +145,23 @@ class Fetcher(MessagingHandler): A message handler for blocking receivers. """ - def __init__(self, connection: 'Connection', prefetch: int): + def __init__(self, connection: Connection, prefetch: int): super().__init__(prefetch=prefetch, auto_accept=False) self.connection = connection self.incoming = collections.deque([]) self.unsettled = collections.deque([]) - def on_message(self, event: 'Event') -> None: + def on_message(self, event: Event) -> None: self.incoming.append((event.message, event.delivery)) self.connection.container.yield_() # Wake up the wait() loop to handle the message. - def on_link_error(self, event: 'Event') -> None: + def on_link_error(self, event: Event) -> None: if event.link.state & Endpoint.LOCAL_ACTIVE: event.link.close() if not self.connection.closing: raise LinkDetached(event.link) - def on_connection_error(self, event: 'Event') -> None: + def on_connection_error(self, event: Event) -> None: if not self.connection.closing: raise ConnectionClosed(event.connection) @@ -171,7 +173,7 @@ class Fetcher(MessagingHandler): """ return len(self.incoming) - def pop(self) -> 'Message': + def pop(self) -> Message: """ Get the next available incoming message. If the message is unsettled, its delivery object is moved onto the unsettled queue, and can be settled with @@ -203,8 +205,8 @@ class BlockingReceiver(BlockingLink): def __init__( self, - connection: 'BlockingConnection', - receiver: 'Receiver', + connection: BlockingConnection, + receiver: Receiver, fetcher: Optional[Fetcher], credit: int = 1 ) -> None: @@ -231,7 +233,7 @@ class BlockingReceiver(BlockingLink): def receive( self, timeout: Union[None, Literal[False], float] = False - ) -> 'Message': + ) -> Message: """ Blocking receive call which will return only when a message is received or a timeout (if supplied) occurs. @@ -321,7 +323,7 @@ class ConnectionClosed(ConnectionException): :param connection: The connection which closed unexpectedly. """ - def __init__(self, connection: 'Connection') -> None: + def __init__(self, connection: Connection) -> None: self.connection = connection txt = "Connection %s closed" % connection.hostname if connection.remote_condition: @@ -522,7 +524,7 @@ class BlockingConnection(Handler): raise ConnectionException( "Connection %s disconnected: %s" % (self.url, self.disconnected)) - def on_link_remote_close(self, event: 'Event') -> None: + def on_link_remote_close(self, event: Event) -> None: """ Event callback for when the remote terminus closes. """ @@ -531,7 +533,7 @@ class BlockingConnection(Handler): if not self.closing: raise LinkDetached(event.link) - def on_connection_remote_close(self, event: 'Event') -> None: + def on_connection_remote_close(self, event: Event) -> None: """ Event callback for when the link peer closes the connection. """ @@ -540,13 +542,13 @@ class BlockingConnection(Handler): if not self.closing: raise ConnectionClosed(event.connection) - def on_transport_tail_closed(self, event: 'Event') -> None: + def on_transport_tail_closed(self, event: Event) -> None: self.on_transport_closed(event) - def on_transport_head_closed(self, event: 'Event') -> None: + def on_transport_head_closed(self, event: Event) -> None: self.on_transport_closed(event) - def on_transport_closed(self, event: 'Event') -> None: + def on_transport_closed(self, event: Event) -> None: if not self.closing: self.disconnected = event.transport.condition or "unknown" @@ -590,7 +592,7 @@ class SyncRequestResponse(IncomingMessageHandler): self.receiver = self.connection.create_receiver(None, dynamic=True, credit=1, handler=self) self.response = None - def call(self, request: 'Message') -> 'Message': + def call(self, request: Message) -> Message: """ Send a request message, wait for and return the response message. @@ -619,7 +621,7 @@ class SyncRequestResponse(IncomingMessageHandler): """ return self.receiver.remote_source.address - def on_message(self, event: 'Event') -> None: + def on_message(self, event: Event) -> None: """ Called when we receive a message for our receiver. diff --git a/python/proton/_wrapper.py b/python/proton/_wrapper.py index 2a4b7e682..68b7b4c36 100644 --- a/python/proton/_wrapper.py +++ b/python/proton/_wrapper.py @@ -17,6 +17,8 @@ # under the License. # +from __future__ import annotations + from typing import Any, Callable, ClassVar, Optional from cproton import addressof, isnull, pn_incref, pn_decref, \ @@ -48,13 +50,13 @@ class Wrapper: __slots__ = ["_impl", "_attrs"] @classmethod - def wrap(cls, impl: Any) -> Optional['Wrapper']: + def wrap(cls, impl: Any) -> Optional[Wrapper]: if isnull(impl): return None else: return cls(impl) - def __new__(cls, impl: Any = None) -> 'Wrapper': + def __new__(cls, impl: Any = None) -> Wrapper: attrs = None try: if impl is None: --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@qpid.apache.org For additional commands, e-mail: commits-h...@qpid.apache.org