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 typing import TYPE_CHECKING, cast, Awaitable, Callable, Type
from typing import TYPE_CHECKING, cast, Awaitable, Callable, Type, Optional, Union
import logging
import urllib.request
@ -116,7 +116,7 @@ class Client:
http: ClientSession
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."""
self.user = user
@ -149,8 +149,8 @@ class Client:
def get(
self,
url: str | URL,
headers: dict[str, str] | None = None,
url: Union[str, URL],
headers: Optional[dict[str, str]] = None,
**kwargs,
) -> _RequestContextManager:
# TODO Is auth ever needed?

View File

@ -15,6 +15,8 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""Custom wrapper classes around types defined by the KakaoTalk API."""
from typing import Optional, Union
from attr import dataclass
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
ChannelInfoUnion = NormalChannelInfo | OpenChannelInfo
UserInfoUnion = NormalChannelUserInfo | OpenChannelUserInfo
ChannelInfoUnion = Union[NormalChannelInfo, OpenChannelInfo]
UserInfoUnion = Union[NormalChannelUserInfo, OpenChannelUserInfo]
@dataclass
class PortalChannelInfo(SerializableAttrs):
name: str
#participants: list[PuppetUserInfo]
# 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
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from typing import Optional, Union
from attr import dataclass
from mautrix.types import SerializableAttrs, JSON
@ -34,9 +36,9 @@ class ProfileFeed(SerializableAttrs):
downloadId: str
contents: list[ProfileFeedObject]
url: str
serviceUrl: str | None = None # NOTE Made optional
serviceUrl: Optional[str] = None # NOTE Made optional
webUrl: str
serviceWebUrl: str | None = None # NOTE Made optional
serviceWebUrl: Optional[str] = None # NOTE Made optional
updatedAt: int
cursor: int
feedMessage: str
@ -99,7 +101,7 @@ class ProfileStruct(SerializableAttrs):
screenToken: int
# NEW
nickname: str
userId: int | str | Long # NOTE Should confirm this
userId: Union[int, str, Long] # NOTE Should confirm this
suspended: bool
meBadge: 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
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from typing import Union
from attr import dataclass
from mautrix.types import SerializableAttrs
@ -24,4 +26,4 @@ from ..bson import Long
class MentionStruct(SerializableAttrs):
at: list[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]
ChannelType = KnownChannelType | str # Substitute for ChannelType = "name1" | ... | "nameN" | str
ChannelType = Union[KnownChannelType, str] # Substitute for ChannelType = "name1" | ... | "nameN" | str
__all__ = [

View File

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

View File

@ -13,6 +13,8 @@
#
# 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/>.
from typing import Optional, Union
from attr import dataclass
from mautrix.types import SerializableAttrs
@ -29,9 +31,9 @@ class ChatTypeComponent(SerializableAttrs):
@dataclass(kw_only=True)
class Chat(ChatTypeComponent):
text: str | None = None
attachment: Attachment | None = None
supplement: dict | None = None
text: Optional[str] = None
attachment: Optional[Attachment] = None
supplement: Optional[dict] = None
@dataclass
@ -57,7 +59,7 @@ class ChatLogLinked(ChatLogged):
class ChatWritten(Chat):
sender: ChannelUser
sendAt: int
messageId: int | Long
messageId: Union[int, Long]
@dataclass
@ -73,7 +75,7 @@ class TypedChatlog(Chatlog, TypedChat):
@dataclass(kw_only=True)
class ChatOptions(SerializableAttrs):
shout: bool | None = None
shout: Optional[bool] = None
__all__ = [

View File

@ -13,6 +13,8 @@
#
# 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/>.
from typing import Union
from enum import IntEnum
@ -54,7 +56,7 @@ class KnownChatType(IntEnum):
OPEN_VOTE = 97
OPEN_POST = 98
ChatType = KnownChatType | int
ChatType = Union[KnownChatType, int]
DELETED_MESSAGE_OFFSET = 16384;

View File

@ -13,7 +13,7 @@
#
# 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/>.
from typing import NewType
from typing import NewType, Union
from attr import dataclass
@ -24,7 +24,7 @@ from ..channel.channel_info import ChannelLoginData, NormalChannelData
from ..openlink.open_channel_info import OpenChannelData
ChannelLoginDataItem = NewType("ChannelLoginDataItem", ChannelLoginData[NormalChannelData | OpenChannelData])
ChannelLoginDataItem = NewType("ChannelLoginDataItem", ChannelLoginData[Union[NormalChannelData, OpenChannelData]])
@deserializer(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 enum import IntEnum
@ -56,7 +56,7 @@ class KnownLinkPrivilegeMask(IntEnum):
NON_SPECIAL_LINK = 512
USE_BOT = 1024
LinkPrivilegeMask = KnownLinkPrivilegeMask | int | Long;
LinkPrivilegeMask = Union[KnownLinkPrivilegeMask, int, Long]
@dataclass
@ -67,8 +67,8 @@ class OpenPrivilegeComponent(SerializableAttrs):
@dataclass(kw_only=True)
class OpenLinkSettings(SerializableAttrs):
linkName: str
linkCoverURL: str | None = None
description: str | None = None
linkCoverURL: Optional[str] = None
description: Optional[str] = None
searchable: bool
activated: bool
@ -109,12 +109,12 @@ class InformedOpenLink(SerializableAttrs):
@dataclass(kw_only=True)
class OpenLinkUpdateTemplate(SerializableAttrs):
passcode: str | None = None
passcode: Optional[str] = None
@dataclass(kw_only=True)
class OpenLinkCreateTemplate(SerializableAttrs):
mainProfileOnly: bool | None = None
mainProfileOnly: Optional[bool] = None
@dataclass

View File

@ -13,7 +13,7 @@
#
# 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/>.
from typing import Generic, Type, TypeVar
from typing import Generic, Type, TypeVar, Union
from attr import dataclass
from enum import IntEnum
@ -66,12 +66,12 @@ class KnownDataStatusCode(IntEnum):
UPDATE_REQUIRED = -999
SERVER_UNDER_MAINTENANCE = -9797
DataStatusCode = KnownDataStatusCode | int
DataStatusCode = Union[KnownDataStatusCode, int]
@dataclass
class ResponseState(SerializableAttrs):
status: DataStatusCode | KnownAuthStatusCode # NOTE Added KnownAuthStatusCode
status: Union[DataStatusCode, KnownAuthStatusCode] # NOTE Added KnownAuthStatusCode
@dataclass
@ -96,7 +96,7 @@ class CommandResultDoneValue(RootCommandResult, Generic[ResultType]):
def deserialize_result(
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."""
if "result" in data:
# TODO Allow arbitrary result object?

View File

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