Removed <type> | None type hints.

They break on Python < 3.10.
This commit is contained in:
Fabio Manganiello 2023-10-03 01:15:13 +02:00
parent fd7037d048
commit 40d3ad1150
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774
3 changed files with 64 additions and 61 deletions

View file

@ -1,5 +1,5 @@
from datetime import datetime from datetime import datetime
from typing import Dict, Any from typing import Any, Dict, Optional
from platypush.message.event import Event from platypush.message.event import Event
@ -13,13 +13,13 @@ class MatrixEvent(Event):
self, self,
*args, *args,
server_url: str, server_url: str,
sender_id: str | None = None, sender_id: Optional[str] = None,
sender_display_name: str | None = None, sender_display_name: Optional[str] = None,
sender_avatar_url: str | None = None, sender_avatar_url: Optional[str] = None,
room_id: str | None = None, room_id: Optional[str] = None,
room_name: str | None = None, room_name: Optional[str] = None,
room_topic: str | None = None, room_topic: Optional[str] = None,
server_timestamp: datetime | None = None, server_timestamp: Optional[datetime] = None,
**kwargs **kwargs
): ):
""" """
@ -70,11 +70,11 @@ class MatrixMessageEvent(MatrixEvent):
self, self,
*args, *args,
body: str = '', body: str = '',
url: str | None = None, url: Optional[str] = None,
thumbnail_url: str | None = None, thumbnail_url: Optional[str] = None,
mimetype: str | None = None, mimetype: Optional[str] = None,
formatted_body: str | None = None, formatted_body: Optional[str] = None,
format: str | None = None, format: Optional[str] = None,
**kwargs **kwargs
): ):
""" """
@ -148,7 +148,7 @@ class MatrixCallEvent(MatrixEvent):
""" """
def __init__( def __init__(
self, *args, call_id: str, version: int, sdp: str | None = None, **kwargs self, *args, call_id: str, version: int, sdp: Optional[str] = None, **kwargs
): ):
""" """
:param call_id: The unique ID of the call. :param call_id: The unique ID of the call.
@ -163,7 +163,7 @@ class MatrixCallInviteEvent(MatrixCallEvent):
Event triggered when the user is invited to a call. Event triggered when the user is invited to a call.
""" """
def __init__(self, *args, invite_validity: float | None = None, **kwargs): def __init__(self, *args, invite_validity: Optional[float] = None, **kwargs):
""" """
:param invite_validity: For how long the invite will be valid, in seconds. :param invite_validity: For how long the invite will be valid, in seconds.
:param sdp: SDP text of the session description. :param sdp: SDP text of the session description.
@ -242,7 +242,9 @@ class MatrixUserPresenceEvent(MatrixEvent):
Event triggered when a user comes online or goes offline. Event triggered when a user comes online or goes offline.
""" """
def __init__(self, *args, is_active: bool, last_active: datetime | None, **kwargs): def __init__(
self, *args, is_active: bool, last_active: Optional[datetime], **kwargs
):
""" """
:param is_active: True if the user is currently online. :param is_active: True if the user is currently online.
:param topic: When the user was last active. :param topic: When the user was last active.

View file

