Skip to content

Commit

Permalink
Fix lints
Browse files Browse the repository at this point in the history
  • Loading branch information
jerith committed Feb 13, 2024
1 parent 8f49366 commit 1d4f082
Show file tree
Hide file tree
Showing 15 changed files with 92 additions and 90 deletions.
13 changes: 12 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ ruff = "^0.0.261"
Sphinx = "^6.1.3"
sphinxcontrib-httpdomain = "^1.8.1"
trio-typing = "^0.8.0"
types-pyyaml = "^6.0"

[tool.poetry.group.lsp]
optional = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
from http import HTTPStatus
from logging import getLogger
from typing import Optional, Union

from attrs import define, field
from httpx import AsyncClient
Expand Down Expand Up @@ -54,12 +53,12 @@ class JunebugMessageApiConfig(BaseConfig):
# The URL to POST inbound messages to.
mo_message_url: str
# Authorization token to use for inbound message HTTP requests.
mo_message_url_auth_token: Optional[str] = None
mo_message_url_auth_token: str | None = None

# The URL to POST events with no associated message info to.
default_event_url: Optional[str] = None
default_event_url: str | None = None
# Authorization token to use for events with no associates message info.
default_event_auth_token: Optional[str] = None
default_event_auth_token: str | None = None

# Base URL path for outbound message HTTP requests. This has "/messages"
# appended to it. For compatibility with existing Junebug API clients, set
Expand All @@ -71,7 +70,7 @@ class JunebugMessageApiConfig(BaseConfig):
transport_type: TransportType = TransportType.SMS

# If None, all outbound messages must be replies or have a from address.
default_from_addr: Optional[str] = None
default_from_addr: str | None = None

# If True, outbound messages with both `to` and `reply_to` set will be sent
# as non-reply messages if the `reply_to` message can't be found.
Expand Down Expand Up @@ -188,7 +187,7 @@ async def handle_event(self, event: Event) -> None:
if cs.cancelled_caught:
logger.error(LOG_EV_HTTP_TIMEOUT, {"timeout": timeout, "event": ev})

async def http_send_message(self) -> tuple[Union[str, dict], int, dict[str, str]]:
async def http_send_message(self) -> tuple[str | dict, int, dict[str, str]]:
_message_id = generate_message_id()
try:
# TODO: Log requests that timed out?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from abc import ABC, abstractmethod
from typing import Optional

import cattrs
from attrs import define
Expand All @@ -11,7 +10,7 @@
@define(frozen=True)
class EventHttpInfo:
url: str
auth_token: Optional[str]
auth_token: str | None


class JunebugStateCache(ABC): # pragma: no cover
Expand All @@ -20,12 +19,12 @@ async def store_event_http_info(
self,
message_id: str,
url: str,
auth_token: Optional[str],
auth_token: str | None,
) -> None:
...

@abstractmethod
async def fetch_event_http_info(self, message_id: str) -> Optional[EventHttpInfo]:
async def fetch_event_http_info(self, message_id: str) -> EventHttpInfo | None:
...

@abstractmethod
Expand All @@ -37,7 +36,7 @@ async def store_inbound(self, msg: Message) -> None:
...

@abstractmethod
async def fetch_inbound(self, message_id: str) -> Optional[Message]:
async def fetch_inbound(self, message_id: str) -> Message | None:
...

@abstractmethod
Expand All @@ -62,15 +61,15 @@ async def store_event_http_info(
self,
message_id: str,
url: str,
auth_token: Optional[str],
auth_token: str | None,
) -> None:
"""
Stores the mapping between one or many smpp message IDs and the vumi message ID
"""
if self.config.store_event_info:
self._event_http_info[message_id] = EventHttpInfo(url, auth_token)

async def fetch_event_http_info(self, message_id: str) -> Optional[EventHttpInfo]:
async def fetch_event_http_info(self, message_id: str) -> EventHttpInfo | None:
return self._event_http_info[message_id]

async def delete_event_http_info(self, message_id: str) -> None:
Expand All @@ -82,7 +81,7 @@ async def store_inbound(self, msg: Message) -> None:
"""
self._inbound[msg.message_id] = msg.serialise()

async def fetch_inbound(self, message_id: str) -> Optional[Message]:
async def fetch_inbound(self, message_id: str) -> Message | None:
msg = self._inbound[message_id]
return Message.deserialise(msg) if msg else None

Expand Down
16 changes: 8 additions & 8 deletions src/vumi2/applications/junebug_message_api/messages.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Optional
from typing import Any

import cattrs
from attrs import define, field
Expand Down Expand Up @@ -128,12 +128,12 @@ class JunebugOutboundMessage:
"""

