Skip to content

Commit

Permalink
type hints and docs (#1521)
Browse files Browse the repository at this point in the history
  • Loading branch information
oberstet committed Feb 16, 2022
1 parent bc98e4e commit e3273fb
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 30 deletions.
7 changes: 6 additions & 1 deletion autobahn/wamp/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,13 @@ def onClose(self, wasClean):
"""


# ISession.register collides with the abc.ABCMeta.register method
class _ABC(abc.ABC):
_abc_register = abc.ABC.register


@public
class ISession(abc.ABC):
class ISession(_ABC):
"""
Interface for WAMP sessions.
"""
Expand Down
4 changes: 2 additions & 2 deletions autobahn/wamp/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from autobahn.wamp import role
from autobahn.wamp import exception
from autobahn.wamp.exception import ApplicationError, ProtocolError, SessionNotReady, SerializationError, TypeCheckError
from autobahn.wamp.interfaces import IPayloadCodec, IAuthenticator # noqa
from autobahn.wamp.interfaces import ISession, IPayloadCodec, IAuthenticator # noqa
from autobahn.wamp.types import SessionDetails, CloseDetails, EncodedPayload
from autobahn.exception import PayloadExceededError
from autobahn.wamp.request import \
Expand Down Expand Up @@ -1940,7 +1940,7 @@ def on_disconnect(self):


# ISession.register collides with the abc.ABCMeta.register method
# ISession.register(ApplicationSession)
ISession._abc_register(ApplicationSession)


class ApplicationSessionFactory(object):
Expand Down
8 changes: 7 additions & 1 deletion autobahn/wamp/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,13 @@ def __init__(self, batched=False):
if _HAS_CBOR:

class CBORObjectSerializer(object):
"""
CBOR serializer based on `cbor2 <https://github.com/agronholm/cbor2>`_.
This CBOR serializer has proper support for arbitrary precision decimals,
via tagged decimal fraction encoding, as described in
`RFC7049 section 2.4.3 <https://datatracker.ietf.org/doc/html/rfc7049#section-2.4.3>`_.
"""

NAME = 'cbor'

Expand All @@ -711,7 +718,6 @@ class CBORObjectSerializer(object):

def __init__(self, batched=False):
"""
Ctor.
:param batched: Flag that controls whether serializer operates in batched mode.
:type batched: bool
Expand Down
27 changes: 14 additions & 13 deletions autobahn/wamp/uri.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


import re

from typing import Optional, Union

from autobahn.util import public
from autobahn.wamp.types import RegisterOptions, SubscribeOptions
Expand All @@ -40,7 +40,7 @@
)


def convert_starred_uri(uri):
def convert_starred_uri(uri: str):
"""
Convert a starred URI to a standard WAMP URI and a detected matching
policy. A starred URI is one that may contain the character '*' used
Expand Down Expand Up @@ -130,7 +130,8 @@ class Pattern(object):
This pattern is stricter than a general WAMP URI component since a valid Python identifier is required.
"""

def __init__(self, uri, target, options=None, check_types=False):
def __init__(self, uri: str, target: int, options: Optional[Union[SubscribeOptions, RegisterOptions]] = None,
check_types: Optional[bool] = None):
"""
:param uri: The URI or URI pattern, e.g. ``"com.myapp.product.<product:int>.update"``.
Expand All @@ -153,15 +154,15 @@ def __init__(self, uri, target, options=None, check_types=False):
:type check_types: bool
"""
assert(type(uri) == str)
assert(len(uri) > 0)
assert(target in [Pattern.URI_TARGET_ENDPOINT,
Pattern.URI_TARGET_HANDLER,
Pattern.URI_TARGET_EXCEPTION])
assert (type(uri) == str)
assert (len(uri) > 0)
assert (target in [Pattern.URI_TARGET_ENDPOINT,
Pattern.URI_TARGET_HANDLER,
Pattern.URI_TARGET_EXCEPTION])
if target == Pattern.URI_TARGET_ENDPOINT:
assert(options is None or type(options) == RegisterOptions)
assert (options is None or type(options) == RegisterOptions)
elif target == Pattern.URI_TARGET_HANDLER:
assert(options is None or type(options) == SubscribeOptions)
assert (options is None or type(options) == SubscribeOptions)
else:
options = None

Expand Down Expand Up @@ -327,7 +328,7 @@ def is_exception(self):


@public
def register(uri, options=None, check_types=False):
def register(uri: Optional[str], options: Optional[RegisterOptions] = None, check_types: Optional[bool] = None):
"""
Decorator for WAMP procedure endpoints.
Expand Down Expand Up @@ -360,7 +361,7 @@ def decorate(f):


@public
def subscribe(uri, options=None, check_types=False):
def subscribe(uri: Optional[str], options: Optional[SubscribeOptions] = None, check_types: Optional[bool] = None):
"""
Decorator for WAMP event handlers.
Expand Down Expand Up @@ -389,7 +390,7 @@ def decorate(f):


@public
def error(uri):
def error(uri: str):
"""
Decorator for WAMP error classes.
"""
Expand Down
26 changes: 13 additions & 13 deletions autobahn/websocket/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,21 +556,21 @@ def __init__(self):

