8000 Use Explicit Optionals (#3692) · Devors/python-telegram-bot@99fd443 · GitHub
[go: up one dir, main page]

Skip to content

Commit 99fd443

Browse files
Use Explicit Optionals (python-telegram-bot#3692)
Co-authored-by: Dmitry Kolomatskiy <58207913+lemontree210@users.noreply.github.com>
1 parent e432c29 commit 99fd443

File tree

146 files changed

+2798
-2637
lines changed

Some content is hidden

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

146 files changed

+2798
-2637
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ The following wonderful people contributed directly or indirectly to this projec
7777
- `Matheus Lemos <https://github.com/mlemosf>`_
7878
- `Michael Dix <https://github.com/Eisberge>`_
7979
- `Michael Elovskikh <https://github.com/wronglink>`_
80+
- `Miguel C. R. <https://github.com/MiguelX413>`_
8081
- `miles <https://github.com/miles170>`_
8182
- `Mischa Krüger <https://github.com/Makman2>`_
8283
- `naveenvhegde <https://github.com/naveenvhegde>`_

examples/contexttypesbot.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ def __init__(self) -> None:
5757
class CustomContext(CallbackContext[ExtBot, dict, ChatData, dict]):
5858
"""Custom class for context."""
5959

60-
def __init__(self, application: Application, chat_id: int = None, user_id: int = None):
60+
def __init__(
61+
self,
62+
application: Application,
63+
chat_id: Optional[int] = None,
64+
user_id: Optional[int] = None,
65+
):
6166
super().__init__(application=application, chat_id=chat_id, user_id=user_id)
6267
self._message_id: Optional[int] = None
6368

setup.cfg

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ disallow_untyped_defs = True
6464
disallow_incomplete_defs = True
6565
disallow_untyped_decorators = True
6666
show_error_codes = True
67-
implicit_optional = True
6867

6968
# For some files, it's easier to just disable strict-optional all together instead of
7069
# cluttering the code with `# type: ignore`s or stuff like

telegram/_bot.py

Lines changed: 448 additions & 442 deletions
Large diffs are not rendered by default.

telegram/_botcommand.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# along with this program. If not, see [http://www.gnu.org/licenses/].
1919
"""This module contains an object that represents a Telegram Bot Command."""
2020

21-
from typing import ClassVar
21+
from typing import ClassVar, Optional
2222

2323
from telegram import constants
2424
from telegram._telegramobject import TelegramObject
@@ -52,7 +52,7 @@ class BotCommand(TelegramObject):
5252

5353
__slots__ = ("description", "command")
5454

55-
def __init__(self, command: str, description: str, *, api_kwargs: JSONDict = None):
55+
def __init__(self, command: str, description: str, *, api_kwargs: Optional[JSONDict] = None):
5656
super().__init__(api_kwargs=api_kwargs)
5757
self.command: str = command
5858
self.description: str = description

telegram/_botcommandscope.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class BotCommandScope(TelegramObject):
7575
CHAT_MEMBER: ClassVar[str] = constants.BotCommandScopeType.CHAT_MEMBER
7676
""":const:`telegram.constants.BotCommandScopeType.CHAT_MEMBER`"""
7777

78-
def __init__(self, type: str, *, api_kwargs: JSONDict = None):
78+
def __init__(self, type: str, *, api_kwargs: Optional[JSONDict] = None):
7979
super().__init__(api_kwargs=api_kwargs)
8080
self.type: str = type
8181
self._id_attrs = (self.type,)
@@ -128,7 +128,7 @@ class BotCommandScopeDefault(BotCommandScope):
128128

129129
__slots__ = ()
130130

131-
def __init__(self, *, api_kwargs: JSONDict = None):
131+
def __init__(self, *, api_kwargs: Optional[JSONDict] = None):
132132
super().__init__(type=BotCommandScope.DEFAULT, api_kwargs=api_kwargs)
133133
self._freeze()
134134

@@ -144,7 +144,7 @@ class BotCommandScopeAllPrivateChats(BotCommandScope):
144144

145145
__slots__ = ()
146146

147-
def __init__(self, *, api_kwargs: JSONDict = None):
147+
def __init__(self, *, api_kwargs: Optional[JSONDict] = None):
148148
super().__init__(type=BotCommandScope.ALL_PRIVATE_CHATS, api_kwargs=api_kwargs)
149149
self._freeze()
150150

@@ -159,7 +159,7 @@ class BotCommandScopeAllGroupChats(BotCommandScope):
159159

160160
__slots__ = ()
161161

162-
def __init__(self, *, api_kwargs: JSONDict = None):
162+
def __init__(self, *, api_kwargs: Optional[JSONDict] = None):
163163
super().__init__(type=BotCommandScope.ALL_GROUP_CHATS, api_kwargs=api_kwargs)
164164
self._freeze()
165165

@@ -174,7 +174,7 @@ class BotCommandScopeAllChatAdministrators(BotCommandScope):
174174

175175
__slots__ = ()
176176

177-
def __init__(self, *, api_kwargs: JSONDict = None):
177+
def __init__(self, *, api_kwargs: Optional[JSONDict] = None):
178178
super().__init__(type=BotCommandScope.ALL_CHAT_ADMINISTRATORS, api_kwargs=api_kwargs)
179179
self._freeze()
180180

@@ -197,7 +197,7 @@ class BotCommandScopeChat(BotCommandScope):
197197

198198
__slots__ = ("chat_id",)
199199

200-
def __init__(self, chat_id: Union[str, int], *, api_kwargs: JSONDict = None):
200+
def __init__(self, chat_id: Union[str, int], *, api_kwargs: Optional[JSONDict] = None):
201201
super().__init__(type=BotCommandScope.CHAT, api_kwargs=api_kwargs)
202202
with self._unfrozen():
203203
self.chat_id: Union[str, int] = (
@@ -224,7 +224,7 @@ class BotCommandScopeChatAdministrators(BotCommandScope):
224224

225225
__slots__ = ("chat_id",)
226226

227-
def __init__(self, chat_id: Union[str, int], *, api_kwargs: JSONDict = None):
227+
def __init__(self, chat_id: Union[str, int], *, api_kwargs: Optional[JSONDict] = None):
228228
super().__init__(type=BotCommandScope.CHAT_ADMINISTRATORS, api_kwargs=api_kwargs)
229229
with self._unfrozen():
230230
self.chat_id: Union[str, int] = (
@@ -254,7 +254,9 @@ class BotCommandScopeChatMember(BotCommandScope):
254254

255255
__slots__ = ("chat_id", "user_id")
256256

257-
def __init__(self, chat_id: Union[str, int], user_id: int, *, api_kwargs: JSONDict = None):
257+
def __init__(
258+
self, chat_id: Union[str, int], user_id: int, *, api_kwargs: Optional[JSONDict] = None
259+
):
258260
super().__init__(type=BotCommandScope.CHAT_MEMBER, api_kwargs=api_kwargs)
259261
with self._unfrozen():
260262
self.chat_id: Union[str, int] = (

telegram/_botdescription.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
# You should have received a copy of the GNU Lesser Public License
1818
# along with this program. If not, see [http://www.gnu.org/licenses/].
1919
"""This module contains two objects that represent a Telegram bots (short) description."""
20+
from typing import Optional
21+
2022
from telegram._telegramobject import TelegramObject
2123
from telegram._utils.types import JSONDict
2224

@@ -39,7 +41,7 @@ class BotDescription(TelegramObject):
3941

4042
__slots__ = ("description",)
4143

42-
def __init__(self, description: str, *, api_kwargs: JSONDict = None):
44+
def __init__(self, description: str, *, api_kwargs: Optional[JSONDict] = None):
4345
super().__init__(api_kwargs=api_kwargs)
4446
self.description: str = description
4547

@@ -66,7 +68,7 @@ class BotShortDescription(TelegramObject):
6668

6769
__slots__ = ("short_description",)
6870

69-
def __init__(self, short_description: str, *, api_kwargs: JSONDict = None):
71+
def __init__(self, short_description: str, *, api_kwargs: Optional[JSONDict] = None):
7072
super().__init__(api_kwargs=api_kwargs)
7173
self.short_description: str = short_description
7274

telegram/_botname.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# You should have received a copy of the GNU Lesser Public License
1818
# along with this program. If not, see [http://www.gnu.org/licenses/].
1919
"""This module contains an object that represent a Telegram bots name."""
20-
from typing import ClassVar
20+
from typing import ClassVar, Optional
2121

2222
from telegram import constants
2323
from telegram._telegramobject import TelegramObject
@@ -42,7 +42,7 @@ class BotName(TelegramObject):
4242

4343
__slots__ = ("name",)
4444

45-
def __init__(self, name: str, *, api_kwargs: JSONDict = None):
45+
def __init__(self, name: str, *, api_kwargs: Optional[JSONDict] = None):
4646
super().__init__(api_kwargs=api_kwargs)
4747
self.name: str = name
4848

telegram/_callbackquery.py

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,12 @@ def __init__(
118118
id: str,
119119
from_user: User,
120120
chat_instance: str,
121-
message: Message = None,
122-
data: str = None,
123-
inline_message_id: str = None,
124-
game_short_name: str = None,
121+
message: Optional[Message] = None,
122+
data: Optional[str] = None,
123+
inline_message_id: Optional[str] = None,
124+
game_short_name: Optional[str] = None,
125125
*,
126-
api_kwargs: JSONDict = None,
126+
api_kwargs: Optional[JSONDict] = None,
127127
):
128128
super().__init__(api_kwargs=api_kwargs)
129129
# Required
@@ -155,16 +155,16 @@ def de_json(cls, data: Optional[JSONDict], bot: "Bot") -> Optional["CallbackQuer
155155

156156
async def answer(
157157
self,
158-
text: str = None,
159-
show_alert: bool = None,
160-
url: str = None,
161-
cache_time: int = None,
158+
text: Optional[str] = None,
159+
show_alert: Optional[bool] = None,
160+
url: Optional[str] = None,
161+
cache_time: Optional[int] = None,
162162
*,
163163
read_timeout: ODVInput[float] = DEFAULT_NONE,
164164
write_timeout: ODVInput[float] = DEFAULT_NONE,
165165
connect_timeout: ODVInput[float] = DEFAULT_NONE,
166166
pool_timeout: ODVInput[float] = DEFAULT_NONE,
167-
api_kwargs: JSONDict = None,
167+
api_kwargs: Optional[JSONDict] = None,
168168
) -> bool:
169169
"""Shortcut for::
170170
@@ -195,14 +195,14 @@ async def edit_message_text(
195195
text: str,
196196
parse_mode: ODVInput[str] = DEFAULT_NONE,
197197
disable_web_page_preview: ODVInput[bool] = DEFAULT_NONE,
198-
reply_markup: "InlineKeyboardMarkup" = None,
199-
entities: Sequence["MessageEntity"] = None,
198+
reply_markup: Optional["InlineKeyboardMarkup"] = None,
199+
entities: Optional[Sequence["MessageEntity"]] = None,
200200
*,
201201
read_timeout: ODVInput[float] = DEFAULT_NONE,
202202
write_timeout: ODVInput[float] = DEFAULT_NONE,
203203
connect_timeout: ODVInput[float] = DEFAULT_NONE,
204204
pool_timeout: ODVInput[float] = DEFAULT_NONE,
205-
api_kwargs: JSONDict = None,
205+
api_kwargs: Optional[JSONDict] = None,
206206
) -> Union[Message, bool]:
207207
"""Shortcut for either::
208208
@@ -253,16 +253,16 @@ async def edit_message_text(
253253

254254
async def edit_message_caption(
255255
self,
256-
caption: str = None,
257-
reply_markup: "InlineKeyboardMarkup" = None,
256+
caption: Optional[str] = None,
257+
reply_markup: Optional["InlineKeyboardMarkup"] = None,
258258
parse_mode: ODVInput[str] = DEFAULT_NONE,
259-
caption_entities: Sequence["MessageEntity"] = None,
259+
caption_entities: Optional[Sequence["MessageEntity"]] = None,
260260
*,
261261
read_timeout: ODVInput[float] = DEFAULT_NONE,
262262
write_timeout: ODVInput[float] = DEFAULT_NONE,
263263
connect_timeout: ODVInput[float] = DEFAULT_NONE,
264264
pool_timeout: ODVInput[float] = DEFAULT_NONE,
265-
api_kwargs: JSONDict = None,
265+
api_kwargs: Optional[JSONDict] = None,
266266
) -> Union[Message, bool]:
267267
"""Shortcut for either::
268268
@@ -317,7 +317,7 @@ async def edit_message_reply_markup(
317317
write_timeout: ODVInput[float] = DEFAULT_NONE,
318318
connect_timeout: ODVInput[float] = DEFAULT_NONE,
319319
pool_timeout: ODVInput[float] = DEFAULT_NONE,
320-
api_kwargs: JSONDict = None,
320+
api_kwargs: Optional[JSONDict] = None,
321321
) -> Union[Message, bool]:
322322
"""Shortcut for either::
323323
@@ -362,13 +362,13 @@ async def edit_message_reply_markup(
362362
async def edit_message_media(
363363
self,
364364
media: "InputMedia",
365-
reply_markup: "InlineKeyboardMarkup" = None,
365+
reply_markup: Optional["InlineKeyboardMarkup"] = None,
366366
*,
367367
read_timeout: ODVInput[float] = DEFAULT_NONE,
368368
write_timeout: ODVInput[float] = DEFAULT_NONE,
369369
connect_timeout: ODVInput[float] = DEFAULT_NONE,
370370
pool_timeout: ODVInput[float] = DEFAULT_NONE,
371-
api_kwargs: JSONDict = None,
371+
api_kwargs: Optional[JSONDict] = None,
372372
) -> Union[Message, bool]:
373373
"""Shortcut for either::
374374
@@ -413,19 +413,19 @@ async def edit_message_media(
413413

414414
async def edit_message_live_location(
415415
self,
416-
latitude: float = None,
417-
longitude: float = None,
418-
reply_markup: "InlineKeyboardMarkup" = None,
419-
horizontal_accuracy: float = None,
420-
heading: int = None,
421-
proximity_alert_radius: int = None,
416+
latitude: Optional[float] = None,
417+
longitude: Optional[float] = None,
418+
reply_markup: Optional["InlineKeyboardMarkup"] = None,
419+
horizontal_accuracy: Optional[float] = None,
420+
heading: Optional[int] = None,
421+
proximity_alert_radius: Optional[int] = None,
422422
*,
423-
location: Location = None,
423+
location: Optional[Location] = None,
424424
read_timeout: ODVInput[float] = DEFAULT_NONE,
425425
write_timeout: ODVInput[float] = DEFAULT_NONE,
426426
connect_timeout: ODVInput[float] = DEFAULT_NONE,
427427
pool_timeout: ODVInput[float] = DEFAULT_NONE,
428-
api_kwargs: JSONDict = None,
428+
api_kwargs: Optional[JSONDict] = None,
429429
) -> Union[Message, bool]:
430430
"""Shortcut for either::
431431
@@ -481,13 +481,13 @@ async def edit_message_live_location(
481481

482482
async def stop_message_live_location(
483483
self,
484-
reply_markup: "InlineKeyboardMarkup" = None,
484+
reply_markup: Optional["InlineKeyboardMarkup"] = None,
485485
*,
486486
read_timeout: ODVInput[float] = DEFAULT_NONE,
487487
write_timeout: ODVInput[float] = DEFAULT_NONE,
488488
connect_timeout: ODVInput[float] = DEFAULT_NONE,
489489
pool_timeout: ODVInput[float] = DEFAULT_NONE,
490-
api_kwargs: JSONDict = None,
490+
api_kwargs: Optional[JSONDict] = None,
491491
) -> Union[Message, bool]:
492492
"""Shortcut for either::
493493
@@ -533,14 +533,14 @@ async def set_game_score(
533533
self,
534534
user_id: Union[int, str],
535535
score: int,
536-
force: bool = None,
537-
disable_edit_message: bool = None,
536+
force: Optional[bool] = None,
537+
disable_edit_message: Optional[bool] = None,
538538
*,
539539
read_timeout: ODVInput[float] = DEFAULT_NONE,
540540
write_timeout: ODVInput[float] = DEFAULT_NONE,
541541
connect_timeout: ODVInput[float] = DEFAULT_NONE,
542542
pool_timeout: ODVInput[float] = DEFAULT_NONE,
543-
api_kwargs: JSONDict = None,
543+
api_kwargs: Optional[JSONDict] = None,
544544
) -> Union[Message, bool]:
545545
"""Shortcut for either::
546546
@@ -595,7 +595,7 @@ async def get_game_high_scores(
595595
write_timeout: ODVInput[float] = DEFAULT_NONE,
596596
connect_timeout: ODVInput[float] = DEFAULT_NONE,
597597
pool_timeout: ODVInput[float] = DEFAULT_NONE,
598-
api_kwargs: JSONDict = None,
598+
api_kwargs: Optional[JSONDict] = None,
599599
) -> Tuple["GameHighScore", ...]:
600600
"""Shortcut for either::
601601
@@ -643,7 +643,7 @@ async def delete_message(
643643
write_timeout: ODVInput[float] = DEFAULT_NONE,
644644
connect_timeout: ODVInput[float] = DEFAULT_NONE,
645645
pool_timeout: ODVInput[float] = DEFAULT_NONE,
646-
api_kwargs: JSONDict = None,
646+
api_kwargs: Optional[JSONDict] = None,
647647
) -> bool:
648648
"""Shortcut for::
649649
@@ -671,7 +671,7 @@ async def pin_message(
671671
write_timeout: ODVInput[float] = DEFAULT_NONE,
672672
connect_timeout: ODVInput[float] = DEFAULT_NONE,
673673
pool_timeout: ODVInput[float] = DEFAULT_NONE,
674-
api_kwargs: JSONDict = None,
674+
api_kwargs: Optional[JSONDict] = None,
675675
) -> bool:
676676
"""Shortcut for::
677677
< 10000 /td>
@@ -699,7 +699,7 @@ async def unpin_message(
699699
write_timeout: ODVInput[float] = DEFAULT_NONE,
700700
connect_timeout: ODVInput[float] = DEFAULT_NONE,
701701
pool_timeout: ODVInput[float] = DEFAULT_NONE,
702-
api_kwargs: JSONDict = None,
702+
api_kwargs: Optional[JSONDict] = None,
703703
) -> bool:
704704
"""Shortcut for::
705705
@@ -722,21 +722,21 @@ async def unpin_message(
722722
async def copy_message(
723723
self,
724724
chat_id: Union[int, str],
725-
caption: str = None,
725+
caption: Optional[str] = None,
726726
parse_mode: ODVInput[str] = DEFAULT_NONE,
727-
caption_entities: Sequence["MessageEntity"] = None,
727+
caption_entities: Optional[Sequence["MessageEntity"]] = None,
728728
disable_notification: DVInput[bool] = DEFAULT_NONE,
729-
reply_to_message_id: int = None,
729+
reply_to_message_id: Optional[int] = None,
730730
allow_sending_without_reply: DVInput[bool] = DEFAULT_NONE,
731-
reply_markup: ReplyMarkup = None,
731+
reply_markup: Optional[ReplyMarkup] = None,
732732
protect_content: ODVInput[bool] = DEFAULT_NONE,
733-
message_thread_id: int = None,
733+
message_thread_id: Optional[int] = None,
734734
*,
735735
read_timeout: ODVInput[float] = DEFAULT_NONE,
736736
write_timeout: ODVInput[float] = DEFAULT_NONE,
737737
connect_timeout: ODVInput[float] = DEFAULT_NONE,
738738
pool_timeout: ODVInput[float] = DEFAULT_NONE,
739-
api_kwargs: JSONDict = None,
739+
api_kwargs: Optional[JSONDict] = None,
740740
) -> "MessageId":
741741
"""Shortcut for::
742742

0 commit comments

Comments
 (0)
10C8
0