Skip to content

Rename typ to type #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions distributed_websocket/_broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ async def publish(self, channel: str, message: Any) -> None:
async def get_message(self, **kwargs) -> Message | None:
message = await self._messages.get()
if self.has_subscribers(message['channel']):
typ, topic, conn_id, data = untag_broker_message(message['data'])
return Message(data=data, typ=typ, topic=topic, conn_id=conn_id)
type, topic, conn_id, data = untag_broker_message(message['data'])
return Message(data=data, type=type, topic=topic, conn_id=conn_id)

def has_subscribers(self, channel: str) -> bool:
return channel in self._subscribers
Expand Down Expand Up @@ -111,8 +111,8 @@ async def get_message(self, **kwargs) -> Message | None:
ignore_subscribe_messages=True
)
if message:
typ, topic, conn_id, data = untag_broker_message(message['data'])
return Message(data=data, typ=typ, topic=topic, conn_id=conn_id)
type, topic, conn_id, data = untag_broker_message(message['data'])
return Message(data=data, type=type, topic=topic, conn_id=conn_id)


def _create_inmemory_broker() -> InMemoryBroker:
Expand Down
20 changes: 10 additions & 10 deletions distributed_websocket/_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ def tag_client_message(data: dict) -> Any:


def validate_incoming_message(data: dict) -> None:
typ, topic, conn_id = (
type, topic, conn_id = (
data.get('type'),
data.get('topic'),
data.get('conn_id'),
)
if not is_valid_type_client_message(data):
raise ValueError(f'Invalid message type: {typ}')
if topic is None and typ not in __NULL_TOPIC_ALLOWED_TYPES:
raise ValueError(f'Invalid message type "{typ}" with no topic')
if conn_id is None and typ in __REQUIRE_CONN_ID_TYPES:
raise ValueError(f'Invalid message type "{typ}" with no conn_id')
raise ValueError(f'Invalid message type: {type}')
if topic is None and type not in __NULL_TOPIC_ALLOWED_TYPES:
raise ValueError(f'Invalid message type "{type}" with no topic')
if conn_id is None and type in __REQUIRE_CONN_ID_TYPES:
raise ValueError(f'Invalid message type "{type}" with no conn_id')


def untag_broker_message(data: dict | str) -> tuple:
Expand All @@ -70,11 +70,11 @@ def __init__(
self,
*,
data: Any,
typ: str,
type: str,
topic: str | None = None,
conn_id: str | list[str] | None = None,
) -> None:
self.typ = typ
self.type = type
self.topic = topic
self.conn_id = conn_id
self.data = data
Expand All @@ -83,13 +83,13 @@ def __init__(
def from_client_message(cls, *, data: dict) -> 'Message':
return cls(
data=data,
typ=data.pop('type', None),
type=data.pop('type', None),
topic=data.pop('topic', None),
conn_id=data.pop('conn_id', None),
)

def __serialize__(self) -> dict[str, Any]:
self.data['type'] = self.typ
self.data['type'] = self.type
self.data['topic'] = self.topic
self.data['conn_id'] = self.conn_id
return self.data
4 changes: 2 additions & 2 deletions distributed_websocket/_subscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


def is_subscription_message(message: Message) -> bool:
return message.typ == 'subscribe' or message.typ == 'unsubscribe'
return message.type == 'subscribe' or message.type == 'unsubscribe'


def _is_valid_subscription(topic: str) -> bool:
Expand Down Expand Up @@ -46,7 +46,7 @@ def unsubscribe(connection: Connection, message: Message) -> None:
def handle_subscription_message(
connection: Connection, message: Message
) -> None:
if message.typ == 'subscribe':
if message.type == 'subscribe':
subscribe(connection, message)
else:
unsubscribe(connection, message)
2 changes: 1 addition & 1 deletion distributed_websocket/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def send_by_conn_id(self, message: Message) -> None:
def _get_outgoing_message_handler(
self, message: Message
) -> Callable[[Message], T | Coroutine[Any, Any, T]]:
return getattr(self, message.typ, self.send)
return getattr(self, message.type, self.send)

def send_msg(self, message: Message) -> None:
self._get_outgoing_message_handler(message)(message)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async def app(scope: Scope, receive: Receive, send: Send) -> None:
manager.send(
Message(
data={'msg': 'hello'},
typ='send',
type='send',
topic='tests/1',
)
)
Expand Down Expand Up @@ -55,7 +55,7 @@ async def app(scope: Scope, receive: Receive, send: Send) -> None:
manager.send(
Message(
data={'msg': 'hello'},
typ='send',
type='send',
topic='tests/1',
)
)
Expand Down
8 changes: 4 additions & 4 deletions tests/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
def test_message_01():
m = Message(
data={'msg': 'hello'},
typ='send',
type='send',
topic='test',
conn_id='test',
)
Expand All @@ -25,16 +25,16 @@ def test_message_02():
data={'msg': 'hello', 'type': 'send', 'topic': 'test', 'conn_id': 'test'}
)
assert m.data == {'msg': 'hello'}
assert m.typ == 'send'
assert m.type == 'send'
assert m.topic == 'test'
assert m.conn_id == 'test'


def test_untag_broker_message_01():
typ, topic, conn_id, data = untag_broker_message(
type, topic, conn_id, data = untag_broker_message(
'{"msg": "hello", "type": "send", "topic": "test", "conn_id": "test"}'
)
assert typ == 'send'
assert type == 'send'
assert topic == 'test'
assert conn_id == 'test'
assert data == {'msg': 'hello'}