Skip to content
This repository was archived by the owner on Dec 23, 2024. It is now read-only.

Commit 1dc4df8

Browse files
Improve typing hints (#537)
* Change type1 or type2 to Union[type1, type2] * Address @KunoiSayami suggestions * Change Union[type1, None] to Optional[type1] * Update PR with latest commit changes * Address Dan suggestions
1 parent e1dac6c commit 1dc4df8

Some content is hidden

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

47 files changed

+121
-110
lines changed

pyrogram/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from hashlib import sha256
3030
from importlib import import_module
3131
from pathlib import Path
32-
from typing import Union, List
32+
from typing import Union, List, Optional
3333

3434
import pyrogram
3535
from pyrogram import raw
@@ -396,7 +396,7 @@ def parse_mode(self):
396396
return self._parse_mode
397397

398398
@parse_mode.setter
399-
def parse_mode(self, parse_mode: Union[str, None] = "combined"):
399+
def parse_mode(self, parse_mode: Optional[str] = "combined"):
400400
if parse_mode not in self.PARSE_MODES:
401401
raise ValueError('parse_mode must be one of {} or None. Not "{}"'.format(
402402
", ".join(f'"{m}"' for m in self.PARSE_MODES[:-1]),
@@ -406,7 +406,7 @@ def parse_mode(self, parse_mode: Union[str, None] = "combined"):
406406
self._parse_mode = parse_mode
407407

408408
# TODO: redundant, remove in next major version
409-
def set_parse_mode(self, parse_mode: Union[str, None] = "combined"):
409+
def set_parse_mode(self, parse_mode: Optional[str] = "combined"):
410410
"""Set the parse mode to be used globally by the client.
411411
412412
When setting the parse mode with this method, all other methods having a *parse_mode* parameter will follow the

pyrogram/connection/connection.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import asyncio
2020
import logging
2121

22+
from typing import Optional
23+
2224
from .transport import *
2325
from ..session.internals import DataCenter
2426

@@ -79,5 +81,5 @@ async def send(self, data: bytes):
7981
except Exception:
8082
raise OSError
8183

82-
async def recv(self) -> bytes or None:
84+
async def recv(self) -> Optional[bytes]:
8385
return await self.protocol.recv()

pyrogram/connection/transport/tcp/tcp_abridged.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import logging
2020

21+
from typing import Optional
22+
2123
from .tcp import TCP
2224

2325
log = logging.getLogger(__name__)
@@ -41,7 +43,7 @@ async def send(self, data: bytes, *args):
4143
+ data
4244
)
4345

44-
async def recv(self, length: int = 0) -> bytes or None:
46+
async def recv(self, length: int = 0) -> Optional[bytes]:
4547
length = await super().recv(1)
4648

4749
if length is None:

pyrogram/connection/transport/tcp/tcp_abridged_o.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
import logging
2020
import os
2121

22+
from typing import Optional
23+
2224
from pyrogram import utils
25+
2326
from pyrogram.crypto import aes
2427
from .tcp import TCP
2528

@@ -61,7 +64,7 @@ async def send(self, data: bytes, *args):
6164

6265
await super().send(payload)
6366

64-
async def recv(self, length: int = 0) -> bytes or None:
67+
async def recv(self, length: int = 0) -> Optional[bytes]:
6568
length = await super().recv(1)
6669

6770
if length is None:

pyrogram/connection/transport/tcp/tcp_full.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

1919
import logging
20+
from typing import Optional
2021
from binascii import crc32
2122
from struct import pack, unpack
2223

@@ -42,7 +43,7 @@ async def send(self, data: bytes, *args):
4243

4344
await super().send(data)
4445

45-
async def recv(self, length: int = 0) -> bytes or None:
46+
async def recv(self, length: int = 0) -> Optional[bytes]:
4647
length = await super().recv(4)
4748

4849
if length is None:

pyrogram/connection/transport/tcp/tcp_intermediate.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

1919
import logging
20+
from typing import Optional
2021
from struct import pack, unpack
2122

2223
from .tcp import TCP
@@ -35,7 +36,7 @@ async def connect(self, address: tuple):
3536
async def send(self, data: bytes, *args):
3637
await super().send(pack("<i", len(data)) + data)
3738

38-
async def recv(self, length: int = 0) -> bytes or None:
39+
async def recv(self, length: int = 0) -> Optional[bytes]:
3940
length = await super().recv(4)
4041

4142
if length is None:

pyrogram/connection/transport/tcp/tcp_intermediate_o.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import logging
2020
import os
21+
from typing import Optional
2122
from struct import pack, unpack
2223

2324
from pyrogram.crypto import aes
@@ -62,7 +63,7 @@ async def send(self, data: bytes, *args):
6263
)
6364
)
6465

65-
async def recv(self, length: int = 0) -> bytes or None:
66+
async def recv(self, length: int = 0) -> Optional[bytes]:
6667
length = await super().recv(4)
6768

6869
if length is None:

pyrogram/errors/rpc_error.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import re
2020
from datetime import datetime
2121
from importlib import import_module
22-
from typing import Type
22+
from typing import Type, Union
2323

2424
from pyrogram import raw
2525
from pyrogram.raw.core import TLObject
@@ -32,7 +32,7 @@ class RPCError(Exception):
3232
NAME = None
3333
MESSAGE = "{x}"
3434

35-
def __init__(self, x: int or raw.types.RpcError = None, rpc_name: str = None, is_unknown: bool = False):
35+
def __init__(self, x: Union[int, raw.types.RpcError] = None, rpc_name: str = None, is_unknown: bool = False):
3636
super().__init__("[{} {}]: {} {}".format(
3737
self.CODE,
3838
self.ID or self.NAME,

pyrogram/filters.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ async def linked_channel_filter(_, __, m: Message):
699699
# endregion
700700

701701

702-
def command(commands: str or List[str], prefixes: str or List[str] = "/", case_sensitive: bool = False):
702+
def command(commands: Union[str, List[str]], prefixes: Union[str, List[str]] = "/", case_sensitive: bool = False):
703703
"""Filter commands, i.e.: text messages starting with "/" or any other custom prefix.
704704
705705
Parameters:
@@ -824,7 +824,7 @@ class user(Filter, set):
824824
Defaults to None (no users).
825825
"""
826826

827-
def __init__(self, users: int or str or list = None):
827+
def __init__(self, users: Union[int, str, List[Union[int, str]]] = None):
828828
users = [] if users is None else users if isinstance(users, list) else [users]
829829

830830
super().__init__(
@@ -856,7 +856,7 @@ class chat(Filter, set):
856856
Defaults to None (no chats).
857857
"""
858858

859-
def __init__(self, chats: int or str or list = None):
859+
def __init__(self, chats: Union[int, str, List[Union[int, str]]] = None):
860860
chats = [] if chats is None else chats if isinstance(chats, list) else [chats]
861861

862862
super().__init__(

pyrogram/methods/chats/set_slow_mode.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
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 Union
19+
from typing import Union, Optional
2020

2121
from pyrogram import raw
2222
from pyrogram.scaffold import Scaffold
@@ -26,7 +26,7 @@ class SetSlowMode(Scaffold):
2626
async def set_slow_mode(
2727
self,
2828
chat_id: Union[int, str],
29-
seconds: Union[int, None]
29+
seconds: Optional[int]
3030
) -> bool:
3131
"""Set the slow mode interval for a chat.
3232

0 commit comments

Comments
 (0)