Remove union operator for Python <3.10 compatibility

This commit is contained in:
Andrew Ferrazzutti 2022-02-26 04:06:26 -05:00
parent b1e922a0fd
commit db41292be7
12 changed files with 69 additions and 57 deletions

View File

@ -22,7 +22,7 @@ with any other potential backend.
from __future__ import annotations from __future__ import annotations
from typing import TYPE_CHECKING, cast, Awaitable, Callable, Type from typing import TYPE_CHECKING, cast, Awaitable, Callable, Type, Optional, Union
import logging import logging
import urllib.request import urllib.request
@ -116,7 +116,7 @@ class Client:
http: ClientSession http: ClientSession
log: TraceLogger log: TraceLogger
def __init__(self, user: User, log: TraceLogger | None = None): def __init__(self, user: User, log: Optional[TraceLogger] = None):
"""Create a per-user client object for user-specific client functionality.""" """Create a per-user client object for user-specific client functionality."""
self.user = user self.user = user
@ -149,8 +149,8 @@ class Client:
def get( def get(
self, self,
url: str | URL, url: Union[str, URL],
headers: dict[str, str] | None = None, headers: Optional[dict[str, str]] = None,
**kwargs, **kwargs,
) -> _RequestContextManager: ) -> _RequestContextManager:
# TODO Is auth ever needed? # TODO Is auth ever needed?

View File

@ -15,6 +15,8 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
"""Custom wrapper classes around types defined by the KakaoTalk API.""" """Custom wrapper classes around types defined by the KakaoTalk API."""
from typing import Optional, Union
from attr import dataclass from attr import dataclass
from mautrix.types import SerializableAttrs from mautrix.types import SerializableAttrs
@ -24,12 +26,12 @@ from ..types.openlink.open_channel_info import OpenChannelInfo
from ..types.user.channel_user_info import NormalChannelUserInfo, OpenChannelUserInfo from ..types.user.channel_user_info import NormalChannelUserInfo, OpenChannelUserInfo
ChannelInfoUnion = NormalChannelInfo | OpenChannelInfo ChannelInfoUnion = Union[NormalChannelInfo, OpenChannelInfo]
UserInfoUnion = NormalChannelUserInfo | OpenChannelUserInfo UserInfoUnion = Union[NormalChannelUserInfo, OpenChannelUserInfo]
@dataclass @dataclass
class PortalChannelInfo(SerializableAttrs): class PortalChannelInfo(SerializableAttrs):
name: str name: str
#participants: list[PuppetUserInfo] #participants: list[PuppetUserInfo]
# TODO Image # TODO Image
channel_info: ChannelInfoUnion | None = None # Should be set manually by caller channel_info: Optional[ChannelInfoUnion] = None # Should be set manually by caller

View File

@ -13,6 +13,8 @@
# #
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
from typing import Optional, Union
from attr import dataclass from attr import dataclass
from mautrix.types import SerializableAttrs, JSON from mautrix.types import SerializableAttrs, JSON
@ -34,9 +36,9 @@ class ProfileFeed(SerializableAttrs):
downloadId: str downloadId: str
contents: list[ProfileFeedObject] contents: list[ProfileFeedObject]
url: str url: str
serviceUrl: str | None = None # NOTE Made optional serviceUrl: Optional[str] = None # NOTE Made optional
webUrl: str webUrl: str
serviceWebUrl: str | None = None # NOTE Made optional serviceWebUrl: Optional[str] = None # NOTE Made optional
updatedAt: int updatedAt: int
cursor: int cursor: int
feedMessage: str feedMessage: str
@ -99,7 +101,7 @@ class ProfileStruct(SerializableAttrs):
screenToken: int screenToken: int
# NEW # NEW
nickname: str nickname: str
userId: int | str | Long # NOTE Should confirm this userId: Union[int, str, Long] # NOTE Should confirm this
suspended: bool suspended: bool
meBadge: bool meBadge: bool
# TODO feeds = {feeds: list, last: bool} # TODO feeds = {feeds: list, last: bool}

View File