def onOpen(self):
"""
Implements :func:`autobahn.websocket.interfaces.IWebSocketChannel.onOpen`
Implements :meth:`autobahn.websocket.interfaces.IWebSocketChannel.onOpen`
"""
self.log.debug("WebSocketProtocol.onOpen")

def onMessageBegin(self, isBinary):
"""
Implements :func:`autobahn.websocket.interfaces.IWebSocketChannel.onMessageBegin`
Implements :meth:`autobahn.websocket.interfaces.IWebSocketChannel.onMessageBegin`
"""
self.message_is_binary = isBinary
self.message_data = []
self.message_data_total_length = 0

def onMessageFrameBegin(self, length):
"""
Implements :func:`autobahn.websocket.interfaces.IWebSocketChannel.onMessageFrameBegin`
Implements :meth:`autobahn.websocket.interfaces.IWebSocketChannel.onMessageFrameBegin`
"""
self.frame_length = length
self.frame_data = []
Expand All @@ -589,7 +589,7 @@ def onMessageFrameBegin(self, length):

def onMessageFrameData(self, payload):
"""
Implements :func:`autobahn.websocket.interfaces.IWebSocketChannel.onMessageFrameData`
Implements :meth:`autobahn.websocket.interfaces.IWebSocketChannel.onMessageFrameData`
"""
if not self.failedByMe:
if self.websocket_version == 0:
Expand All @@ -605,7 +605,7 @@ def onMessageFrameData(self, payload):

def onMessageFrameEnd(self):
"""
Implements :func:`autobahn.websocket.interfaces.IWebSocketChannel.onMessageFrameEnd`
Implements :meth:`autobahn.websocket.interfaces.IWebSocketChannel.onMessageFrameEnd`
"""
if not self.failedByMe:
self._onMessageFrame(self.frame_data)
Expand All @@ -614,14 +614,14 @@ def onMessageFrameEnd(self):

def onMessageFrame(self, payload):
"""
Implements :func:`autobahn.websocket.interfaces.IWebSocketChannel.onMessageFrame`
Implements :meth:`autobahn.websocket.interfaces.IWebSocketChannel.onMessageFrame`
"""
if not self.failedByMe:
self.message_data.extend(payload)

def onMessageEnd(self):
"""
Implements :func:`autobahn.websocket.interfaces.IWebSocketChannel.onMessageEnd`
Implements :meth:`autobahn.websocket.interfaces.IWebSocketChannel.onMessageEnd`
"""
if not self.failedByMe:
payload = b''.join(self.message_data)
Expand All @@ -645,7 +645,7 @@ def error(f):

def onMessage(self, payload, isBinary):
"""
Implements :func:`autobahn.websocket.interfaces.IWebSocketChannel.onMessage`
Implements :meth:`autobahn.websocket.interfaces.IWebSocketChannel.onMessage`
"""
self.log.debug(
"WebSocketProtocol.onMessage(payload=<{payload_len} bytes)>, isBinary={isBinary}",
Expand All @@ -655,7 +655,7 @@ def onMessage(self, payload, isBinary):

def onPing(self, payload):
"""
Implements :func:`autobahn.websocket.interfaces.IWebSocketChannel.onPing`
Implements :meth:`autobahn.websocket.interfaces.IWebSocketChannel.onPing`
"""
self.log.debug(
"WebSocketProtocol.onPing(payload=<{payload_len} bytes>)",
Expand All @@ -666,7 +666,7 @@ def onPing(self, payload):

def onPong(self, payload):
"""
Implements :func:`autobahn.websocket.interfaces.IWebSocketChannel.onPong`
Implements :meth:`autobahn.websocket.interfaces.IWebSocketChannel.onPong`
"""
self.log.debug(
"WebSocketProtocol.onPong(payload=<{payload_len} bytes>)",
Expand All @@ -675,7 +675,7 @@ def onPong(self, payload):

def onClose(self, wasClean, code, reason):
"""
Implements :func:`autobahn.websocket.interfaces.IWebSocketChannel.onClose`
Implements :meth:`autobahn.websocket.interfaces.IWebSocketChannel.onClose`
"""
self.log.debug(
"WebSocketProtocol.onClose(wasClean={wasClean}, code={code}, reason={reason})",
Expand Down Expand Up @@ -2534,7 +2534,7 @@ def onConnect(self, request):
to accept the WebSocket connection request.
:param request: WebSocket connection request information.
:type request: instance of :class:`autobahn.websocket.protocol.ConnectionRequest`
:type request: instance of :class:`autobahn.websocket.types.ConnectionRequest`
"""
return None

Expand Down Expand Up @@ -3428,7 +3428,7 @@ def onConnect(self, response):
connection was established.
:param response: WebSocket connection response information.
:type response: instance of :class:`autobahn.websocket.protocol.ConnectionResponse`
:type response: instance of :class:`autobahn.websocket.types.ConnectionResponse`
"""
pass

Expand Down

0 comments on commit e3273fb

Please sign in to comment.