Skip to content

Commit cea867a

Browse files
committed
mypy fix
1 parent 1d8c00c commit cea867a

File tree

9 files changed

+47
-59
lines changed

9 files changed

+47
-59
lines changed

pybotx/async_buffer.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import abc
12
import os
23
from typing import Optional
34

@@ -18,10 +19,12 @@ async def tell(self) -> int: ... # noqa: WPS428
1819

1920

2021
class AsyncBufferWritable(AsyncBufferBase):
22+
@abc.abstractmethod
2123
async def write(self, content: bytes) -> int: ... # noqa: WPS428
2224

2325

2426
class AsyncBufferReadable(AsyncBufferBase):
27+
@abc.abstractmethod
2528
async def read(
2629
self,
2730
bytes_to_read: Optional[int] = None,

pybotx/bot/handler.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
from dataclasses import dataclass
22
from functools import partial
3-
from typing import TYPE_CHECKING, Awaitable, Callable, List, Literal, TypeVar, Union
3+
from typing import (
4+
TYPE_CHECKING,
5+
Awaitable,
6+
Callable,
7+
List,
8+
Literal,
9+
TypeVar,
10+
Union,
11+
cast,
12+
)
413

514
from pybotx.models.commands import BotCommand
615
from pybotx.models.message.incoming_message import IncomingMessage
@@ -69,7 +78,7 @@ async def __call__(self, message: IncomingMessage, bot: "Bot") -> None:
6978
handler_func = self.handler_func
7079

7180
for middleware in self.middlewares[::-1]:
72-
handler_func = partial(middleware, call_next=handler_func)
81+
handler_func = cast(HandlerFunc, partial(middleware, call_next=handler_func)) # type: ignore[type-arg, call-arg]
7382

7483
await handler_func(message, bot)
7584

pybotx/models/async_files.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ class Image(AsyncFileBase):
5252
@dataclass
5353
class Video(AsyncFileBase):
5454
type: Literal[AttachmentTypes.VIDEO]
55-
5655
duration: int
5756

5857

@@ -64,7 +63,6 @@ class Document(AsyncFileBase):
6463
@dataclass
6564
class Voice(AsyncFileBase):
6665
type: Literal[AttachmentTypes.VOICE]
67-
6866
duration: int
6967

7068

@@ -78,12 +76,6 @@ class APIAsyncFileBase(VerifiedPayloadBaseModel):
7876
file_hash: str
7977

8078
class Config:
81-
"""BotX sends extra fields which are used by client only.
82-
83-
We skip their validation, but extra fields will be saved during
84-
serialization/deserialization.
85-
"""
86-
8779
extra = "allow"
8880

8981

@@ -93,7 +85,6 @@ class ApiAsyncFileImage(APIAsyncFileBase):
9385

9486
class ApiAsyncFileVideo(APIAsyncFileBase):
9587
type: Literal[APIAttachmentTypes.VIDEO]
96-
9788
duration: int
9889

9990

@@ -103,7 +94,6 @@ class ApiAsyncFileDocument(APIAsyncFileBase):
10394

10495
class ApiAsyncFileVoice(APIAsyncFileBase):
10596
type: Literal[APIAttachmentTypes.VOICE]
106-
10797
duration: int
10898

10999

@@ -121,7 +111,6 @@ def convert_async_file_from_domain(file: File) -> APIAsyncFile:
121111
attachment_type = convert_attachment_type_from_domain(file.type)
122112

123113
if attachment_type == APIAttachmentTypes.IMAGE:
124-
attachment_type = cast(Literal[APIAttachmentTypes.IMAGE], attachment_type)
125114
file = cast(Image, file)
126115

127116
return ApiAsyncFileImage(
@@ -135,7 +124,6 @@ def convert_async_file_from_domain(file: File) -> APIAsyncFile:
135124
)
136125

137126
if attachment_type == APIAttachmentTypes.VIDEO:
138-
attachment_type = cast(Literal[APIAttachmentTypes.VIDEO], attachment_type)
139127
file = cast(Video, file)
140128

141129
return ApiAsyncFileVideo(
@@ -150,7 +138,6 @@ def convert_async_file_from_domain(file: File) -> APIAsyncFile:
150138
)
151139

152140
if attachment_type == APIAttachmentTypes.DOCUMENT:
153-
attachment_type = cast(Literal[APIAttachmentTypes.DOCUMENT], attachment_type)
154141
file = cast(Document, file)
155142

156143
return ApiAsyncFileDocument(
@@ -164,7 +151,6 @@ def convert_async_file_from_domain(file: File) -> APIAsyncFile:
164151
)
165152

166153
if attachment_type == APIAttachmentTypes.VOICE:
167-
attachment_type = cast(Literal[APIAttachmentTypes.VOICE], attachment_type)
168154
file = cast(Voice, file)
169155

170156
return ApiAsyncFileVoice(
@@ -185,7 +171,6 @@ def convert_async_file_to_domain(async_file: APIAsyncFile) -> File:
185171
attachment_type = convert_attachment_type_to_domain(async_file.type)
186172

187173
if attachment_type == AttachmentTypes.IMAGE:
188-
attachment_type = cast(Literal[AttachmentTypes.IMAGE], attachment_type)
189174
async_file = cast(ApiAsyncFileImage, async_file)
190175

191176
return Image(
@@ -200,7 +185,6 @@ def convert_async_file_to_domain(async_file: APIAsyncFile) -> File:
200185
)
201186

202187
if attachment_type == AttachmentTypes.VIDEO:
203-
attachment_type = cast(Literal[AttachmentTypes.VIDEO], attachment_type)
204188
async_file = cast(ApiAsyncFileVideo, async_file)
205189

206190
return Video(
@@ -216,7 +200,6 @@ def convert_async_file_to_domain(async_file: APIAsyncFile) -> File:
216200
)
217201

218202
if attachment_type == AttachmentTypes.DOCUMENT:
219-
attachment_type = cast(Literal[AttachmentTypes.DOCUMENT], attachment_type)
220203
async_file = cast(ApiAsyncFileDocument, async_file)
221204

222205
return Document(
@@ -231,7 +214,6 @@ def convert_async_file_to_domain(async_file: APIAsyncFile) -> File:
231214
)
232215

233216
if attachment_type == AttachmentTypes.VOICE:
234-
attachment_type = cast(Literal[AttachmentTypes.VOICE], attachment_type)
235217
async_file = cast(ApiAsyncFileVoice, async_file)
236218

237219
return Voice(

pybotx/models/attachments.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def convert_api_attachment_to_domain( # noqa: WPS212
220220
attachment_type = convert_attachment_type_to_domain(api_attachment.type)
221221

222222
if attachment_type == AttachmentTypes.IMAGE:
223-
attachment_type = cast(Literal[AttachmentTypes.IMAGE], attachment_type)
223+
attachment_type = cast(Literal[AttachmentTypes.IMAGE], attachment_type) # type: ignore[redundant-cast]
224224
api_attachment = cast(BotAPIAttachmentImage, api_attachment)
225225
content = decode_rfc2397(api_attachment.data.content)
226226

@@ -233,7 +233,7 @@ def convert_api_attachment_to_domain( # noqa: WPS212
233233
)
234234

235235
if attachment_type == AttachmentTypes.VIDEO:
236-
attachment_type = cast(Literal[AttachmentTypes.VIDEO], attachment_type)
236+
attachment_type = cast(Literal[AttachmentTypes.VIDEO], attachment_type) # type: ignore[redundant-cast]
237237
api_attachment = cast(BotAPIAttachmentVideo, api_attachment)
238238
content = decode_rfc2397(api_attachment.data.content)
239239

@@ -247,7 +247,7 @@ def convert_api_attachment_to_domain( # noqa: WPS212
247247
)
248248

249249
if attachment_type == AttachmentTypes.DOCUMENT:
250-
attachment_type = cast(Literal[AttachmentTypes.DOCUMENT], attachment_type)
250+
attachment_type = cast(Literal[AttachmentTypes.DOCUMENT], attachment_type) # type: ignore[redundant-cast]
251251
api_attachment = cast(BotAPIAttachmentDocument, api_attachment)
252252
content = decode_rfc2397(api_attachment.data.content)
253253

@@ -260,7 +260,7 @@ def convert_api_attachment_to_domain( # noqa: WPS212
260260
)
261261

262262
if attachment_type == AttachmentTypes.VOICE:
263-
attachment_type = cast(Literal[AttachmentTypes.VOICE], attachment_type)
263+
attachment_type = cast(Literal[AttachmentTypes.VOICE], attachment_type) # type: ignore[redundant-cast]
264264
api_attachment = cast(BotAPIAttachmentVoice, api_attachment)
265265
content = decode_rfc2397(api_attachment.data.content)
266266
attachment_extension = get_attachment_extension_from_encoded_content(
@@ -277,7 +277,7 @@ def convert_api_attachment_to_domain( # noqa: WPS212
277277
)
278278

279279
if attachment_type == AttachmentTypes.LOCATION:
280-
attachment_type = cast(Literal[AttachmentTypes.LOCATION], attachment_type)
280+
attachment_type = cast(Literal[AttachmentTypes.LOCATION], attachment_type) # type: ignore[redundant-cast]
281281
api_attachment = cast(BotAPIAttachmentLocation, api_attachment)
282282

283283
return Location(
@@ -288,15 +288,14 @@ def convert_api_attachment_to_domain( # noqa: WPS212
288288
)
289289

290290
if attachment_type == AttachmentTypes.CONTACT:
291-
attachment_type = cast(Literal[AttachmentTypes.CONTACT], attachment_type)
291+
attachment_type = cast(Literal[AttachmentTypes.CONTACT], attachment_type) # type: ignore[redundant-cast]
292292
api_attachment = cast(BotAPIAttachmentContact, api_attachment)
293293

294294
return Contact(
295295
name=api_attachment.data.contact_name,
296296
)
297297

298298
if attachment_type == AttachmentTypes.LINK:
299-
attachment_type = cast(Literal[AttachmentTypes.LINK], attachment_type)
300299
api_attachment = cast(BotAPIAttachmentLink, api_attachment)
301300

302301
return Link(
@@ -307,7 +306,6 @@ def convert_api_attachment_to_domain( # noqa: WPS212
307306
)
308307

309308
if attachment_type == AttachmentTypes.STICKER:
310-
attachment_type = cast(Literal[AttachmentTypes.STICKER], attachment_type)
311309
api_attachment = cast(BotAPIAttachmentSticker, api_attachment)
312310

313311
return Sticker(

pybotx/models/message/incoming_message.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,9 @@ def _convert_bot_api_mention_to_domain(api_mention_data: BotAPIMentionData) -> M
154154

155155
def convert_bot_api_entity_to_domain(api_entity: BotAPIEntity) -> Entity:
156156
if api_entity.type == BotAPIEntityTypes.MENTION:
157-
api_entity = cast(BotAPIMention, api_entity)
158157
return _convert_bot_api_mention_to_domain(api_entity.data)
159158

160159
if api_entity.type == BotAPIEntityTypes.FORWARD:
161-
api_entity = cast(BotAPIForward, api_entity)
162160

163161
return Forward(
164162
chat_id=api_entity.data.group_chat_id,
@@ -170,7 +168,6 @@ def convert_bot_api_entity_to_domain(api_entity: BotAPIEntity) -> Entity:
170168
)
171169

172170
if api_entity.type == BotAPIEntityTypes.REPLY:
173-
api_entity = cast(BotAPIReply, api_entity)
174171

175172
mentions = MentionList()
176173
for api_mention_data in api_entity.data.mentions:

tests/client/test_botx_method_callback.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# type: ignore [attr-defined]
1+
# mypy: disable-error-code=attr-defined
22

33
import asyncio
44
import time

tests/system_events/factories.py

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import uuid
22
from typing import Any, Dict, List, Optional
33

4-
from factory.base import DictFactory # type: ignore
5-
from factory.declarations import SubFactory # type: ignore
4+
from factory.base import DictFactory
5+
from factory.declarations import SubFactory
66

77

8-
class DeviceMetaFactory(DictFactory): # type: ignore[misc]
9-
8+
class DeviceMetaFactory(DictFactory):
109
permissions: Optional[str] = None
1110
pushes: Optional[str] = None
1211
timezone: Optional[str] = None
1312

1413

15-
class FromFactory(DictFactory): # type: ignore[misc]
14+
class FromFactory(DictFactory):
1615
user_huid: Optional[str] = None
1716
group_chat_id: str = "8dada2c8-67a6-4434-9dec-570d244e78ee"
1817
ad_login: Optional[str] = None
@@ -22,7 +21,7 @@ class FromFactory(DictFactory): # type: ignore[misc]
2221
manufacturer: Optional[str] = None
2322
device: Optional[str] = None
2423
device_software: Optional[str] = None
25-
device_meta: Dict[str, Any] = SubFactory(DeviceMetaFactory) # noqa: F821
24+
device_meta: Any = SubFactory(DeviceMetaFactory) # type: ignore[no-untyped-call]
2625
platform: Optional[str] = None
2726
platform_package_id: Optional[str] = None
2827
is_admin: Optional[bool] = None
@@ -32,29 +31,24 @@ class FromFactory(DictFactory): # type: ignore[misc]
3231
host: str = "cts.ccteam.ru"
3332

3433

35-
class CommandDataFactory(DictFactory): # type: ignore[misc]
36-
34+
class CommandDataFactory(DictFactory):
3735
added_members: List[str] = [uuid.uuid4().hex, uuid.uuid4().hex]
3836

3937

40-
class CommandFactory(DictFactory): # type: ignore[misc]
41-
38+
class CommandFactory(DictFactory):
4239
body: str = "system:user_joined_to_chat"
4340
command_type: str = "system"
44-
data: Dict[str, Any] = SubFactory(CommandDataFactory) # noqa: F821
41+
data: Any = SubFactory(CommandDataFactory) # type: ignore[no-untyped-call]
4542
metadata: Dict[str, Any] = {}
4643

4744

48-
class BotAPIJoinToChatFactory(DictFactory): # type: ignore[misc]
49-
45+
class BotAPIJoinToChatFactory(DictFactory):
5046
sync_id: str = uuid.uuid4().hex
51-
command: Dict[str, Any] = SubFactory(CommandFactory) # noqa: F821
47+
command: Any = SubFactory(CommandFactory) # type: ignore[no-untyped-call]
5248
async_files: List[str] = []
5349
attachments: List[str] = []
5450
entities: List[str] = []
55-
from_: Dict[str, Any] = SubFactory(
56-
FromFactory,
57-
) # noqa: F821
51+
from_: Any = SubFactory(FromFactory) # type: ignore[no-untyped-call]
5852
bot_id: str = uuid.uuid4().hex
5953
proto_version: int = 4
6054
source_sync_id: Optional[str] = None
@@ -63,8 +57,7 @@ class Meta:
6357
rename = {"from_": "from"}
6458

6559

66-
class ConferenceChangedDataFactory(DictFactory): # type: ignore[misc]
67-
60+
class ConferenceChangedDataFactory(DictFactory):
6861
access_code = None
6962
actor = None
7063
added_users = ["5c053f2a-0bdf-4ab1-9bc9-256fee9db7ba"]

tests/system_events/test_conference_changed.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from datetime import datetime
2-
from typing import Any, Callable, Dict, Optional
2+
from typing import Any, Callable, Dict, Optional, cast
33
from uuid import UUID
44

55
import pytest
@@ -14,7 +14,7 @@
1414
)
1515
from pybotx.models.enums import ConferenceLinkTypes
1616
from pybotx.models.system_events.conference_changed import ConferenceChangedEvent
17-
from tests.system_events.factories import ConferenceChangedDataFactory # type: ignore
17+
from tests.system_events.factories import ConferenceChangedDataFactory
1818

1919
pytestmark = [
2020
pytest.mark.asyncio,
@@ -32,9 +32,12 @@ async def test__conference_changed_succeed(
3232
api_incoming_message_factory: Callable[..., Dict[str, Any]],
3333
) -> None:
3434
# - Arrange -
35-
conference_change_data = ConferenceChangedDataFactory(
36-
call_id=str(call_id),
37-
link_type="public",
35+
conference_change_data = cast(
36+
Dict[str, Any],
37+
ConferenceChangedDataFactory(
38+
call_id=str(call_id),
39+
link_type="public",
40+
), # type: ignore[no-untyped-call]
3841
)
3942
payload = api_incoming_message_factory(
4043
body="system:conference_changed",

tests/system_events/test_join_to_chat.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Optional
1+
from typing import Any, Dict, Optional, cast
22
from uuid import UUID
33

44
import pytest
@@ -13,7 +13,7 @@
1313
lifespan_wrapper,
1414
)
1515
from pybotx.models.system_events.user_joined_to_chat import JoinToChatEvent
16-
from tests.system_events.factories import BotAPIJoinToChatFactory # type: ignore
16+
from tests.system_events.factories import BotAPIJoinToChatFactory
1717

1818
pytestmark = [
1919
pytest.mark.asyncio,
@@ -33,7 +33,10 @@ async def test__join_to_chat__succeed(
3333
3. The registered user_joined_to_chat handler is called with this event
3434
"""
3535

36-
payload: dict[str, Any] = BotAPIJoinToChatFactory(bot_id=bot_account.id.hex)
36+
payload: Dict[str, Any] = cast(
37+
Dict[str, Any],
38+
BotAPIJoinToChatFactory(bot_id=bot_account.id.hex), # type: ignore[no-untyped-call]
39+
)
3740

3841
collector = HandlerCollector()
3942
join_to_chat: Optional[JoinToChatEvent] = None

0 commit comments

Comments
 (0)