Skip to content

Commit 32624ef

Browse files
committed
Improve type hints
1 parent 4ebf5cf commit 32624ef

File tree

189 files changed

+729
-815
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

189 files changed

+729
-815
lines changed

compiler/api/template/combinator.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class {name}(TLObject): # type: ignore
2727
{read_types}
2828
return {name}({return_arguments})
2929

30-
def write(self) -> bytes:
30+
def write(self, *args) -> bytes:
3131
b = BytesIO()
3232
b.write(Int(self.ID, False))
3333

pyrogram/client.py

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,19 @@
2121
import inspect
2222
import logging
2323
import os
24+
import platform
2425
import re
2526
import shutil
27+
import sys
2628
import tempfile
2729
from concurrent.futures.thread import ThreadPoolExecutor
2830
from configparser import ConfigParser
2931
from hashlib import sha256
3032
from importlib import import_module
33+
from io import StringIO
34+
from mimetypes import MimeTypes
3135
from pathlib import Path
32-
from typing import Union, List, Optional
36+
from typing import Union, List, Optional, Callable
3337

3438
import pyrogram
3539
from pyrogram import __version__, __license__
@@ -51,12 +55,14 @@
5155
from pyrogram.utils import ainput
5256
from .dispatcher import Dispatcher
5357
from .file_id import FileId, FileType, ThumbnailSource
54-
from .scaffold import Scaffold
58+
from .mime_types import mime_types
59+
from .parser import Parser
60+
from .session.internals import MsgId
5561

5662
log = logging.getLogger(__name__)
5763

5864

59-
class Client(Methods, Scaffold):
65+
class Client(Methods):
6066
"""Pyrogram Client, the main means for interacting with Telegram.
6167
6268
Parameters:
@@ -177,10 +183,26 @@ class Client(Methods, Scaffold):
177183
terminal environments.
178184
"""
179185

