From 31a73ff14faec4b4158e204177bc1363a5060393 Mon Sep 17 00:00:00 2001 From: Badiboy Date: Sat, 17 Aug 2024 12:45:27 +0300 Subject: [PATCH 1/3] Fix some type hints and warnings --- telebot/types.py | 66 +++++++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 26 deletions(-) diff --git a/telebot/types.py b/telebot/types.py index 21480e31d..28ea9eca4 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -1525,7 +1525,7 @@ def __init__(self, message_id, from_user, date, chat, content_type, options, jso self.json = json_string @property - def html_text(self) -> str: + def html_text(self) -> Optional[str]: """ Returns html-rendered text. """ @@ -1534,7 +1534,7 @@ def html_text(self) -> str: return apply_html_entities(self.text, self.entities, getattr(self, "custom_subs", None)) @property - def html_caption(self) -> str: + def html_caption(self) -> Optional[str]: """ Returns html-rendered caption. """ @@ -2734,6 +2734,7 @@ def __init__(self, text: str, request_contact: Optional[bool]=None, if request_user is not None: logger.warning('The parameter "request_user" is deprecated, use "request_users" instead') if self.request_users is None: + # noinspection PyTypeChecker self.request_users = request_user @@ -4268,6 +4269,7 @@ def __init__(self, result_id, from_user, query, location=None, inline_message_id self.query: str = query +# noinspection PyShadowingBuiltins class InlineQueryResultBase(ABC, Dictionaryable, JsonSerializable): """ This object represents one result of an inline query. Telegram clients currently support results of the following 20 types: @@ -5407,8 +5409,8 @@ class InlineQueryResultCachedBase(ABC, JsonSerializable): Base class of all InlineQueryResultCached* classes. """ def __init__(self): - self.type: str = None - self.id: str = None + self.type: str = "" + self.id: str = "" self.title: Optional[str] = None self.description: Optional[str] = None self.caption: Optional[str] = None @@ -5416,6 +5418,7 @@ def __init__(self): self.input_message_content: Optional[InputMessageContent] = None self.parse_mode: Optional[str] = None self.caption_entities: Optional[List[MessageEntity]] = None + # noinspection PyTypeChecker self.payload_dic: Dict[str] = {} self.show_caption_above_media: Optional[bool] = None @@ -7104,6 +7107,7 @@ def to_dict(self): return json_dict +# noinspection PyShadowingBuiltins class Poll(JsonDeserializable): """ This object contains information about a poll. @@ -7586,7 +7590,7 @@ def to_dict(self): raise NotImplementedError -# noinspection PyUnusedLocal +# noinspection PyUnusedLocal,PyShadowingBuiltins class MenuButtonCommands(MenuButton): """ Represents a menu button, which opens the bot's list of commands. @@ -7610,7 +7614,7 @@ def to_json(self): return json.dumps(self.to_dict()) -# noinspection PyUnusedLocal +# noinspection PyUnusedLocal,PyShadowingBuiltins class MenuButtonWebApp(MenuButton): """ Represents a menu button, which launches a Web App. @@ -7645,7 +7649,7 @@ def to_json(self): return json.dumps(self.to_dict()) -# noinspection PyUnusedLocal +# noinspection PyUnusedLocal,PyShadowingBuiltins class MenuButtonDefault(MenuButton): """ Describes that no specific value for the menu button was set. @@ -8202,7 +8206,7 @@ def to_dict(self) -> dict: def to_json(self) -> str: return json.dumps(self.to_dict()) - def convert_input_sticker(self) -> Tuple[dict, Optional[dict]]: + def convert_input_sticker(self) -> Tuple[str, Optional[dict]]: if service_utils.is_string(self.sticker): return self.to_json(), None @@ -8343,6 +8347,7 @@ def to_json(self) -> str: return json.dumps(self.to_dict()) +# noinspection PyShadowingBuiltins class Story(JsonDeserializable): """ This object represents a story. @@ -9250,12 +9255,12 @@ def __init__(self, request_id: int, users: List[SharedUser], **kwargs): self.users: List[SharedUser] = users @property - def user_id(self) -> int: + def user_id(self) -> None: logger.warning('The parameter "user_id" is deprecated, use "user_ids" instead') return None @property - def user_ids(self) -> List[int]: + def user_ids(self) -> List[SharedUser]: logger.warning('The parameter "user_ids" is deprecated, use "users" instead') return self.users @@ -10217,8 +10222,9 @@ def de_json(cls, json_string): elif obj["type"] == "failed": return RevenueWithdrawalStateFailed.de_json(obj) return None - + +# noinspection PyShadowingBuiltins class RevenueWithdrawalStatePending(RevenueWithdrawalState): """ The withdrawal is in progress. @@ -10232,7 +10238,6 @@ class RevenueWithdrawalStatePending(RevenueWithdrawalState): :rtype: :class:`RevenueWithdrawalStatePending` """ - # noinspection PyPackageRequirements def __init__(self, type, **kwargs): self.type: str = type @@ -10243,6 +10248,7 @@ def de_json(cls, json_string): return cls(**obj) +# noinspection PyShadowingBuiltins class RevenueWithdrawalStateSucceeded(RevenueWithdrawalState): """ The withdrawal succeeded. @@ -10262,7 +10268,6 @@ class RevenueWithdrawalStateSucceeded(RevenueWithdrawalState): :rtype: :class:`RevenueWithdrawalStateSucceeded` """ - # noinspection PyPackageRequirements def __init__(self, type, date, url, **kwargs): self.type: str = type self.date: int = date @@ -10273,9 +10278,9 @@ def de_json(cls, json_string): if json_string is None: return None obj = cls.check_json(json_string) return cls(**obj) - - + +# noinspection PyShadowingBuiltins class RevenueWithdrawalStateFailed(RevenueWithdrawalState): """ The withdrawal failed and the transaction was refunded. @@ -10289,7 +10294,6 @@ class RevenueWithdrawalStateFailed(RevenueWithdrawalState): :rtype: :class:`RevenueWithdrawalStateFailed` """ - # noinspection PyPackageRequirements def __init__(self, type, **kwargs): self.type: str = type @@ -10329,7 +10333,9 @@ def de_json(cls, json_string): return TransactionPartnerTelegramAds.de_json(obj) elif obj["type"] == "other": return TransactionPartnerOther.de_json(obj) - + + +# noinspection PyShadowingBuiltins class TransactionPartnerFragment(TransactionPartner): """ Describes a withdrawal transaction with Fragment. @@ -10347,7 +10353,6 @@ class TransactionPartnerFragment(TransactionPartner): """ - # noinspection PyPackageRequirements def __init__(self, type, withdrawal_state=None, **kwargs): self.type: str = type self.withdrawal_state: Optional[RevenueWithdrawalState] = withdrawal_state @@ -10359,9 +10364,9 @@ def de_json(cls, json_string): if 'withdrawal_state' in obj: obj['withdrawal_state'] = RevenueWithdrawalState.de_json(obj['withdrawal_state']) return cls(**obj) - +# noinspection PyShadowingBuiltins class TransactionPartnerUser(TransactionPartner): """ Describes a transaction with a user. @@ -10392,7 +10397,9 @@ def de_json(cls, json_string): obj = cls.check_json(json_string) obj['user'] = User.de_json(obj['user']) return cls(**obj) - + + +# noinspection PyShadowingBuiltins class TransactionPartnerTelegramAds(TransactionPartner): """ Describes a transaction with Telegram Ads. @@ -10413,8 +10420,10 @@ def __init__(self, type, **kwargs): def de_json(cls, json_string): if json_string is None: return None obj = cls.check_json(json_string) - - + return obj + + +# noinspection PyShadowingBuiltins class TransactionPartnerOther(TransactionPartner): """ Describes a transaction with an unknown source or recipient. @@ -10436,9 +10445,9 @@ def de_json(cls, json_string): if json_string is None: return None obj = cls.check_json(json_string) return cls(**obj) - +# noinspection PyShadowingBuiltins class StarTransaction(JsonDeserializable): """ Describes a Telegram Star transaction. @@ -10531,7 +10540,9 @@ def de_json(cls, json_string): return PaidMediaPhoto.de_json(obj) elif obj["type"] == "video": return PaidMediaVideo.de_json(obj) - + + +# noinspection PyShadowingBuiltins class PaidMediaPreview(PaidMedia): """ The paid media isn't available before the payment. @@ -10565,8 +10576,9 @@ def de_json(cls, json_string): if json_string is None: return None obj = cls.check_json(json_string) return cls(**obj) - + +# noinspection PyShadowingBuiltins class PaidMediaPhoto(PaidMedia): """ The paid media is a photo. @@ -10595,8 +10607,9 @@ def de_json(cls, json_string): obj['photo'] = [PhotoSize.de_json(photo) for photo in obj['photo']] return cls(**obj) - + +# noinspection PyShadowingBuiltins class PaidMediaVideo(PaidMedia): """ The paid media is a video. @@ -10653,6 +10666,7 @@ def __init__(self, star_count, paid_media, **kwargs): self.paid_media: List[PaidMedia] = paid_media +# noinspection PyShadowingBuiltins class InputPaidMedia(JsonSerializable): """ This object describes the paid media to be sent. Currently, it can be one of From 2d41d2a516ed5cf513d5ab7ae30a67f32c9a55d7 Mon Sep 17 00:00:00 2001 From: Badiboy Date: Sat, 17 Aug 2024 13:23:07 +0300 Subject: [PATCH 2/3] any_text / any_entities to Message --- telebot/types.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/telebot/types.py b/telebot/types.py index 28ea9eca4..e972696b1 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -1618,6 +1618,14 @@ def user_shared(self): logger.warning('The parameter "user_shared" is deprecated, use "users_shared" instead') return self.users_shared + @property + def any_text(self): + return self.caption if (self.caption is not None) else self.text + + @property + def any_entities(self): + return self.caption_entities if (self.caption_entities is not None) else self.entities + # noinspection PyShadowingBuiltins class MessageEntity(Dictionaryable, JsonSerializable, JsonDeserializable): From 8dcfdc5e6bfb5d5633c8c55e1ac3f84030526140 Mon Sep 17 00:00:00 2001 From: Badiboy Date: Sat, 17 Aug 2024 19:08:25 +0300 Subject: [PATCH 3/3] any_xxx typehints added --- telebot/types.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/telebot/types.py b/telebot/types.py index e972696b1..badd99a75 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -1619,11 +1619,11 @@ def user_shared(self): return self.users_shared @property - def any_text(self): + def any_text(self) -> Optional[str]: return self.caption if (self.caption is not None) else self.text @property - def any_entities(self): + def any_entities(self) -> Optional[List[MessageEntity]]: return self.caption_entities if (self.caption_entities is not None) else self.entities