Improve matrix typing (#105067)

This commit is contained in:
Marc Mueller 2023-12-05 13:40:14 +01:00 committed by GitHub
parent f460fdf632
commit 6e0ba8e726
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,7 +7,7 @@ import logging
import mimetypes import mimetypes
import os import os
import re import re
from typing import NewType, TypedDict from typing import Final, NewType, Required, TypedDict
import aiofiles.os import aiofiles.os
from nio import AsyncClient, Event, MatrixRoom from nio import AsyncClient, Event, MatrixRoom
@ -49,11 +49,11 @@ _LOGGER = logging.getLogger(__name__)
SESSION_FILE = ".matrix.conf" SESSION_FILE = ".matrix.conf"
CONF_HOMESERVER = "homeserver" CONF_HOMESERVER: Final = "homeserver"
CONF_ROOMS = "rooms" CONF_ROOMS: Final = "rooms"
CONF_COMMANDS = "commands" CONF_COMMANDS: Final = "commands"
CONF_WORD = "word" CONF_WORD: Final = "word"
CONF_EXPRESSION = "expression" CONF_EXPRESSION: Final = "expression"
CONF_USERNAME_REGEX = "^@[^:]*:.*" CONF_USERNAME_REGEX = "^@[^:]*:.*"
CONF_ROOMS_REGEX = "^[!|#][^:]*:.*" CONF_ROOMS_REGEX = "^[!|#][^:]*:.*"
@ -78,10 +78,10 @@ RoomAnyID = RoomID | RoomAlias
class ConfigCommand(TypedDict, total=False): class ConfigCommand(TypedDict, total=False):
"""Corresponds to a single COMMAND_SCHEMA.""" """Corresponds to a single COMMAND_SCHEMA."""
name: str # CONF_NAME name: Required[str] # CONF_NAME
rooms: list[RoomID] | None # CONF_ROOMS rooms: list[RoomID] # CONF_ROOMS
word: WordCommand | None # CONF_WORD word: WordCommand # CONF_WORD
expression: ExpressionCommand | None # CONF_EXPRESSION expression: ExpressionCommand # CONF_EXPRESSION
COMMAND_SCHEMA = vol.All( COMMAND_SCHEMA = vol.All(
@ -223,15 +223,15 @@ class MatrixBot:
def _load_commands(self, commands: list[ConfigCommand]) -> None: def _load_commands(self, commands: list[ConfigCommand]) -> None:
for command in commands: for command in commands:
# Set the command for all listening_rooms, unless otherwise specified. # Set the command for all listening_rooms, unless otherwise specified.
command.setdefault(CONF_ROOMS, list(self._listening_rooms.values())) # type: ignore[misc] command.setdefault(CONF_ROOMS, list(self._listening_rooms.values()))
# COMMAND_SCHEMA guarantees that exactly one of CONF_WORD and CONF_expression are set. # COMMAND_SCHEMA guarantees that exactly one of CONF_WORD and CONF_expression are set.
if (word_command := command.get(CONF_WORD)) is not None: if (word_command := command.get(CONF_WORD)) is not None:
for room_id in command[CONF_ROOMS]: # type: ignore[literal-required] for room_id in command[CONF_ROOMS]:
self._word_commands.setdefault(room_id, {}) self._word_commands.setdefault(room_id, {})
self._word_commands[room_id][word_command] = command # type: ignore[index] self._word_commands[room_id][word_command] = command
else: else:
for room_id in command[CONF_ROOMS]: # type: ignore[literal-required] for room_id in command[CONF_ROOMS]:
self._expression_commands.setdefault(room_id, []) self._expression_commands.setdefault(room_id, [])
self._expression_commands[room_id].append(command) self._expression_commands[room_id].append(command)
@ -263,7 +263,7 @@ class MatrixBot:
# After single-word commands, check all regex commands in the room. # After single-word commands, check all regex commands in the room.
for command in self._expression_commands.get(room_id, []): for command in self._expression_commands.get(room_id, []):
match: re.Match = command[CONF_EXPRESSION].match(message.body) # type: ignore[literal-required] match = command[CONF_EXPRESSION].match(message.body)
if not match: if not match:
continue continue
message_data = { message_data = {