186+
APP_VERSION = f"Pyrogram {__version__}"
187+
DEVICE_MODEL = f"{platform.python_implementation()} {platform.python_version()}"
188+
SYSTEM_VERSION = f"{platform.system()} {platform.release()}"
189+
190+
LANG_CODE = "en"
191+
192+
PARENT_DIR = Path(sys.argv[0]).parent
193+
194+
INVITE_LINK_RE = re.compile(r"^(?:https?://)?(?:www\.)?(?:t(?:elegram)?\.(?:org|me|dog)/(?:joinchat/|\+))([\w-]+)$")
195+
WORKERS = min(32, (os.cpu_count() or 0) + 4) # os.cpu_count() can be None
196+
WORKDIR = PARENT_DIR
197+
CONFIG_FILE = PARENT_DIR / "config.ini"
198+
199+
mimetypes = MimeTypes()
200+
mimetypes.readfp(StringIO(mime_types))
201+
180202
def __init__(
181203
self,
182204
session_name: Union[str, Storage],
183-
api_id: Union[int, str] = None,
205+
api_id: int = None,
184206
api_hash: str = None,
185207
app_version: str = None,
186208
device_model: str = None,
@@ -194,9 +216,9 @@ def __init__(
194216
phone_code: str = None,
195217
password: str = None,
196218
force_sms: bool = False,
197-
workers: int = Scaffold.WORKERS,
198-
workdir: str = Scaffold.WORKDIR,
199-
config_file: str = Scaffold.CONFIG_FILE,
219+
workers: int = WORKERS,
220+
workdir: str = WORKDIR,
221+
config_file: str = CONFIG_FILE,
200222
plugins: dict = None,
201223
parse_mode: "enums.ParseMode" = enums.ParseMode.DEFAULT,
202224
no_updates: bool = None,
@@ -207,7 +229,7 @@ def __init__(
207229
super().__init__()
208230

209231
self.session_name = session_name
210-
self.api_id = int(api_id) if api_id else None
232+
self.api_id = api_id
211233
self.api_hash = api_hash
212234
self.app_version = app_version
213235
self.device_model = device_model
@@ -246,6 +268,24 @@ def __init__(
246268
raise ValueError("Unknown storage engine")
247269

248270
self.dispatcher = Dispatcher(self)
271+
272+
self.rnd_id = MsgId
273+
274+
self.parser = Parser(self)
275+
self.parse_mode = enums.ParseMode.DEFAULT
276+
277+
self.session = None
278+
279+
self.media_sessions = {}
280+
self.media_sessions_lock = asyncio.Lock()
281+
282+
self.is_connected = None
283+
self.is_initialized = None
284+
285+
self.takeout_id = None
286+
287+
self.disconnect_handler = None
288+
249289
self.loop = asyncio.get_event_loop()
250290

251291
def __enter__(self):
@@ -790,7 +830,7 @@ async def get_file(
790830
self,
791831
file_id: FileId,
792832
file_size: int,
793-
progress: callable,
833+
progress: Callable,
794834
progress_args: tuple = ()
795835
) -> str:
796836
dc_id = file_id.dc_id

pyrogram/filters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def create(func: Callable, name: str = None, **kwargs) -> Filter:
127127
Custom filters give you extra control over which updates are allowed or not to be processed by your handlers.
128128
129129
Parameters:
130-
func (``callable``):
130+
func (``Callable``):
131131
A function that accepts three positional arguments *(filter, client, update)* and returns a boolean: True if the
132132
update should be handled, False otherwise.
133133
The *filter* argument refers to the filter itself and can be used to access keyword arguments (read below).

pyrogram/handlers/callback_query_handler.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19+
from typing import Callable
20+
1921
from .handler import Handler
2022

2123

@@ -27,7 +29,7 @@ class CallbackQueryHandler(Handler):
2729
:meth:`~pyrogram.Client.on_callback_query` decorator.
2830
2931
Parameters:
30-
callback (``callable``):
32+
callback (``Callable``):
3133
Pass a function that will be called when a new CallbackQuery arrives. It takes *(client, callback_query)*
3234
as positional arguments (look at the section below for a detailed description).
3335
@@ -43,5 +45,5 @@ class CallbackQueryHandler(Handler):
4345
The received callback query.
4446
"""
4547

46-
def __init__(self, callback: callable, filters=None):
48+
def __init__(self, callback: Callable, filters=None):
4749
super().__init__(callback, filters)

pyrogram/handlers/chat_join_request_handler.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19+
from typing import Callable
20+
1921
from .handler import Handler
2022

2123

@@ -27,7 +29,7 @@ class ChatJoinRequestHandler(Handler):
2729
:meth:`~pyrogram.Client.on_chat_join_request` decorator.
2830
2931
Parameters:
30-
callback (``callable``):
32+
callback (``Callable``):
3133
Pass a function that will be called when a new ChatJoinRequest event arrives. It takes
3234
*(client, chat_join_request)* as positional arguments (look at the section below for a detailed
3335
description).
@@ -43,5 +45,5 @@ class ChatJoinRequestHandler(Handler):
4345
The received chat join request.
4446
"""
4547

46-
def __init__(self, callback: callable, filters=None):
48+
def __init__(self, callback: Callable, filters=None):
4749
super().__init__(callback, filters)

pyrogram/handlers/chat_member_updated_handler.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19+
from typing import Callable
20+
1921
from .handler import Handler
2022

2123

@@ -27,7 +29,7 @@ class ChatMemberUpdatedHandler(Handler):
2729
:meth:`~pyrogram.Client.on_chat_member_updated` decorator.
2830
2931
Parameters:
30-
callback (``callable``):
32+
callback (``Callable``):
3133
Pass a function that will be called when a new ChatMemberUpdated event arrives. It takes
3234
*(client, chat_member_updated)* as positional arguments (look at the section below for a detailed
3335
description).
@@ -43,5 +45,5 @@ class ChatMemberUpdatedHandler(Handler):
4345
The received chat member update.
4446
"""
4547

46-
def __init__(self, callback: callable, filters=None):
48+
def __init__(self, callback: Callable, filters=None):
4749
super().__init__(callback, filters)

pyrogram/handlers/chosen_inline_result_handler.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19+
from typing import Callable
20+
1921
from .handler import Handler
2022

2123

@@ -27,7 +29,7 @@ class ChosenInlineResultHandler(Handler):
2729
:meth:`~pyrogram.Client.on_chosen_inline_result` decorator.
2830
2931
Parameters:
30-
callback (``callable``):
32+
callback (``Callable``):
3133
Pass a function that will be called when a new chosen inline result arrives.
3234
It takes *(client, chosen_inline_result)* as positional arguments (look at the section below for a
3335
detailed description).
@@ -44,5 +46,5 @@ class ChosenInlineResultHandler(Handler):
4446
The received chosen inline result.
4547
"""
4648

47-
def __init__(self, callback: callable, filters=None):
49+
def __init__(self, callback: Callable, filters=None):
4850
super().__init__(callback, filters)

pyrogram/handlers/deleted_messages_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class DeletedMessagesHandler(Handler):
3232
:meth:`~pyrogram.Client.on_deleted_messages` decorator.
3333
3434
Parameters:
35-
callback (``callable``):
35+
callback (``Callable``):
3636
Pass a function that will be called when one or more messages have been deleted.
3737
It takes *(client, messages)* as positional arguments (look at the section below for a detailed description).
3838

pyrogram/handlers/disconnect_handler.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19+
from typing import Callable
20+
1921
from .handler import Handler
2022

2123

@@ -27,7 +29,7 @@ class DisconnectHandler(Handler):
2729
:meth:`~pyrogram.Client.on_disconnect` decorator.
2830
2931
Parameters:
30-
callback (``callable``):
32+
callback (``Callable``):
3133
Pass a function that will be called when a disconnection occurs. It takes *(client)*
3234
as positional argument (look at the section below for a detailed description).
3335
@@ -37,5 +39,5 @@ class DisconnectHandler(Handler):
3739
is established.
3840
"""
3941

40-
def __init__(self, callback: callable):
42+
def __init__(self, callback: Callable):
4143
super().__init__(callback)

pyrogram/handlers/inline_query_handler.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19+
from typing import Callable
20+
1921
from .handler import Handler
2022

2123

@@ -27,7 +29,7 @@ class InlineQueryHandler(Handler):
2729
:meth:`~pyrogram.Client.on_inline_query` decorator.
2830
2931
Parameters:
30-
callback (``callable``):
32+
callback (``Callable``):
3133
Pass a function that will be called when a new InlineQuery arrives. It takes *(client, inline_query)*
3234
as positional arguments (look at the section below for a detailed description).
3335
@@ -43,5 +45,5 @@ class InlineQueryHandler(Handler):
4345
The received inline query.
4446
"""
4547

46-
def __init__(self, callback: callable, filters=None):
48+
def __init__(self, callback: Callable, filters=None):
4749
super().__init__(callback, filters)

0 commit comments

Comments
 (0)