content: str
to: Optional[str] = None
from_addr: Optional[str] = None
group: Optional[str] = None
reply_to: Optional[str] = None
event_url: Optional[str] = None
event_auth_token: Optional[str] = None
to: str | None = None
from_addr: str | None = None
group: str | None = None
reply_to: str | None = None
event_url: str | None = None
event_auth_token: str | None = None
priority: int = 1
channel_data: dict[str, Any] = field(factory=dict)

Expand All @@ -143,7 +143,7 @@ def __attrs_post_init__(self):

@classmethod
def deserialise(
cls, data: dict[str, Any], default_from: Optional[str] = None
cls, data: dict[str, Any], default_from: str | None = None
) -> "JunebugOutboundMessage":
if data.get("from") is None:
data["from"] = default_from
Expand Down
4 changes: 2 additions & 2 deletions src/vumi2/async_helpers.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from collections.abc import Awaitable
from inspect import isawaitable
from typing import TypeVar, Union, cast
from typing import TypeVar, cast

_RETURN = TypeVar("_RETURN")


async def maybe_awaitable(value: Union[_RETURN, Awaitable[_RETURN]]) -> _RETURN:
async def maybe_awaitable(value: _RETURN | Awaitable[_RETURN]) -> _RETURN:
if isawaitable(value):
# cast because isawaitable doesn't properly narrow the type
return await cast(Awaitable[_RETURN], value)
Expand Down
13 changes: 5 additions & 8 deletions src/vumi2/config.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import os
from argparse import Namespace
from collections.abc import Iterable
from collections.abc import Callable, Iterable
from pathlib import Path
from typing import (
Any,
Callable,
Generic,
Optional,
Protocol,
TypeVar,
Union,
get_type_hints,
)

Expand All @@ -27,7 +24,7 @@ class Configurable(Protocol, Generic[CT]):
config: CT


def get_config_class(cls: Union[Configurable[CT], type[Configurable[CT]]]) -> type[CT]:
def get_config_class(cls: Configurable[CT] | type[Configurable[CT]]) -> type[CT]:
return get_type_hints(cls)["config"]


Expand All @@ -37,7 +34,7 @@ def structure(config: dict, cls: type[CT]) -> CT:

def structure_config(
config: dict,
obj: Union[Configurable[CT], type[Configurable[CT]]],
obj: Configurable[CT] | type[Configurable[CT]],
) -> CT:
return structure(config, get_config_class(obj))

Expand All @@ -56,8 +53,8 @@ class BaseConfig:
amqp: AmqpConfig = Factory(AmqpConfig)
amqp_url: str = ""
worker_concurrency: int = 20
sentry_dsn: Optional[str] = None
http_bind: Optional[str] = None
sentry_dsn: str | None = None
http_bind: str | None = None
log_level: str = "INFO"


Expand Down
8 changes: 4 additions & 4 deletions src/vumi2/connectors.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
from collections.abc import Awaitable
from collections.abc import Awaitable, Callable
from logging import getLogger
from typing import Callable, Optional, overload
from typing import overload

import trio
from async_amqp import AmqpProtocol # type: ignore
Expand All @@ -16,8 +16,8 @@
logger = getLogger(__name__)


MessageCallbackType = Callable[[Message], Optional[Awaitable[None]]]
EventCallbackType = Callable[[Event], Optional[Awaitable[None]]]
MessageCallbackType = Callable[[Message], Awaitable[None] | None]
EventCallbackType = Callable[[Event], Awaitable[None] | None]
_AmqpChType = tuple[Channel, bytes, Envelope, Properties]


Expand Down
8 changes: 4 additions & 4 deletions src/vumi2/message_caches.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from abc import ABC, abstractmethod
from collections.abc import Iterator, MutableMapping
from typing import Generic, Optional, TypeVar
from typing import Generic, TypeVar

