Skip to content

Commit 4b72f32

Browse files
committed
Fixes on structures
1 parent 7092b5d commit 4b72f32

File tree

16 files changed

+214
-273
lines changed

16 files changed

+214
-273
lines changed

setup.cfg

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = telegrambots-custom
3-
version = 0.0.6rc2
3+
version = 0.0.7rc0
44
author = immmdreza
55
author_email = ir310022@gmail.com
66
description = A custom extension packages for telegrambots.
@@ -21,7 +21,7 @@ packages = find:
2121
python_requires = >=3.10
2222
install_requires =
2323
aiohttp==3.8.1
24-
telegrambots==0.0.12rc0
24+
telegrambots==0.0.13rc0
2525
include-package-data = True
2626

2727
[options.packages.find]

src/telegrambots/custom/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
TextMessageContext,
88
CallbackQueryContext,
99
ContinueWithInfo,
10-
ContextTemplate,
10+
Context,
1111
)
1212
from .key_resolvers import (
1313
create_callback_query_key,
@@ -22,13 +22,13 @@
2222
"Dispatcher",
2323
"message_filters",
2424
"callback_query_filters",
25-
"ContextTemplate",
25+
"Context",
2626
"ContinueWithInfo",
2727
"MessageContext",
2828
"CallbackQueryContext",
2929
"create_callback_query_key",
3030
"create_message_key",
3131
"create_key",
3232
"KeyBuilder",
33-
"TextMessageContext"
33+
"TextMessageContext",
3434
]

src/telegrambots/custom/client.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional, cast, Union
1+
from typing import Any, Optional, cast, Union
22

