From d095ef847296dbaa7efa43e5fffe2ef0b717672b Mon Sep 17 00:00:00 2001 From: Benjamin Braatz Date: Wed, 17 Mar 2021 09:48:06 +0100 Subject: [PATCH] Use typing.Dict/List for dict/list in type hints. Generic dict/list is only possible with Python 3.9, not 3.8. --- controlpi/__init__.py | 4 ++-- controlpi/baseplugin.py | 8 ++++---- controlpi/messagebus.py | 30 +++++++++++++++--------------- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/controlpi/__init__.py b/controlpi/__init__.py index a894c93..d5bd838 100644 --- a/controlpi/__init__.py +++ b/controlpi/__init__.py @@ -9,7 +9,7 @@ The package combines them in its run function. import asyncio import jsonschema -from typing import Any +from typing import Dict, Any from controlpi.messagebus import MessageBus, Message, MessageTemplate from controlpi.pluginregistry import PluginRegistry @@ -19,7 +19,7 @@ from controlpi.baseplugin import BasePlugin, PluginConf CONF_SCHEMA = {'type': 'object'} -async def run(conf: dict[str, dict[str, Any]]) -> None: +async def run(conf: Dict[str, Dict[str, Any]]) -> None: """Run the ControlPi system based on a configuration. Check the given configuration, set up a plugin registry and a message diff --git a/controlpi/baseplugin.py b/controlpi/baseplugin.py index 6df6ed5..4dbe069 100644 --- a/controlpi/baseplugin.py +++ b/controlpi/baseplugin.py @@ -85,11 +85,11 @@ import jsonschema # type: ignore from controlpi.messagebus import MessageBus -from typing import Union, Any -JSONSchema = Union[bool, dict[str, Union[None, str, int, float, bool, - dict[str, Any], list[Any]]]] +from typing import Union, Dict, List, Any +JSONSchema = Union[bool, Dict[str, Union[None, str, int, float, bool, + Dict[str, Any], List[Any]]]] # Could be more specific. -PluginConf = dict[str, Any] +PluginConf = Dict[str, Any] # Could be more specific. diff --git a/controlpi/messagebus.py b/controlpi/messagebus.py index 812af64..b65b4b5 100644 --- a/controlpi/messagebus.py +++ b/controlpi/messagebus.py @@ -85,19 +85,19 @@ import asyncio import json import jsonschema # type: ignore -from typing import Union, Any, Iterable, Callable, Coroutine -MessageValue = Union[None, str, int, float, bool, dict[str, Any], list[Any]] +from typing import Union, Dict, List, Any, Iterable, Callable, Coroutine +MessageValue = Union[None, str, int, float, bool, Dict[str, Any], List[Any]] # Should really be: # MessageValue = Union[None, str, int, float, bool, -# dict[str, 'MessageValue'], list['MessageValue']] +# Dict[str, 'MessageValue'], List['MessageValue']] # But mypy does not support recursion by now: # https://github.com/python/mypy/issues/731 -JSONSchema = Union[bool, dict[str, MessageValue]] +JSONSchema = Union[bool, Dict[str, MessageValue]] # Could be even more specific. MessageCallback = Callable[['Message'], Coroutine[Any, Any, None]] -class Message(dict[str, MessageValue]): +class Message(Dict[str, MessageValue]): """Define arbitrary message. Messages are dictionaries with string keys and values that are strings, @@ -124,7 +124,7 @@ class Message(dict[str, MessageValue]): """ def __init__(self, sender: str, - init: dict[str, MessageValue] = None) -> None: + init: Dict[str, MessageValue] = None) -> None: """Initialise message. Message is initialised with given sender and possibly given @@ -309,7 +309,7 @@ class Message(dict[str, MessageValue]): return self[key] -class MessageTemplate(dict[str, JSONSchema]): +class MessageTemplate(Dict[str, JSONSchema]): """Define a message template. A message template is a mapping from string keys to JSON schemas as @@ -334,7 +334,7 @@ class MessageTemplate(dict[str, JSONSchema]): True """ - def __init__(self, init: dict[str, JSONSchema] = None) -> None: + def __init__(self, init: Dict[str, JSONSchema] = None) -> None: """Initialise message. Template is initialised empty or with given key-value pairs: @@ -721,8 +721,8 @@ class MessageTemplateRegistry: >>> r = MessageTemplateRegistry() """ - self._clients: list[str] = [] - self._children: dict[str, dict[str, MessageTemplateRegistry]] = {} + self._clients: List[str] = [] + self._children: Dict[str, Dict[str, MessageTemplateRegistry]] = {} def insert(self, template: MessageTemplate, client: str) -> None: """Register a client for a template. @@ -842,7 +842,7 @@ class MessageTemplateRegistry: dict_keys([]) """ self._clients = [c for c in self._clients if c != client] - new_children: dict[str, dict[str, MessageTemplateRegistry]] = {} + new_children: Dict[str, Dict[str, MessageTemplateRegistry]] = {} for key in self._children: new_children[key] = {} for schema in self._children[key]: @@ -892,7 +892,7 @@ class MessageTemplateRegistry: return True return False - def get(self, message: Message) -> list[str]: + def get(self, message: Message) -> List[str]: """Get all clients registered for templates matching a message. >>> r = MessageTemplateRegistry() @@ -925,7 +925,7 @@ class MessageTemplateRegistry: result.append(client) return result - def get_templates(self, client: str) -> list[MessageTemplate]: + def get_templates(self, client: str) -> List[MessageTemplate]: """Get all templates for a client. >>> r = MessageTemplateRegistry() @@ -1079,10 +1079,10 @@ class MessageBus: >>> asyncio.run(main()) """ self._queue: asyncio.Queue = asyncio.Queue() - self._plugins: dict[str, str] = {} + self._plugins: Dict[str, str] = {} self._send_reg: MessageTemplateRegistry = MessageTemplateRegistry() self._recv_reg: MessageTemplateRegistry = MessageTemplateRegistry() - self._callbacks: dict[str, MessageCallback] = {} + self._callbacks: Dict[str, MessageCallback] = {} def register(self, client: str, plugin: str, sends: Iterable[MessageTemplate], -- 2.34.1