@ -13,6 +13,8 @@
# #
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
from typing import Union
from attr import dataclass from attr import dataclass
from mautrix.types import SerializableAttrs from mautrix.types import SerializableAttrs
@ -24,4 +26,4 @@ from ..bson import Long
class MentionStruct(SerializableAttrs): class MentionStruct(SerializableAttrs):
at: list[int] at: list[int]
len: int len: int
user_id: Long | int user_id: Union[Long, int]

View File

@ -31,7 +31,7 @@ class KnownChannelType(str, Enum):
return str in [KnownChannelType.DirectChat, KnownChannelType.OD] return str in [KnownChannelType.DirectChat, KnownChannelType.OD]
ChannelType = KnownChannelType | str # Substitute for ChannelType = "name1" | ... | "nameN" | str ChannelType = Union[KnownChannelType, str] # Substitute for ChannelType = "name1" | ... | "nameN" | str
__all__ = [ __all__ = [

View File

@ -13,6 +13,8 @@
# #
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
from typing import Optional, Union
from attr import dataclass from attr import dataclass
from enum import Enum, IntEnum from enum import Enum, IntEnum
@ -35,7 +37,7 @@ class KnownChannelMetaType(IntEnum):
OPEN_CHANNEL_CHAT = 13 OPEN_CHANNEL_CHAT = 13
BOT = 14 BOT = 14
ChannelMetaType = KnownChannelMetaType | int ChannelMetaType = Union[KnownChannelMetaType, int]
class ChannelClientMetaType(str, Enum): class ChannelClientMetaType(str, Enum):
@ -53,20 +55,20 @@ class ChannelClientMetaType(str, Enum):
class ChannelMetaStruct(SerializableAttrs): class ChannelMetaStruct(SerializableAttrs):
type: ChannelMetaType type: ChannelMetaType
revision: Long revision: Long
authorId: Long | None = None authorId: Optional[Long] = None
content: str content: str
updatedAt: int updatedAt: int
@dataclass(kw_only=True) @dataclass(kw_only=True)
class ChannelClientMetaStruct(SerializableAttrs): class ChannelClientMetaStruct(SerializableAttrs):
name: str | None = None name: Optional[str] = None
image_path: str | None = None image_path: Optional[str] = None
favourite: bool | None = None favourite: Optional[bool] = None
push_sound: bool | None = None push_sound: Optional[bool] = None
chat_hide: bool | None = None chat_hide: Optional[bool] = None
fullImageUrl: str | None = None fullImageUrl: Optional[str] = None
imageUrl: str | None = None imageUrl: Optional[str] = None
@dataclass @dataclass
@ -88,7 +90,7 @@ class TvMetaContent(SerializableAttrs):
@dataclass(kw_only=True) @dataclass(kw_only=True)
class TvLiveMetaContent(SerializableAttrs): class TvLiveMetaContent(SerializableAttrs):
url: str url: str
live: str | None = 'on' live: Optional[str] = 'on'
@dataclass @dataclass
@ -96,7 +98,7 @@ class LiveTalkInfoOnMetaContent(SerializableAttrs):
liveon: bool liveon: bool
title: str title: str
startTime: int startTime: int
userId: int | Long userId: Union[int, Long]
csIP: str csIP: str
csIP6: str csIP6: str
csPort: int csPort: int
@ -106,17 +108,17 @@ class LiveTalkInfoOnMetaContent(SerializableAttrs):
@dataclass(kw_only=True) @dataclass(kw_only=True)
class LiveTalkInfoOffMetaContent(SerializableAttrs): class LiveTalkInfoOffMetaContent(SerializableAttrs):
"""Substitute for LiveTalkInfoOffMetaContent extends Partial<LiveTalkInfoOffMetaContent>""" """Substitute for LiveTalkInfoOffMetaContent extends Partial<LiveTalkInfoOffMetaContent>"""
liveon: bool liveon: bool = False
title: str | None = None title: Optional[str] = None
startTime: int | None = None startTime: Optional[int] = None
userId: int | Long | None userId: Union[int, Long, None] = None
csIP: str | None = None csIP: Optional[str] = None
csIP6: str | None = None csIP6: Optional[str] = None
csPort: int | None = None csPort: Optional[int] = None
callId: str | None = None callId: Optional[str] = None
LiveTalkInfoMetaContent = LiveTalkInfoOnMetaContent | LiveTalkInfoOffMetaContent; LiveTalkInfoMetaContent = Union[LiveTalkInfoOnMetaContent, LiveTalkInfoOffMetaContent]
@dataclass @dataclass
@ -147,10 +149,10 @@ BotDelCommandStruct = BotCommandStruct
@dataclass(kw_only=True) @dataclass(kw_only=True)
class BotMetaContent(SerializableAttrs): class BotMetaContent(SerializableAttrs):
add: list[BotAddCommandStruct] | None = None add: Optional[list[BotAddCommandStruct]] = None
update: list[BotAddCommandStruct] | None = None update: Optional[list[BotAddCommandStruct]] = None
full: list[BotAddCommandStruct] | None = None full: Optional[list[BotAddCommandStruct]] = None
delete: list[BotDelCommandStruct] | None = field(json="del", default=None) delete: Optional[list[BotDelCommandStruct]] = field(json="del", default=None)
__all__ = [ __all__ = [

View File

@ -13,6 +13,8 @@
# #
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
from typing import Optional, Union
from attr import dataclass from attr import dataclass
from mautrix.types import SerializableAttrs from mautrix.types import SerializableAttrs
@ -29,9 +31,9 @@ class ChatTypeComponent(SerializableAttrs):
@dataclass(kw_only=True) @dataclass(kw_only=True)
class Chat(ChatTypeComponent): class Chat(ChatTypeComponent):
text: str | None = None text: Optional[str] = None
attachment: Attachment | None = None attachment: Optional[Attachment] = None
supplement: dict | None = None supplement: Optional[dict] = None
@dataclass @dataclass
@ -57,7 +59,7 @@ class ChatLogLinked(ChatLogged):
class ChatWritten(Chat): class ChatWritten(Chat):
sender: ChannelUser sender: ChannelUser
sendAt: int sendAt: int
messageId: int | Long messageId: Union[int, Long]
@dataclass @dataclass
@ -73,7 +75,7 @@ class TypedChatlog(Chatlog, TypedChat):
@dataclass(kw_only=True) @dataclass(kw_only=True)
class ChatOptions(SerializableAttrs): class ChatOptions(SerializableAttrs):
shout: bool | None = None shout: Optional[bool] = None
__all__ = [ __all__ = [

View File

@ -13,6 +13,8 @@
# #
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
from typing import Union
from enum import IntEnum from enum import IntEnum
@ -54,7 +56,7 @@ class KnownChatType(IntEnum):
OPEN_VOTE = 97 OPEN_VOTE = 97
OPEN_POST = 98 OPEN_POST = 98
ChatType = KnownChatType | int ChatType = Union[KnownChatType, int]
DELETED_MESSAGE_OFFSET = 16384; DELETED_MESSAGE_OFFSET = 16384;

View File

@ -13,7 +13,7 @@
# #
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
from typing import NewType from typing import NewType, Union
from attr import dataclass from attr import dataclass
@ -24,7 +24,7 @@ from ..channel.channel_info import ChannelLoginData, NormalChannelData
from ..openlink.open_channel_info import OpenChannelData from ..openlink.open_channel_info import OpenChannelData
ChannelLoginDataItem = NewType("ChannelLoginDataItem", ChannelLoginData[NormalChannelData | OpenChannelData]) ChannelLoginDataItem = NewType("ChannelLoginDataItem", ChannelLoginData[Union[NormalChannelData, OpenChannelData]])
@deserializer(ChannelLoginDataItem) @deserializer(ChannelLoginDataItem)
def deserialize_channel_login_data_item(data: JSON) -> ChannelLoginDataItem: def deserialize_channel_login_data_item(data: JSON) -> ChannelLoginDataItem:

View File

@ -21,7 +21,7 @@ from .open_link_user_info import *
""" """
from typing import TYPE_CHECKING from typing import TYPE_CHECKING, Optional, Union
from attr import dataclass from attr import dataclass
from enum import IntEnum from enum import IntEnum
@ -56,7 +56,7 @@ class KnownLinkPrivilegeMask(IntEnum):
NON_SPECIAL_LINK = 512 NON_SPECIAL_LINK = 512
USE_BOT = 1024 USE_BOT = 1024
LinkPrivilegeMask = KnownLinkPrivilegeMask | int | Long; LinkPrivilegeMask = Union[KnownLinkPrivilegeMask, int, Long]
@dataclass @dataclass
@ -67,8 +67,8 @@ class OpenPrivilegeComponent(SerializableAttrs):
@dataclass(kw_only=True) @dataclass(kw_only=True)
class OpenLinkSettings(SerializableAttrs): class OpenLinkSettings(SerializableAttrs):
linkName: str linkName: str
linkCoverURL: str | None = None linkCoverURL: Optional[str] = None
description: str | None = None description: Optional[str] = None
searchable: bool searchable: bool
activated: bool activated: bool
@ -109,12 +109,12 @@ class InformedOpenLink(SerializableAttrs):
@dataclass(kw_only=True) @dataclass(kw_only=True)
class OpenLinkUpdateTemplate(SerializableAttrs): class OpenLinkUpdateTemplate(SerializableAttrs):
passcode: str | None = None passcode: Optional[str] = None
@dataclass(kw_only=True) @dataclass(kw_only=True)
class OpenLinkCreateTemplate(SerializableAttrs): class OpenLinkCreateTemplate(SerializableAttrs):
mainProfileOnly: bool | None = None mainProfileOnly: Optional[bool] = None
@dataclass @dataclass

View File

@ -13,7 +13,7 @@
# #
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
from typing import Generic, Type, TypeVar from typing import Generic, Type, TypeVar, Union
from attr import dataclass from attr import dataclass
from enum import IntEnum from enum import IntEnum
@ -66,12 +66,12 @@ class KnownDataStatusCode(IntEnum):
UPDATE_REQUIRED = -999 UPDATE_REQUIRED = -999
SERVER_UNDER_MAINTENANCE = -9797 SERVER_UNDER_MAINTENANCE = -9797
DataStatusCode = KnownDataStatusCode | int DataStatusCode = Union[KnownDataStatusCode, int]
@dataclass @dataclass
class ResponseState(SerializableAttrs): class ResponseState(SerializableAttrs):
status: DataStatusCode | KnownAuthStatusCode # NOTE Added KnownAuthStatusCode status: Union[DataStatusCode, KnownAuthStatusCode] # NOTE Added KnownAuthStatusCode
@dataclass @dataclass
@ -96,7 +96,7 @@ class CommandResultDoneValue(RootCommandResult, Generic[ResultType]):
def deserialize_result( def deserialize_result(
result_type: Type[ResultType], data: JSON result_type: Type[ResultType], data: JSON
) -> CommandResultDoneValue[ResultType] | RootCommandResult: ) -> Union[CommandResultDoneValue[ResultType], RootCommandResult]:
"""Returns equivalent of CommandResult<T>. Does no consistency checking on success & result properties.""" """Returns equivalent of CommandResult<T>. Does no consistency checking on success & result properties."""
if "result" in data: if "result" in data:
# TODO Allow arbitrary result object? # TODO Allow arbitrary result object?

View File

@ -15,7 +15,7 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
from __future__ import annotations from __future__ import annotations
from typing import TYPE_CHECKING from typing import TYPE_CHECKING, Union
import time import time
from mautrix.bridge import BaseMatrixHandler from mautrix.bridge import BaseMatrixHandler
@ -249,7 +249,7 @@ class MatrixHandler(BaseMatrixHandler):
""" """
async def handle_ephemeral_event( async def handle_ephemeral_event(
self, evt: ReceiptEvent | PresenceEvent | TypingEvent self, evt: Union[ReceiptEvent, PresenceEvent, TypingEvent]
) -> None: ) -> None:
if evt.type == EventType.TYPING: if evt.type == EventType.TYPING:
await self.handle_typing(evt.room_id, evt.content.user_ids) await self.handle_typing(evt.room_id, evt.content.user_ids)