33
from telegrambots.wrapper.client import TelegramBotsClient
44
from telegrambots.wrapper.types.methods import (
@@ -64,14 +64,14 @@
6464
SendGame,
6565
SendInvoice,
6666
SendLocation,
67-
SendDice,
68-
SendVideoNote,
69-
SendVoice,
70-
SetChatPermissions,
71-
SendContact,
72-
SendVenue,
73-
SendPhoto,
74-
SendPoll,
67+
# SendDice,
68+
# SendVideoNote,
69+
# SendVoice,
70+
# SetChatPermissions,
71+
# SendContact,
72+
# SendVenue,
73+
# SendPhoto,
74+
# SendPoll,
7575
)
7676
from telegrambots.wrapper.types.objects import (
7777
ForceReply,
@@ -183,7 +183,7 @@ async def get_updates(
183183
limit: int = 100,
184184
timeout: int = 0,
185185
allowed_updates: Optional[list[str]] = None,
186-
) -> list[Update]:
186+
) -> list[Update[Any]]:
187187
"""Use this method to receive incoming updates using long polling.
188188
189189
Args:

src/telegrambots/custom/contexts/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
from ._contexts.context_template import Context, ContextTemplate
1+
from ._contexts.context_template import Context
22
from ._contexts.message_context import MessageContext, TextMessageContext
33
from ._contexts.callback_query_context import CallbackQueryContext
44
from ._contexts._continuously_handler import ContinuouslyHandler, ContinueWithInfo
55

66
__all__ = [
7-
"ContextTemplate",
87
"Context",
98
"MessageContext",
109
"CallbackQueryContext",

src/telegrambots/custom/contexts/_contexts/_continuously_handler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
class ContinueWithInfo(Generic[TUpdate]):
1818
target_tag: str
1919
update_type: type[TUpdate]
20-
resolve_update: Callable[[Update], Optional[TUpdate]]
20+
resolve_update: Callable[[Update[TUpdate]], Optional[TUpdate]]
2121
keys: Sequence[AbstractKeyResolver[TUpdate, Any]]
2222
priority: int = 0
2323
args: tuple[Any, ...] = dataclasses.field(default_factory=tuple) # type: ignore
@@ -116,7 +116,7 @@ def with_message_from(
116116

117117

118118
class ContinuouslyHandlerTemplate:
119-
def check_keys(self, update: Update):
119+
def check_keys(self, update: Update[Any]):
120120
return all(key.is_key(update) for key in self.keys)
121121

122122
@property

src/telegrambots/custom/contexts/_contexts/callback_query_context.py

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,22 @@
77
InlineKeyboardMarkup,
88
)
99

10-
from .context_template import GenericContext
10+
from .context_template import Context
1111

1212
if TYPE_CHECKING:
1313
from ...dispatcher import Dispatcher
1414

1515

16-
class CallbackQueryContext(GenericContext[CallbackQuery]):
16+
class CallbackQueryContext(Context[CallbackQuery]):
1717
def __init__(
1818
self,
1919
dp: "Dispatcher",
20-
update: Update,
20+
update: Update[CallbackQuery],
2121
handler_tag: str,
2222
*args: Any,
2323
**kwargs: Any,
2424
) -> None:
25-
super().__init__(
26-
dp,
27-
update,
28-
CallbackQuery,
29-
handler_tag,
30-
*args,
31-
**kwargs,
32-
)
33-
34-
@final
35-
def __extractor__(self, update: Update) -> CallbackQuery:
36-
c = update.callback_query
37-
if c is not None:
38-
return c
39-
raise ValueError("Update has no callback query")
25+
super().__init__(dp, update, CallbackQuery, handler_tag, *args, **kwargs)
4026

4127
@final
4228
@property
Lines changed: 23 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
from abc import ABC, ABCMeta
21
from typing import (
32
Any,
4-
Callable,
53
Generic,
64
Mapping,
75
Optional,
@@ -11,7 +9,7 @@
119

1210
from telegrambots.wrapper.types.objects import Update
1311

14-
from ...general import Exctractable, TUpdate
12+
from ...general import TUpdate
1513
from ...extensions.context import PropagationExtension, ContinueWithExtensions
1614

1715

@@ -20,16 +18,16 @@
2018
from ...client import TelegramBot
2119

2220

23-
class ContextTemplate(metaclass=ABCMeta):
21+
class Context(Generic[TUpdate], Mapping[str, Any]):
2422
def __init__(
2523
self,
2624
dp: "Dispatcher",
27-
update: Update,
25+
update: Update[TUpdate],
2826
update_type: type[Any],
2927
handler_tag: str,
3028
*args: Any,
3129
**kwargs: Any,
32-
):
30+
) -> None:
3331
self.__dp = dp
3432
self.__update = update
3533
self.__update_type = update_type
@@ -41,6 +39,24 @@ def __init__(
4139
self.__propagation: Optional[PropagationExtension] = None
4240
self.__continue_with: Optional[ContinueWithExtensions] = None
4341

42+
def __getitem__(self, name: str):
43+
return self.kwargs[name]
44+
45+
def __setitem__(self, name: str, value: Any):
46+
self.kwargs[name] = value
47+
48+
def __iter__(self):
49+
return iter(self.kwargs)
50+
51+
def __len__(self) -> int:
52+
return len(self.kwargs)
53+
54+
@final
55+
@property
56+
def update(self) -> TUpdate:
57+
"""`TUpdate`: Update instance"""
58+
return self.wrapper_update.actual_update
59+
4460
@final
4561
@property
4662
def dp(self) -> "Dispatcher":
@@ -67,7 +83,7 @@ def handler_tag(self) -> str:
6783

6884
@final
6985
@property
70-
def wrapper_update(self) -> Update:
86+
def wrapper_update(self) -> Update[TUpdate]:
7187
return self.__update
7288

7389
@final
@@ -95,61 +111,3 @@ def continue_with(self) -> ContinueWithExtensions:
95111
if self.__continue_with is None:
96112
self.__continue_with = ContinueWithExtensions(self)
97113
return self.__continue_with
98-
99-
100-
class GenericContext(
101-
Generic[TUpdate], Exctractable[TUpdate], Mapping[str, Any], ContextTemplate, ABC
102-
):
103-
def __init__(
104-
self,
105-
dp: "Dispatcher",
106-
update: Update,
107-
update_type: type[Any],
108-
handler_tag: str,
109-
*args: Any,
110-
**kwargs: Any,
111-
) -> None:
112-
super().__init__(dp, update, update_type, handler_tag, *args, **kwargs)
113-
114-
def __getitem__(self, name: str):
115-
return self.kwargs[name]
116-
117-
def __setitem__(self, name: str, value: Any):
118-
self.kwargs[name] = value
119-
120-
def __iter__(self):
121-
return iter(self.kwargs)
122-
123-
def __len__(self) -> int:
124-
return len(self.kwargs)
125-
126-
@final
127-
@property
128-
def update(self) -> TUpdate:
129-
"""`TUpdate`: Update instance"""
130-
inner = self.__extractor__(self.wrapper_update)
131-
if inner is None:
132-
raise ValueError(f"Cannot exctract inner update.")
133-
return inner
134-
135-
136-
class Context(Generic[TUpdate], GenericContext[TUpdate]):
137-
def __init__(
138-
self,
139-
_exctractor: Callable[[Update], Optional[TUpdate]],
140-
dp: "Dispatcher",
141-
update: Update,
142-
update_type: type[Any],
143-
handler_tag: str,
144-
*args: Any,
145-
**kwargs: Any,
146-
) -> None:
147-
super().__init__(dp, update, update_type, handler_tag, *args, **kwargs)
148-
self.__extractor = _exctractor
149-
150-
@final
151-
def __extractor__(self, update: Update) -> TUpdate:
152-
r = self.__extractor(update)
153-
if r is None:
154-
raise ValueError(f"Cannot exctract inner update.")
155-
return r

src/telegrambots/custom/contexts/_contexts/message_context.py

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,22 @@
1010
ForceReply,
1111
)
1212

13-
from .context_template import GenericContext
13+
from .context_template import Context
1414

1515
if TYPE_CHECKING:
1616
from ...dispatcher import Dispatcher
1717

1818

19-
class MessageContext(GenericContext[Message]):
19+
class MessageContext(Context[Message]):
2020
def __init__(
2121
self,
2222
dp: "Dispatcher",
23-
update: Update,
23+
update: Update[Message],
2424
handler_tag: str,
2525
*args: Any,
26-
**kwargs: Any,
26+
**kwargs: Any
2727
) -> None:
28-
super().__init__(
29-
dp,
30-
update,
31-
Message,
32-
handler_tag,
33-
*args,
34-
**kwargs,
35-
)
36-
37-
@final
38-
def __extractor__(self, update: Update) -> Message:
39-
m = update.message
40-
if m is not None:
41-
return m
42-
raise ValueError("Update has no message")
28+
super().__init__(dp, update, Message, handler_tag, *args, **kwargs)
4329

4430
@final
4531
async def reply_text(
@@ -99,9 +85,15 @@ async def reply_text(
9985

10086
class TextMessageContext(MessageContext):
10187
def __init__(
102-
self, dp: "Dispatcher", update: Update, handler_tag: str, **kwargs: Any
88+
self,
89+
dp: "Dispatcher",
90+
update: Update[Message],
91+
update_type: type[Any],
92+
handler_tag: str,
93+
*args: Any,
94+
**kwargs: Any
10395
) -> None:
104-
super().__init__(dp, update=update, handler_tag=handler_tag, **kwargs)
96+
super().__init__(dp, update, handler_tag, *args, **kwargs)
10597

10698
@final
10799
@property

src/telegrambots/custom/dispatcher.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def __init__(
3737
self,
3838
_bot: "TelegramBot",
3939
*,
40-
processor_type: Optional[type[ProcessorTemplate[Update]]] = None,
40+
processor_type: Optional[type[ProcessorTemplate[Update[Any]]]] = None,
4141
) -> None:
4242

4343
"""Initializes the dispatcher.
@@ -52,9 +52,9 @@ def __init__(
5252
self._continuously_handlers: list[tuple[ContinuouslyHandlerTemplate]] = []
5353
self._handle_errors: list[AbstractExceptionHandler] = []
5454

55-
self._processor: ProcessorTemplate[Update]
55+
self._processor: ProcessorTemplate[Update[Any]]
5656
if processor_type is None:
57-
self._processor = SequentialProcessor[Update](self._process_update)
57+
self._processor = SequentialProcessor[Update[Any]](self._process_update)
5858
else:
5959
self._processor = processor_type(self._process_update)
6060

@@ -75,7 +75,7 @@ def add(self) -> AddExtensions:
7575
self.__add = AddExtensions(self)
7676
return self.__add
7777

78-
async def feed_update(self, update: Update):
78+
async def feed_update(self, update: Update[Any]):
7979
"""Feeds an update to the dispatcher.
8080
8181
Args:
@@ -173,7 +173,7 @@ async def _unlimited(self, *allowed_updates: str):
173173
async for update in self.bot.stream_updates(list(allowed_updates)):
174174
await self.feed_update(update)
175175

176-
async def _process_update(self, update: Update):
176+
async def _process_update(self, update: Update[Any]):
177177
update_type = update.update_type
178178
if update_type is None:
179179
await self._try_handle_error(ValueError(f"Unknown update type: {update}"))
@@ -234,15 +234,14 @@ async def _process_update(self, update: Update):
234234
async def _do_handling(
235235
self,
236236
handler: HandlerTemplate,
237-
update: Update,
237+
update: Update[Any],
238238
handler_tag: str,
239239
filter_data: Mapping[str, Any],
240240
*args: Any,
241241
**kwargs: Any,
242242
):
243243
try:
244244
await handler.process(
245-
self,
246245
update,
247246
filter_data,
248247
*args,

0 commit comments

Comments
 (0)