from attrs import define
from cattrs import structure
Expand Down Expand Up @@ -34,7 +34,7 @@ def __setitem__(self, key: str, value: T) -> None:
self._remove_expired()
self.data[key] = (current_time(), value)

def __getitem__(self, key: str) -> Optional[T]:
def __getitem__(self, key: str) -> T | None:
self._remove_expired()
if key not in self.data:
return None
Expand Down Expand Up @@ -63,7 +63,7 @@ async def store_outbound(self, outbound: Message) -> None:
...

@abstractmethod
async def fetch_outbound(self, message_id: str) -> Optional[Message]:
async def fetch_outbound(self, message_id: str) -> Message | None:
...


Expand All @@ -81,5 +81,5 @@ def __init__(self, config: dict) -> None:
async def store_outbound(self, outbound: Message) -> None:
self._outbounds[outbound.message_id] = outbound

async def fetch_outbound(self, message_id: str) -> Optional[Message]:
async def fetch_outbound(self, message_id: str) -> Message | None:
return self._outbounds.get(message_id)
24 changes: 12 additions & 12 deletions src/vumi2/messages.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import datetime
from enum import Enum
from typing import Any, Optional, Union
from typing import Any, Union
from uuid import uuid4

import cattrs
Expand Down Expand Up @@ -80,14 +80,14 @@ class Message:
routing_metadata: dict = Factory(dict)
helper_metadata: dict = Factory(dict)
message_id: str = Factory(generate_message_id)
in_reply_to: Optional[str] = None
provider: Optional[str] = None
in_reply_to: str | None = None
provider: str | None = None
session_event: Session = Session.NONE
content: Optional[str] = None
content: str | None = None
transport_metadata: dict = Factory(dict)
group: Optional[str] = None
to_addr_type: Optional[AddressType] = None
from_addr_type: Optional[AddressType] = None
group: str | None = None
to_addr_type: AddressType | None = None
from_addr_type: AddressType | None = None

def serialise(self) -> dict[str, Any]:
return cattrs.unstructure(self)
Expand All @@ -97,7 +97,7 @@ def deserialise(cls: "type[Message]", data: dict[str, Any]) -> "Message":
return cattrs.structure(data, cls)

def reply(
self, content: Optional[str] = None, session_event=Session.RESUME, **kwargs
self, content: str | None = None, session_event=Session.RESUME, **kwargs
) -> "Message":
for f in [
"to_addr",
Expand Down Expand Up @@ -138,11 +138,11 @@ class Event:
timestamp: datetime = Factory(datetime.utcnow)
routing_metadata: dict = Factory(dict)
helper_metadata: dict = Factory(dict)
transport_metadata: Optional[dict] = Factory(dict)
transport_metadata: dict | None = Factory(dict)
event_id: str = Factory(generate_message_id)
sent_message_id: Optional[str] = None
nack_reason: Optional[str] = None
delivery_status: Optional[DeliveryStatus] = None
sent_message_id: str | None = None
nack_reason: str | None = None
delivery_status: DeliveryStatus | None = None

@event_type.validator
def _check_event_type(self, _, value: EventType) -> None:
Expand Down
7 changes: 3 additions & 4 deletions src/vumi2/transports/httprpc/http_rpc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from logging import getLogger
from typing import Union

from attrs import Factory, define
from quart import request
Expand Down Expand Up @@ -32,7 +31,7 @@ class Request:

@define
class Response:
data: Union[str, dict]
data: str | dict
code: int
headers: dict[str, str]

Expand All @@ -49,7 +48,7 @@ async def setup(self) -> None:
self.http.app.add_url_rule(self.config.web_path, view_func=self.inbound_request)
await self.start_consuming()

async def inbound_request(self) -> tuple[Union[str, dict], int, dict[str, str]]:
async def inbound_request(self) -> tuple[str | dict, int, dict[str, str]]:
message_id = generate_message_id()
try:
with move_on_after(self.config.request_timeout):
Expand Down Expand Up @@ -135,7 +134,7 @@ async def handle_outbound_message(self, message: Message) -> None:
await self.publish_ack(message.message_id)

def finish_request(
self, request_id: str, data: Union[str, dict], code=200, headers=None
self, request_id: str, data: str | dict, code=200, headers=None
) -> None:
headers = {} if headers is None else headers
logger.debug(
Expand Down
Loading

0 comments on commit 1d4f082

Please sign in to comment.