This is an automated email from the ASF dual-hosted git repository. jdanek pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/qpid-proton.git
The following commit(s) were added to refs/heads/main by this push: new b329a0c PROTON-2453 fix socket.socket type annotations and add more types (#358) b329a0c is described below commit b329a0c16778d9f24c585550b5a646bced97eccb Author: Jiri Daněk <jda...@redhat.com> AuthorDate: Sun Mar 13 17:31:04 2022 +0100 PROTON-2453 fix socket.socket type annotations and add more types (#358) --- python/proton/_data.py | 4 +- python/proton/_delivery.py | 130 ++++++++++++++++++++------------------------- python/proton/_events.py | 27 +++++----- python/proton/_handlers.py | 9 +++- python/proton/_io.py | 10 ++-- python/proton/_reactor.py | 56 +++++++++---------- 6 files changed, 115 insertions(+), 121 deletions(-) diff --git a/python/proton/_data.py b/python/proton/_data.py index 43c0d76..05294e0 100644 --- a/python/proton/_data.py +++ b/python/proton/_data.py @@ -18,7 +18,7 @@ # import uuid -from typing import Callable, List, Tuple, Union, Optional, Any, Dict, Iterable, overload, TypeVar +from typing import Callable, List, Tuple, Union, Optional, Any, Dict, Iterable, TypeVar try: from typing import Literal except ImportError: @@ -27,7 +27,7 @@ except ImportError: def __getitem__(self, item): pass - class Literal(metaclass=GenericMeta): + class Literal(metaclass=GenericMeta): # type: ignore[no-redef] pass from cproton import PN_ARRAY, PN_BINARY, PN_BOOL, PN_BYTE, PN_CHAR, PN_DECIMAL128, PN_DECIMAL32, PN_DECIMAL64, \ diff --git a/python/proton/_delivery.py b/python/proton/_delivery.py index fa793c8..3badb56 100644 --- a/python/proton/_delivery.py +++ b/python/proton/_delivery.py @@ -30,9 +30,11 @@ from ._condition import cond2obj, obj2cond from ._data import dat2obj, obj2dat from ._wrapper import Wrapper -from typing import Dict, List, Optional, Type, Union, TYPE_CHECKING +from typing import Dict, List, Optional, Type, Union, TYPE_CHECKING, Any + if TYPE_CHECKING: from ._condition import Condition + from ._data import PythonAMQPData, symbol from ._endpoints import Receiver, Sender # circular import from ._reactor import Connection, Session, Transport @@ -133,68 +135,45 @@ class Disposition(object): """ return DispositionType.get(pn_disposition_type(self._impl)) - def _get_section_number(self): + @property + def section_number(self) -> int: + """The section number associated with a disposition.""" return pn_disposition_get_section_number(self._impl) - def _set_section_number(self, n): + @section_number.setter + def section_number(self, n: int) -> None: pn_disposition_set_section_number(self._impl, n) - section_number = property(_get_section_number, _set_section_number, doc=""" - The section number associated with a disposition. - - :type: ``int`` - """) - - def _get_section_offset(self): + @property + def section_offset(self) -> int: + """The section offset associated with a disposition.""" return pn_disposition_get_section_offset(self._impl) - def _set_section_offset(self, n): + @section_offset.setter + def section_offset(self, n: int) -> None: pn_disposition_set_section_offset(self._impl, n) - section_offset = property(_get_section_offset, _set_section_offset, doc=""" - The section offset associated with a disposition. - - :type: ``int`` - """) - - def _get_failed(self): + @property + def failed(self) -> bool: + """The failed flag for this disposition.""" return pn_disposition_is_failed(self._impl) - def _set_failed(self, b): + @failed.setter + def failed(self, b: bool) -> None: pn_disposition_set_failed(self._impl, b) - failed = property(_get_failed, _set_failed, doc=""" - The failed flag for this disposition. - - :type: ``bool`` - """) - - def _get_undeliverable(self): + @property + def undeliverable(self) -> bool: + """The undeliverable flag for this disposition.""" return pn_disposition_is_undeliverable(self._impl) - def _set_undeliverable(self, b): + @undeliverable.setter + def undeliverable(self, b: bool) -> None: pn_disposition_set_undeliverable(self._impl, b) - undeliverable = property(_get_undeliverable, _set_undeliverable, doc=""" - The undeliverable flag for this disposition. - - :type: ``bool`` - """) - - def _get_data(self): - if self.local: - return self._data - else: - return dat2obj(pn_disposition_data(self._impl)) - - def _set_data(self, obj): - if self.local: - self._data = obj - else: - raise AttributeError("data attribute is read-only") - - data = property(_get_data, _set_data, doc=""" - Access the disposition as a :class:`Data` object. + @property + def data(self) -> Optional[List[int]]: + """Access the disposition as a :class:`Data` object. Dispositions are an extension point in the AMQP protocol. The disposition interface provides setters/getters for those @@ -204,24 +183,22 @@ class Disposition(object): The :class:`Data` object returned by this operation is valid until the parent delivery is settled. - - :type: :class:`Data` - """) - - def _get_annotations(self): + """ if self.local: - return self._annotations + return self._data else: - return dat2obj(pn_disposition_annotations(self._impl)) + return dat2obj(pn_disposition_data(self._impl)) - def _set_annotations(self, obj): + @data.setter + def data(self, obj: List[int]) -> None: if self.local: - self._annotations = obj + self._data = obj else: - raise AttributeError("annotations attribute is read-only") + raise AttributeError("data attribute is read-only") - annotations = property(_get_annotations, _set_annotations, doc=""" - The annotations associated with a disposition. + @property + def annotations(self) -> Optional[Dict['symbol', 'PythonAMQPData']]: + """The annotations associated with a disposition. The :class:`Data` object retrieved by this operation may be modified prior to updating a delivery. When a delivery is updated, the @@ -232,33 +209,40 @@ class Disposition(object): The :class:`Data` object returned by this operation is valid until the parent delivery is settled. - - :type: :class:`Data` - """) - - def _get_condition(self): + """ if self.local: - return self._condition + return self._annotations else: - return cond2obj(pn_disposition_condition(self._impl)) + return dat2obj(pn_disposition_annotations(self._impl)) - def _set_condition(self, obj): + @annotations.setter + def annotations(self, obj: Dict[str, 'PythonAMQPData']) -> None: if self.local: - self._condition = obj + self._annotations = obj else: - raise AttributeError("condition attribute is read-only") + raise AttributeError("annotations attribute is read-only") - condition = property(_get_condition, _set_condition, doc=""" - The condition object associated with a disposition. + @property + def condition(self) -> Optional['Condition']: + """The condition object associated with a disposition. The :class:`Condition` object retrieved by this operation may be modified prior to updating a delivery. When a delivery is updated, the condition described by the disposition is reported to the peer if applicable to the current delivery state, e.g. states such as :const:`REJECTED`. + """ + if self.local: + return self._condition + else: + return cond2obj(pn_disposition_condition(self._impl)) - :type: :class:`Condition` - """) + @condition.setter + def condition(self, obj: 'Condition') -> None: + if self.local: + self._condition = obj + else: + raise AttributeError("condition attribute is read-only") class Delivery(Wrapper): diff --git a/python/proton/_events.py b/python/proton/_events.py index d05c819..b3c10c2 100644 --- a/python/proton/_events.py +++ b/python/proton/_events.py @@ -33,13 +33,14 @@ from cproton import PN_CONNECTION_BOUND, PN_CONNECTION_FINAL, PN_CONNECTION_INIT from ._delivery import Delivery from ._endpoints import Connection, Link, Session from ._transport import Transport -from typing import Any, List, Optional, Union, TYPE_CHECKING, Callable +from typing import Any, List, Optional, Union, TYPE_CHECKING, Callable, Tuple, Type if TYPE_CHECKING: from ._reactor import Container from ._endpoints import Receiver, Sender from ._handlers import ConnectSelectable from ._selectable import Selectable + from types import TracebackType class Collector: @@ -122,7 +123,7 @@ class EventType(object): return self.name -def _dispatch(handler, method, *args): +def _dispatch(handler: Any, method: str, *args) -> None: m = getattr(handler, method, None) if m: m(*args) @@ -141,12 +142,8 @@ class EventBase(object): return self._type @property - def handler(self): - """ - The handler for this event type. Not implemented, always returns ``None``. - - :type: ``None`` - """ + def handler(self) -> Optional['Handler']: + """The handler for this event type. Not implemented, always returns ``None``.""" return None def dispatch(self, handler: 'Handler', type: Optional[EventType] = None) -> None: @@ -464,7 +461,7 @@ class Event(EventBase): return self._clsname @property - def context(self): + def context(self) -> Union[Optional[Any], Connection, Session, Link, Delivery, Transport]: """ The context object associated with the event. @@ -478,7 +475,7 @@ class Event(EventBase): return self._context @property - def handler(self): + def handler(self) -> Optional['Handler']: """ The handler for this event. The handler is determined by looking at the following in order: @@ -525,7 +522,7 @@ class Event(EventBase): """ return self._transport._reactor - def __getattr__(self, name): + def __getattr__(self, name: str) -> Any: """ This will look for a property of the event as an attached context object of the same type as the property (but lowercase) @@ -607,7 +604,7 @@ class Event(EventBase): class LazyHandlers(object): - def __get__(self, obj, clazz): + def __get__(self, obj: 'Handler', clazz: Any) -> Union['LazyHandlers', List[Any]]: if obj is None: return self ret = [] @@ -622,7 +619,11 @@ class Handler(object): handlers = LazyHandlers() # TODO What to do with on_error? - def add(self, handler, on_error=None): + def add( + self, + handler: Any, + on_error: Optional[Callable[[Tuple[Type[BaseException], BaseException, 'TracebackType']], None]] = None, + ) -> None: """ Add a child handler diff --git a/python/proton/_handlers.py b/python/proton/_handlers.py index ac783d0..c9d1ec2 100644 --- a/python/proton/_handlers.py +++ b/python/proton/_handlers.py @@ -1328,7 +1328,14 @@ class IOHandler(Handler): class ConnectSelectable(Selectable): - def __init__(self, sock, reactor, addrs, transport, iohandler): + def __init__( + self, + sock: socket.socket, + reactor: 'Container', + addrs: List[Any], + transport: Transport, + iohandler: IOHandler + ) -> None: super(ConnectSelectable, self).__init__(sock, reactor) self.writing = True self._addrs = addrs diff --git a/python/proton/_io.py b/python/proton/_io.py index 5628d1d..482f327 100644 --- a/python/proton/_io.py +++ b/python/proton/_io.py @@ -35,16 +35,16 @@ PN_INVALID_SOCKET = -1 class IO(object): @staticmethod - def _setupsocket(s: socket) -> None: + def _setupsocket(s: socket.socket) -> None: s.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, True) s.setblocking(False) @staticmethod - def close(s: socket) -> None: + def close(s: socket.socket) -> None: s.close() @staticmethod - def listen(host, port): + def listen(host, port) -> socket.socket: s = socket.socket() IO._setupsocket(s) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True) @@ -53,13 +53,13 @@ class IO(object): return s @staticmethod - def accept(s: socket): + def accept(s: socket.socket): n = s.accept() IO._setupsocket(n[0]) return n @staticmethod - def connect(addr): + def connect(addr) -> socket.socket: s = socket.socket(addr[0], addr[1], addr[2]) IO._setupsocket(s) try: diff --git a/python/proton/_reactor.py b/python/proton/_reactor.py index ec01deb..0a84d16 100644 --- a/python/proton/_reactor.py +++ b/python/proton/_reactor.py @@ -23,7 +23,8 @@ import logging import re import os import queue -from typing import Any, Dict, Iterator, Optional, List, Union, Callable, TYPE_CHECKING +from typing import Any, Dict, Iterator, Optional, List, Union, Callable, TYPE_CHECKING, Tuple, Type + try: from typing import Literal except ImportError: @@ -57,8 +58,10 @@ from ._selectable import Selectable if TYPE_CHECKING: from ._endpoints import Receiver, Sender + from ._data import PythonAMQPData from ._handlers import TransactionHandler from socket import socket + from types import TracebackType from uuid import UUID @@ -129,11 +132,11 @@ class Reactor(object): self._handler = Handler() self._timerheap = [] self._timers = 0 - self.errors = [] + self.errors: List[Tuple[Type[BaseException], BaseException, 'TracebackType']] = [] for h in handlers: self.handler.add(h, on_error=self.on_error) - def on_error(self, info): + def on_error(self, info: Tuple[Type[BaseException], BaseException, 'TracebackType']) -> None: self.errors.append(info) self.yield_() @@ -148,23 +151,23 @@ class Reactor(object): """ return handler - def _get_global(self): + @property + def global_handler(self) -> Handler: return self._global_handler - def _set_global(self, handler): + @global_handler.setter + def global_handler(self, handler: Handler) -> None: self._global_handler = self._make_handler(handler) - global_handler = property(_get_global, _set_global) - - def _get_timeout(self): + @property + def timeout(self) -> float: return self._timeout - def _set_timeout(self, secs): + @timeout.setter + def timeout(self, secs: float) -> None: self._timeout = secs - timeout = property(_get_timeout, _set_timeout) - - def yield_(self): + def yield_(self) -> None: self._yield = True def mark(self) -> float: @@ -176,15 +179,15 @@ class Reactor(object): def now(self) -> float: return self._now - def _get_handler(self): + @property + def handler(self) -> Handler: return self._handler - def _set_handler(self, handler): + @handler.setter + def handler(self, handler: Handler) -> None: self._handler = self._make_handler(handler) - handler = property(_get_handler, _set_handler) - - def run(self): + def run(self) -> None: """ Start the processing of events and messages for this container. """ @@ -349,7 +352,7 @@ class Reactor(object): result.collect(self._collector) return result - def connection_to_host(self, host, port, handler=None): + def connection_to_host(self, host, port, handler: Optional[Handler] = None) -> Connection: """Create an outgoing Connection that will be managed by the reactor. The reactor's pn_iohandler will create a socket connection to the host once the connection is opened. @@ -358,7 +361,7 @@ class Reactor(object): self.set_connection_host(conn, host, port) return conn - def set_connection_host(self, connection, host, port): + def set_connection_host(self, connection: Connection, host, port) -> None: """Change the address used by the connection. The address is used by the reactor's iohandler to create an outgoing socket connection. This must be set prior to opening the connection. @@ -398,7 +401,7 @@ class Reactor(object): def push_event( self, - obj: Union[Task, 'Container', Selectable], + obj: Union['Reactor', Task, 'Container', Selectable], etype: EventType ) -> None: self._collector.put(obj, etype) @@ -569,11 +572,11 @@ class Transaction(object): def declare(self) -> None: self._declare = self._send_ctrl(symbol(u'amqp:declare:list'), [None]) - def discharge(self, failed): + def discharge(self, failed: bool) -> None: self.failed = failed self._discharge = self._send_ctrl(symbol(u'amqp:discharge:list'), [self.id, failed]) - def _send_ctrl(self, descriptor, value): + def _send_ctrl(self, descriptor: 'PythonAMQPData', value: 'PythonAMQPData') -> Delivery: delivery = self.txn_ctrl.send(Message(body=Described(descriptor, value))) delivery.transaction = self return delivery @@ -772,10 +775,9 @@ class Filter(ReceiverOption): :param filter_set: A map of filters with :class:`proton.symbol` keys containing the filter name, and the value a filter string. - :type filter_set: ``dict`` """ - def __init__(self, filter_set={}): + def __init__(self, filter_set: Dict[symbol, Described] = {}) -> None: self.filter_set = filter_set def apply(self, receiver: 'Receiver') -> None: @@ -987,14 +989,14 @@ class Backoff(object): return self.iter -def make_backoff_wrapper(backoff): +def make_backoff_wrapper( + backoff: Optional[Union[List[Union[float, int]], bool, Backoff]] +) -> Optional[Union[List[Union[float, int]], bool, Backoff]]: """ Make a wrapper for a backoff object: If the object conforms to the old protocol (has reset and next methods) then wrap it in an iterable that returns an iterator suitable for the new backoff approach otherwise assume it is fine as it is! - :param backoff: - :return: """ class WrappedBackoff(object): def __init__(self, backoff): --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@qpid.apache.org For additional commands, e-mail: commits-h...@qpid.apache.org