Skip to content

Commit

Permalink
Use Explicit Optionals (#3692)
Browse files Browse the repository at this point in the history
Co-authored-by: Dmitry Kolomatskiy <58207913+lemontree210@users.noreply.github.com>
  • Loading branch information
MiguelX413 and lemontree210 committed May 18, 2023
1 parent e432c29 commit 99fd443
Show file tree
Hide file tree
Showing 146 changed files with 2,798 additions and 2,637 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Expand Up @@ -77,6 +77,7 @@ The following wonderful people contributed directly or indirectly to this projec
- `Matheus Lemos <https://github.com/mlemosf>`_
- `Michael Dix <https://github.com/Eisberge>`_
- `Michael Elovskikh <https://github.com/wronglink>`_
- `Miguel C. R. <https://github.com/MiguelX413>`_
- `miles <https://github.com/miles170>`_
- `Mischa Krüger <https://github.com/Makman2>`_
- `naveenvhegde <https://github.com/naveenvhegde>`_
Expand Down
7 changes: 6 additions & 1 deletion examples/contexttypesbot.py
Expand Up @@ -57,7 +57,12 @@ def __init__(self) -> None:
class CustomContext(CallbackContext[ExtBot, dict, ChatData, dict]):
"""Custom class for context."""

def __init__(self, application: Application, chat_id: int = None, user_id: int = None):
def __init__(
self,
application: Application,
chat_id: Optional[int] = None,
user_id: Optional[int] = None,
):
super().__init__(application=application, chat_id=chat_id, user_id=user_id)
self._message_id: Optional[int] = None

Expand Down
1 change: 0 additions & 1 deletion setup.cfg
Expand Up @@ -64,7 +64,6 @@ disallow_untyped_defs = True
disallow_incomplete_defs = True
disallow_untyped_decorators = True
show_error_codes = True
implicit_optional = True

# For some files, it's easier to just disable strict-optional all together instead of
# cluttering the code with `# type: ignore`s or stuff like
Expand Down
890 changes: 448 additions & 442 deletions telegram/_bot.py

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions telegram/_botcommand.py
Expand Up @@ -18,7 +18,7 @@
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a Telegram Bot Command."""

from typing import ClassVar
from typing import ClassVar, Optional

from telegram import constants
from telegram._telegramobject import TelegramObject
Expand Down Expand Up @@ -52,7 +52,7 @@ class BotCommand(TelegramObject):

__slots__ = ("description", "command")

def __init__(self, command: str, description: str, *, api_kwargs: JSONDict = None):
def __init__(self, command: str, description: str, *, api_kwargs: Optional[JSONDict] = None):
super().__init__(api_kwargs=api_kwargs)
self.command: str = command
self.description: str = description
Expand Down
18 changes: 10 additions & 8 deletions telegram/_botcommandscope.py
Expand Up @@ -75,7 +75,7 @@ class BotCommandScope(TelegramObject):
CHAT_MEMBER: ClassVar[str] = constants.BotCommandScopeType.CHAT_MEMBER
""":const:`telegram.constants.BotCommandScopeType.CHAT_MEMBER`"""

def __init__(self, type: str, *, api_kwargs: JSONDict = None):
def __init__(self, type: str, *, api_kwargs: Optional[JSONDict] = None):
super().__init__(api_kwargs=api_kwargs)
self.type: str = type
self._id_attrs = (self.type,)
Expand Down Expand Up @@ -128,7 +128,7 @@ class BotCommandScopeDefault(BotCommandScope):

__slots__ = ()

def __init__(self, *, api_kwargs: JSONDict = None):
def __init__(self, *, api_kwargs: Optional[JSONDict] = None):
super().__init__(type=BotCommandScope.DEFAULT, api_kwargs=api_kwargs)
self._freeze()

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

__slots__ = ()

def __init__(self, *, api_kwargs: JSONDict = None):
def __init__(self, *, api_kwargs: Optional[JSONDict] = None):
super().__init__(type=BotCommandScope.ALL_PRIVATE_CHATS, api_kwargs=api_kwargs)
self._freeze()

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

__slots__ = ()

def __init__(self, *, api_kwargs: JSONDict = None):
def __init__(self, *, api_kwargs: Optional[JSONDict] = None):
super().__init__(type=BotCommandScope.ALL_GROUP_CHATS, api_kwargs=api_kwargs)
self._freeze()

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

__slots__ = ()

def __init__(self, *, api_kwargs: JSONDict = None):
def __init__(self, *, api_kwargs: Optional[JSONDict] = None):
super().__init__(type=BotCommandScope.ALL_CHAT_ADMINISTRATORS, api_kwargs=api_kwargs)
self._freeze()

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

__slots__ = ("chat_id",)

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

__slots__ = ("chat_id",)

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

__slots__ = ("chat_id", "user_id")

def __init__(self, chat_id: Union[str, int], user_id: int, *, api_kwargs: JSONDict = None):
def __init__(
self, chat_id: Union[str, int], user_id: int, *, api_kwargs: Optional[JSONDict] = None
):
super().__init__(type=BotCommandScope.CHAT_MEMBER, api_kwargs=api_kwargs)
with self._unfrozen():
self.chat_id: Union[str, int] = (
Expand Down
6 changes: 4 additions & 2 deletions telegram/_botdescription.py
Expand Up @@ -17,6 +17,8 @@
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains two objects that represent a Telegram bots (short) description."""
from typing import Optional

from telegram._telegramobject import TelegramObject
from telegram._utils.types import JSONDict

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

__slots__ = ("description",)

def __init__(self, description: str, *, api_kwargs: JSONDict = None):
def __init__(self, description: str, *, api_kwargs: Optional[JSONDict] = None):
super().__init__(api_kwargs=api_kwargs)
self.description: str = description

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

__slots__ = ("short_description",)

def __init__(self, short_description: str, *, api_kwargs: JSONDict = None):
def __init__(self, short_description: str, *, api_kwargs: Optional[JSONDict] = None):
super().__init__(api_kwargs=api_kwargs)
self.short_description: str = short_description

Expand Down
4 changes: 2 additions & 2 deletions telegram/_botname.py
Expand Up @@ -17,7 +17,7 @@
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represent a Telegram bots name."""
from typing import ClassVar
from typing import ClassVar, Optional

from telegram import constants
from telegram._telegramobject import TelegramObject
Expand All @@ -42,7 +42,7 @@ class BotName(TelegramObject):

__slots__ = ("name",)

def __init__(self, name: str, *, api_kwargs: JSONDict = None):
def __init__(self, name: str, *, api_kwargs: Optional[JSONDict] = None):
super().__init__(api_kwargs=api_kwargs)
self.name: str = name

Expand Down
86 changes: 43 additions & 43 deletions telegram/_callbackquery.py
Expand Up @@ -118,12 +118,12 @@ def __init__(
id: str,
from_user: User,
chat_instance: str,
message: Message = None,
data: str = None,
inline_message_id: str = None,
game_short_name: str = None,
message: Optional[Message] = None,
data: Optional[str] = None,
inline_message_id: Optional[str] = None,
game_short_name: Optional[str] = None,
*,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(api_kwargs=api_kwargs)
# Required
Expand Down Expand Up @@ -155,16 +155,16 @@ def de_json(cls, data: Optional[JSONDict], bot: "Bot") -> Optional["CallbackQuer

async def answer(
self,
text: str = None,
show_alert: bool = None,
url: str = None,
cache_time: int = None,
text: Optional[str] = None,
show_alert: Optional[bool] = None,
url: Optional[str] = None,
cache_time: Optional[int] = None,
*,
read_timeout: ODVInput[float] = DEFAULT_NONE,
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
) -> bool:
"""Shortcut for::
Expand Down Expand Up @@ -195,14 +195,14 @@ async def edit_message_text(
text: str,
parse_mode: ODVInput[str] = DEFAULT_NONE,
disable_web_page_preview: ODVInput[bool] = DEFAULT_NONE,
reply_markup: "InlineKeyboardMarkup" = None,
entities: Sequence["MessageEntity"] = None,
reply_markup: Optional["InlineKeyboardMarkup"] = None,
entities: Optional[Sequence["MessageEntity"]] = None,
*,
read_timeout: ODVInput[float] = DEFAULT_NONE,
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
) -> Union[Message, bool]:
"""Shortcut for either::
Expand Down Expand Up @@ -253,16 +253,16 @@ async def edit_message_text(

async def edit_message_caption(
self,
caption: str = None,
reply_markup: "InlineKeyboardMarkup" = None,
caption: Optional[str] = None,
reply_markup: Optional["InlineKeyboardMarkup"] = None,
parse_mode: ODVInput[str] = DEFAULT_NONE,
caption_entities: Sequence["MessageEntity"] = None,
caption_entities: Optional[Sequence["MessageEntity"]] = None,
*,
read_timeout: ODVInput[float] = DEFAULT_NONE,
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
) -> Union[Message, bool]:
"""Shortcut for either::
Expand Down Expand Up @@ -317,7 +317,7 @@ async def edit_message_reply_markup(
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
) -> Union[Message, bool]:
"""Shortcut for either::
Expand Down Expand Up @@ -362,13 +362,13 @@ async def edit_message_reply_markup(
async def edit_message_media(
self,
media: "InputMedia",
reply_markup: "InlineKeyboardMarkup" = None,
reply_markup: Optional["InlineKeyboardMarkup"] = None,
*,
read_timeout: ODVInput[float] = DEFAULT_NONE,
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
) -> Union[Message, bool]:
"""Shortcut for either::
Expand Down Expand Up @@ -413,19 +413,19 @@ async def edit_message_media(

async def edit_message_live_location(
self,
latitude: float = None,
longitude: float = None,
reply_markup: "InlineKeyboardMarkup" = None,
horizontal_accuracy: float = None,
heading: int = None,
proximity_alert_radius: int = None,
latitude: Optional[float] = None,
longitude: Optional[float] = None,
reply_markup: Optional["InlineKeyboardMarkup"] = None,
horizontal_accuracy: Optional[float] = None,
heading: Optional[int] = None,
proximity_alert_radius: Optional[int] = None,
*,
location: Location = None,
location: Optional[Location] = None,
read_timeout: ODVInput[float] = DEFAULT_NONE,
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
) -> Union[Message, bool]:
"""Shortcut for either::
Expand Down Expand Up @@ -481,13 +481,13 @@ async def edit_message_live_location(

async def stop_message_live_location(
self,
reply_markup: "InlineKeyboardMarkup" = None,
reply_markup: Optional["InlineKeyboardMarkup"] = None,
*,
read_timeout: ODVInput[float] = DEFAULT_NONE,
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
) -> Union[Message, bool]:
"""Shortcut for either::
Expand Down Expand Up @@ -533,14 +533,14 @@ async def set_game_score(
self,
user_id: Union[int, str],
score: int,
force: bool = None,
disable_edit_message: bool = None,
force: Optional[bool] = None,
disable_edit_message: Optional[bool] = None,
*,
read_timeout: ODVInput[float] = DEFAULT_NONE,
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
) -> Union[Message, bool]:
"""Shortcut for either::
Expand Down Expand Up @@ -595,7 +595,7 @@ async def get_game_high_scores(
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
) -> Tuple["GameHighScore", ...]:
"""Shortcut for either::
Expand Down Expand Up @@ -643,7 +643,7 @@ async def delete_message(
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
) -> bool:
"""Shortcut for::
Expand Down Expand Up @@ -671,7 +671,7 @@ async def pin_message(
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
) -> bool:
"""Shortcut for::
Expand Down Expand Up @@ -699,7 +699,7 @@ async def unpin_message(
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
) -> bool:
"""Shortcut for::
Expand All @@ -722,21 +722,21 @@ async def unpin_message(
async def copy_message(
self,
chat_id: Union[int, str],
caption: str = None,
caption: Optional[str] = None,
parse_mode: ODVInput[str] = DEFAULT_NONE,
caption_entities: Sequence["MessageEntity"] = None,
caption_entities: Optional[Sequence["MessageEntity"]] = None,
disable_notification: DVInput[bool] = DEFAULT_NONE,
reply_to_message_id: int = None,
reply_to_message_id: Optional[int] = None,
allow_sending_without_reply: DVInput[bool] = DEFAULT_NONE,
reply_markup: ReplyMarkup = None,
reply_markup: Optional[ReplyMarkup] = None,
protect_content: ODVInput[bool] = DEFAULT_NONE,
message_thread_id: int = None,
message_thread_id: Optional[int] = None,
*,
read_timeout: ODVInput[float] = DEFAULT_NONE,
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: JSONDict = None,
api_kwargs: Optional[JSONDict] = None,
) -> "MessageId":
"""Shortcut for::
Expand Down

0 comments on commit 99fd443

Please sign in to comment.