@ -5,7 +5,7 @@ import pathlib
import re import re
from dataclasses import dataclass from dataclasses import dataclass
from typing import Collection, Coroutine, Sequence from typing import Collection, Coroutine, Optional, Sequence
from urllib.parse import urlparse from urllib.parse import urlparse
from nio import ( from nio import (
@ -47,7 +47,7 @@ class Credentials:
server_url: str server_url: str
user_id: str user_id: str
access_token: str access_token: str
device_id: str | None device_id: Optional[str] = None
def to_dict(self) -> dict: def to_dict(self) -> dict:
return { return {
@ -98,22 +98,22 @@ class MatrixPlugin(AsyncRunnablePlugin):
def __init__( def __init__(
self, self,
server_url: str = 'https://matrix-client.matrix.org', server_url: str = 'https://matrix-client.matrix.org',
user_id: str | None = None, user_id: Optional[str] = None,
password: str | None = None, password: Optional[str] = None,
access_token: str | None = None, access_token: Optional[str] = None,
device_name: str | None = 'platypush', device_name: Optional[str] = 'platypush',
device_id: str | None = None, device_id: Optional[str] = None,
download_path: str | None = None, download_path: Optional[str] = None,
autojoin_on_invite: bool = True, autojoin_on_invite: bool = True,
autotrust_devices: bool = False, autotrust_devices: bool = False,
autotrust_devices_whitelist: Collection[str] | None = None, autotrust_devices_whitelist: Optional[Collection[str]] = None,
autotrust_users_whitelist: Collection[str] | None = None, autotrust_users_whitelist: Optional[Collection[str]] = None,
autotrust_rooms_whitelist: Collection[str] | None = None, autotrust_rooms_whitelist: Optional[Collection[str]] = None,
**kwargs, **kwargs,
): ):
""" """
Authentication requires user_id/password on the first login. Authentication requires user_id/password on the first login.
Afterwards, session credentials are stored under Afterward, session credentials are stored under
``<$PLATYPUSH_WORKDIR>/matrix/credentials.json`` (default: ``<$PLATYPUSH_WORKDIR>/matrix/credentials.json`` (default:
``~/.local/share/platypush/matrix/credentials.json``), and you can ``~/.local/share/platypush/matrix/credentials.json``), and you can
remove the cleartext credentials from your configuration file. remove the cleartext credentials from your configuration file.
@ -299,9 +299,9 @@ class MatrixPlugin(AsyncRunnablePlugin):
self, self,
room_id: str, room_id: str,
message_type: str = 'text', message_type: str = 'text',
body: str | None = None, body: Optional[str] = None,
attachment: str | None = None, attachment: Optional[str] = None,
tx_id: str | None = None, tx_id: Optional[str] = None,
ignore_unverified_devices: bool = False, ignore_unverified_devices: bool = False,
): ):
""" """
@ -388,8 +388,8 @@ class MatrixPlugin(AsyncRunnablePlugin):
def get_messages( def get_messages(
self, self,
room_id: str, room_id: str,
start: str | None = None, start: Optional[str] = None,
end: str | None = None, end: Optional[str] = None,
backwards: bool = True, backwards: bool = True,
limit: int = 10, limit: int = 10,
): ):
@ -442,10 +442,11 @@ class MatrixPlugin(AsyncRunnablePlugin):
return MatrixDeviceSchema().dump(self._get_device(device_id)) return MatrixDeviceSchema().dump(self._get_device(device_id))
@action @action
def update_device(self, device_id: str, display_name: str | None = None): def update_device(self, device_id: str, display_name: Optional[str] = None):
""" """
Update information about a user's device. Update information about a user's device.
:param device_id: Device ID.
:param display_name: New display name. :param display_name: New display name.
:return: .. schema:: matrix.MatrixDeviceSchema :return: .. schema:: matrix.MatrixDeviceSchema
""" """
@ -460,8 +461,8 @@ class MatrixPlugin(AsyncRunnablePlugin):
def delete_devices( def delete_devices(
self, self,
devices: Sequence[str], devices: Sequence[str],
username: str | None = None, username: Optional[str] = None,
password: str | None = None, password: Optional[str] = None,
): ):
""" """
Delete a list of devices from the user's authorized list and invalidate Delete a list of devices from the user's authorized list and invalidate
@ -564,7 +565,7 @@ class MatrixPlugin(AsyncRunnablePlugin):
self.client.unverify_device(device) self.client.unverify_device(device)
@action @action
def mxc_to_http(self, url: str, homeserver: str | None = None) -> str: def mxc_to_http(self, url: str, homeserver: Optional[str] = None) -> str:
""" """
Convert a Matrix URL (in the format ``mxc://server/media_id``) to an Convert a Matrix URL (in the format ``mxc://server/media_id``) to an
HTTP URL. HTTP URL.
@ -587,8 +588,8 @@ class MatrixPlugin(AsyncRunnablePlugin):
def download( def download(
self, self,
url: str, url: str,
download_path: str | None = None, download_path: Optional[str] = None,
filename: str | None = None, filename: Optional[str] = None,
allow_remote=True, allow_remote=True,
): ):
""" """
@ -641,8 +642,8 @@ class MatrixPlugin(AsyncRunnablePlugin):
def upload( def upload(
self, self,
file: str, file: str,
name: str | None = None, name: Optional[str] = None,
content_type: str | None = None, content_type: Optional[str] = None,
encrypt: bool = False, encrypt: bool = False,
) -> str: ) -> str:
""" """
@ -665,9 +666,9 @@ class MatrixPlugin(AsyncRunnablePlugin):
@action @action
def create_room( def create_room(
self, self,
name: str | None = None, name: Optional[str] = None,
alias: str | None = None, alias: Optional[str] = None,
topic: str | None = None, topic: Optional[str] = None,
is_public: bool = False, is_public: bool = False,
is_direct: bool = False, is_direct: bool = False,
federate: bool = True, federate: bool = True,
@ -729,7 +730,7 @@ class MatrixPlugin(AsyncRunnablePlugin):
self._loop_execute(self.client.room_invite(room_id, user_id)) self._loop_execute(self.client.room_invite(room_id, user_id))
@action @action
def kick(self, room_id: str, user_id: str, reason: str | None = None): def kick(self, room_id: str, user_id: str, reason: Optional[str] = None):
""" """
Kick a user out of a room. Kick a user out of a room.
@ -740,7 +741,7 @@ class MatrixPlugin(AsyncRunnablePlugin):
self._loop_execute(self.client.room_kick(room_id, user_id, reason)) self._loop_execute(self.client.room_kick(room_id, user_id, reason))
@action @action
def ban(self, room_id: str, user_id: str, reason: str | None = None): def ban(self, room_id: str, user_id: str, reason: Optional[str] = None):
""" """
Ban a user from a room. Ban a user from a room.

View file

@ -100,7 +100,7 @@ class Credentials:
server_url: str server_url: str
user_id: str user_id: str
access_token: str access_token: str
device_id: str | None device_id: Optional[str] = None
def to_dict(self) -> dict: def to_dict(self) -> dict:
return { return {
@ -116,13 +116,13 @@ class MatrixClient(AsyncClient):
self, self,
*args, *args,
credentials_file: str, credentials_file: str,
store_path: str | None = None, store_path: Optional[str] = None,
config: Optional[AsyncClientConfig] = None, config: Optional[AsyncClientConfig] = None,
autojoin_on_invite=True, autojoin_on_invite=True,
autotrust_devices=False, autotrust_devices=False,
autotrust_devices_whitelist: Collection[str] | None = None, autotrust_devices_whitelist: Optional[Collection[str]] = None,
autotrust_rooms_whitelist: Collection[str] | None = None, autotrust_rooms_whitelist: Optional[Collection[str]] = None,
autotrust_users_whitelist: Collection[str] | None = None, autotrust_users_whitelist: Optional[Collection[str]] = None,
**kwargs, **kwargs,
): ):
credentials_file = os.path.abspath(os.path.expanduser(credentials_file)) credentials_file = os.path.abspath(os.path.expanduser(credentials_file))
@ -158,7 +158,7 @@ class MatrixClient(AsyncClient):
store_path, 'attachment_keys.json' store_path, 'attachment_keys.json'
) )
self._encrypted_attachments_keystore = {} self._encrypted_attachments_keystore = {}
self._sync_store_timer: threading.Timer | None = None self._sync_store_timer: Optional[threading.Timer] = None
keystore = {} keystore = {}
try: try:
@ -206,9 +206,9 @@ class MatrixClient(AsyncClient):
async def login( async def login(
self, self,
password: str | None = None, password: Optional[str] = None,
device_name: str | None = None, device_name: Optional[str] = None,
token: str | None = None, token: Optional[str] = None,
) -> LoginResponse: ) -> LoginResponse:
self._load_from_file() self._load_from_file()
login_res = None login_res = None
@ -289,7 +289,7 @@ class MatrixClient(AsyncClient):
@logged_in @logged_in
async def room_messages( async def room_messages(
self, room_id: str, start: str | None = None, *args, **kwargs self, room_id: str, start: Optional[str] = None, *args, **kwargs
) -> RoomMessagesResponse: ) -> RoomMessagesResponse:
if not start: if not start:
start = self._last_batches_by_room.get(room_id, {}).get('prev_batch') start = self._last_batches_by_room.get(room_id, {}).get('prev_batch')
@ -351,9 +351,9 @@ class MatrixClient(AsyncClient):
) )
def get_devices_by_user( def get_devices_by_user(
self, user_id: str | None = None self, user_id: Optional[str] = None
) -> Dict[str, Dict[str, OlmDevice]] | Dict[str, OlmDevice]: ) -> Dict[str, Dict[str, OlmDevice]] | Dict[str, OlmDevice]:
devices = {user: devices for user, devices in self.device_store.items()} devices = dict(self.device_store.items())
if user_id: if user_id:
devices = devices.get(user_id, {}) devices = devices.get(user_id, {})
@ -370,7 +370,7 @@ class MatrixClient(AsyncClient):
return self.get_devices().get(device_id) return self.get_devices().get(device_id)
def get_devices_by_room( def get_devices_by_room(
self, room_id: str | None = None self, room_id: Optional[str] = None
) -> Dict[str, Dict[str, OlmDevice]] | Dict[str, OlmDevice]: ) -> Dict[str, Dict[str, OlmDevice]] | Dict[str, OlmDevice]:
devices = { devices = {
room_id: { room_id: {
@ -432,7 +432,7 @@ class MatrixClient(AsyncClient):
@alru_cache(maxsize=500) @alru_cache(maxsize=500)
@client_session @client_session
async def get_profile(self, user_id: str | None = None) -> ProfileGetResponse: async def get_profile(self, user_id: Optional[str] = None) -> ProfileGetResponse:
""" """
Cached version of get_profile. Cached version of get_profile.
""" """
@ -459,7 +459,7 @@ class MatrixClient(AsyncClient):
self, self,
server_name: str, server_name: str,
media_id: str, media_id: str,
filename: str | None = None, filename: Optional[str] = None,
allow_remote: bool = True, allow_remote: bool = True,
): ):
response = await super().download( response = await super().download(