Skip to content

Commit

Permalink
Remove Functionality Deprecated by Bot API 7.2 (#4245)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bibo-Joshi committed May 10, 2024
1 parent c34f481 commit ee6e82d
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 165 deletions.
23 changes: 3 additions & 20 deletions telegram/_bot.py
Expand Up @@ -6312,7 +6312,6 @@ async def create_new_sticker_set(
name: str,
title: str,
stickers: Sequence["InputSticker"],
sticker_format: Optional[str] = None,
sticker_type: Optional[str] = None,
needs_repainting: Optional[bool] = None,
*,
Expand All @@ -6339,6 +6338,9 @@ async def create_new_sticker_set(
Removed the deprecated parameters mentioned above and adjusted the order of the
parameters.
.. versionremoved:: NEXT.VERSION
Removed the deprecated parameter ``sticker_format``.
Args:
user_id (:obj:`int`): User identifier of created sticker set owner.
name (:obj:`str`): Short name of sticker set, to be used in t.me/addstickers/ URLs
Expand All @@ -6358,16 +6360,6 @@ async def create_new_sticker_set(
.. versionadded:: 20.2
sticker_format (:obj:`str`): Format of stickers in the set, must be one of
:attr:`~telegram.constants.StickerFormat.STATIC`,
:attr:`~telegram.constants.StickerFormat.ANIMATED` or
:attr:`~telegram.constants.StickerFormat.VIDEO`.
.. versionadded:: 20.2
.. deprecated:: 21.1
Use :paramref:`telegram.InputSticker.format` instead.
sticker_type (:obj:`str`, optional): Type of stickers in the set, pass
:attr:`telegram.Sticker.REGULAR` or :attr:`telegram.Sticker.MASK`, or
:attr:`telegram.Sticker.CUSTOM_EMOJI`. By default, a regular sticker set is created
Expand All @@ -6387,20 +6379,11 @@ async def create_new_sticker_set(
Raises:
:class:`telegram.error.TelegramError`
"""
if sticker_format is not None:
warn(
"The parameter `sticker_format` is deprecated. Use the parameter"
" `InputSticker.format` in the parameter `stickers` instead.",
stacklevel=2,
category=PTBDeprecationWarning,
)

data: JSONDict = {
"user_id": user_id,
"name": name,
"title": title,
"stickers": stickers,
"sticker_format": sticker_format,
"sticker_type": sticker_type,
"needs_repainting": needs_repainting,
}
Expand Down
42 changes: 4 additions & 38 deletions telegram/_files/sticker.py
Expand Up @@ -27,8 +27,6 @@
from telegram._utils import enum
from telegram._utils.argumentparsing import parse_sequence_arg
from telegram._utils.types import JSONDict
from telegram._utils.warnings import warn
from telegram.warnings import PTBDeprecationWarning

if TYPE_CHECKING:
from telegram import Bot
Expand Down Expand Up @@ -237,20 +235,12 @@ class StickerSet(TelegramObject):
.. versionchanged:: 20.5
|removed_thumb_note|
.. versionremoved:: NEXT.VERSION
Removed the deprecated arguments and attributes ``is_animated`` and ``is_video``.
Args:
name (:obj:`str`): Sticker set name.
title (:obj:`str`): Sticker set title.
is_animated (:obj:`bool`): :obj:`True`, if the sticker set contains animated stickers.
.. deprecated:: 21.1
Bot API 7.2 deprecated this field. This parameter will be removed in a future
version of the library.
is_video (:obj:`bool`): :obj:`True`, if the sticker set contains video stickers.
.. versionadded:: 13.11
.. deprecated:: 21.1
Bot API 7.2 deprecated this field. This parameter will be removed in a future
version of the library.
stickers (Sequence[:class:`telegram.Sticker`]): List of all set stickers.
.. versionchanged:: 20.0
Expand All @@ -269,17 +259,6 @@ class StickerSet(TelegramObject):
Attributes:
name (:obj:`str`): Sticker set name.
title (:obj:`str`): Sticker set title.
is_animated (:obj:`bool`): :obj:`True`, if the sticker set contains animated stickers.
.. deprecated:: 21.1
Bot API 7.2 deprecated this field. This parameter will be removed in a future
version of the library.
is_video (:obj:`bool`): :obj:`True`, if the sticker set contains video stickers.
.. versionadded:: 13.11
.. deprecated:: 21.1
Bot API 7.2 deprecated this field. This parameter will be removed in a future
version of the library.
stickers (Tuple[:class:`telegram.Sticker`]): List of all set stickers.
.. versionchanged:: 20.0
Expand All @@ -297,8 +276,6 @@ class StickerSet(TelegramObject):
"""

__slots__ = (
"is_animated",
"is_video",
"name",
"sticker_type",
"stickers",
Expand All @@ -312,8 +289,6 @@ def __init__(
title: str,
stickers: Sequence[Sticker],
sticker_type: str,
is_animated: Optional[bool] = None,
is_video: Optional[bool] = None,
thumbnail: Optional[PhotoSize] = None,
*,
api_kwargs: Optional[JSONDict] = None,
Expand All @@ -325,15 +300,6 @@ def __init__(
self.sticker_type: str = sticker_type
# Optional
self.thumbnail: Optional[PhotoSize] = thumbnail
if is_animated is not None or is_video is not None:
warn(
"The parameters `is_animated` and `is_video` are deprecated and will be removed "
"in a future version.",
PTBDeprecationWarning,
stacklevel=2,
)
self.is_animated: Optional[bool] = is_animated
self.is_video: Optional[bool] = is_video
self._id_attrs = (self.name,)

self._freeze()
Expand All @@ -350,7 +316,7 @@ def de_json(cls, data: Optional[JSONDict], bot: "Bot") -> Optional["StickerSet"]
api_kwargs = {}
# These are deprecated fields that TG still returns for backwards compatibility
# Let's filter them out to speed up the de-json process
for deprecated_field in ("contains_masks", "thumb"):
for deprecated_field in ("contains_masks", "thumb", "is_animated", "is_video"):
if deprecated_field in data:
api_kwargs[deprecated_field] = data.pop(deprecated_field)

Expand Down
68 changes: 8 additions & 60 deletions telegram/_shared.py
Expand Up @@ -23,12 +23,6 @@
from telegram._telegramobject import TelegramObject
from telegram._utils.argumentparsing import parse_sequence_arg
from telegram._utils.types import JSONDict
from telegram._utils.warnings import warn
from telegram._utils.warnings_transition import (
build_deprecation_warning_message,
warn_about_deprecated_attr_in_property,
)
from telegram.warnings import PTBDeprecationWarning

if TYPE_CHECKING:
from telegram._bot import Bot
Expand All @@ -44,11 +38,14 @@ class UsersShared(TelegramObject):
.. versionadded:: 20.8
Bot API 7.0 replaces ``UserShared`` with this class. The only difference is that now
the :attr:`user_ids` is a sequence instead of a single integer.
the ``user_ids`` is a sequence instead of a single integer.
.. versionchanged:: 21.1
The argument :attr:`users` is now considered for the equality comparison instead of
:attr:`user_ids`.
``user_ids``.
.. versionremoved:: NEXT.VERSION
Removed the deprecated argument and attribute ``user_ids``.
Args:
request_id (:obj:`int`): Identifier of the request.
Expand All @@ -57,18 +54,8 @@ class UsersShared(TelegramObject):
.. versionadded:: 21.1
.. deprecated:: 21.1
In future versions, this argument will become keyword only.
user_ids (Sequence[:obj:`int`], optional): Identifiers of the shared users. These numbers
may have more than 32 significant bits and some programming languages may have
difficulty/silent defects in interpreting them. But they have at most 52 significant
bits, so 64-bit integers or double-precision float types are safe for storing these
identifiers. The bot may not have access to the users and could be unable to use
these identifiers, unless the users are already known to the bot by some other means.
.. deprecated:: 21.1
Bot API 7.2 introduced by :paramref:`users`, replacing this argument. Hence, this
argument is now optional and will be removed in future versions.
.. versionchanged:: NEXT.VERSION
This argument is now required.
Attributes:
request_id (:obj:`int`): Identifier of the request.
Expand All @@ -83,31 +70,14 @@ class UsersShared(TelegramObject):
def __init__(
self,
request_id: int,
user_ids: Optional[Sequence[int]] = None,
users: Optional[Sequence["SharedUser"]] = None,
users: Sequence["SharedUser"],
*,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(api_kwargs=api_kwargs)
self.request_id: int = request_id

if users is None:
raise TypeError("`users` is a required argument since Bot API 7.2")

self.users: Tuple[SharedUser, ...] = parse_sequence_arg(users)

if user_ids is not None:
warn(
build_deprecation_warning_message(
deprecated_name="user_ids",
new_name="users",
object_type="parameter",
bot_api_version="7.2",
),
PTBDeprecationWarning,
stacklevel=2,
)

self._id_attrs = (self.request_id, self.users)

self._freeze()
Expand All @@ -130,28 +100,6 @@ def de_json(cls, data: Optional[JSONDict], bot: "Bot") -> Optional["UsersShared"

return super()._de_json(data=data, bot=bot, api_kwargs=api_kwargs)

@property
def user_ids(self) -> Tuple[int, ...]:
"""
Tuple[:obj:`int`]: Identifiers of the shared users. These numbers may have
more than 32 significant bits and some programming languages may have difficulty/silent
defects in interpreting them. But they have at most 52 significant bits, so 64-bit
integers or double-precision float types are safe for storing these identifiers. The
bot may not have access to the users and could be unable to use these identifiers,
unless the users are already known to the bot by some other means.
.. deprecated:: 21.1
As Bot API 7.2 replaces this attribute with :attr:`users`, this attribute will be
removed in future versions.
"""
warn_about_deprecated_attr_in_property(
deprecated_attr_name="user_ids",
new_attr_name="users",
bot_api_version="7.2",
stacklevel=2,
)
return tuple(user.user_id for user in self.users)


class ChatShared(TelegramObject):
"""
Expand Down
2 changes: 0 additions & 2 deletions telegram/ext/_extbot.py
Expand Up @@ -1185,7 +1185,6 @@ async def create_new_sticker_set(
name: str,
title: str,
stickers: Sequence["InputSticker"],
sticker_format: Optional[str] = None,
sticker_type: Optional[str] = None,
needs_repainting: Optional[bool] = None,
*,
Expand All @@ -1201,7 +1200,6 @@ async def create_new_sticker_set(
name=name,
title=title,
stickers=stickers,
sticker_format=sticker_format,
sticker_type=sticker_type,
needs_repainting=needs_repainting,
read_timeout=read_timeout,
Expand Down
24 changes: 0 additions & 24 deletions tests/_files/test_sticker.py
Expand Up @@ -39,7 +39,6 @@
from telegram.constants import ParseMode, StickerFormat, StickerType
from telegram.error import BadRequest, TelegramError
from telegram.request import RequestData
from telegram.warnings import PTBDeprecationWarning
from tests.auxil.bot_method_checks import (
check_defaults_handling,
check_shortcut_call,
Expand Down Expand Up @@ -714,7 +713,6 @@ async def make_assertion(_, data, *args, **kwargs):
assert data["name"] == "name"
assert data["title"] == "title"
assert data["stickers"] == ["wow.png", "wow.tgs", "wow.webp"]
assert data["sticker_format"] == "static"
assert data["needs_repainting"] is True

monkeypatch.setattr(bot, "_post", make_assertion)
Expand All @@ -723,7 +721,6 @@ async def make_assertion(_, data, *args, **kwargs):
"name",
"title",
stickers=["wow.png", "wow.tgs", "wow.webp"],
sticker_format=StickerFormat.STATIC,
needs_repainting=True,
)

Expand Down Expand Up @@ -784,27 +781,6 @@ async def make_assertion(*_, **kwargs):
monkeypatch.setattr(sticker.get_bot(), "get_file", make_assertion)
assert await sticker.get_file()

async def test_create_new_sticker_set_format_arg_depr(
self, bot, chat_id, sticker_file, monkeypatch
):
async def make_assertion(*_, **kwargs):
pass

monkeypatch.setattr(bot, "_post", make_assertion)
with pytest.warns(PTBDeprecationWarning, match="`sticker_format` is deprecated"):
await bot.create_new_sticker_set(
chat_id,
"name",
"title",
stickers=sticker_file,
sticker_format="static",
)

async def test_deprecation_creation_args(self, recwarn):
with pytest.warns(PTBDeprecationWarning, match="The parameters `is_animated` and ") as w:
StickerSet("name", "title", [], "static", is_animated=True)
assert w[0].filename == __file__, "wrong stacklevel!"


@pytest.mark.xdist_group("stickerset")
class TestStickerSetWithRequest:
Expand Down
6 changes: 1 addition & 5 deletions tests/test_official/exceptions.py
Expand Up @@ -166,11 +166,7 @@ def ignored_param_requirements(object_name: str) -> set[str]:


# Arguments that are optional arguments for now for backwards compatibility
BACKWARDS_COMPAT_KWARGS: dict[str, set[str]] = {
"create_new_sticker_set": {"sticker_format"}, # removed by bot api 7.2
"StickerSet": {"is_animated", "is_video"}, # removed by bot api 7.2
"UsersShared": {"user_ids", "users"}, # removed/added by bot api 7.2
}
BACKWARDS_COMPAT_KWARGS: dict[str, set[str]] = {}


def backwards_compat_kwargs(object_name: str) -> set[str]:
Expand Down
16 changes: 0 additions & 16 deletions tests/test_shared.py
Expand Up @@ -20,7 +20,6 @@
import pytest

from telegram import ChatShared, PhotoSize, SharedUser, UsersShared
from telegram.warnings import PTBDeprecationWarning
from tests.auxil.slots import mro_slots


Expand Down Expand Up @@ -59,24 +58,9 @@ def test_de_json(self, bot):

assert users_shared.request_id == self.request_id
assert users_shared.users == self.users
assert users_shared.user_ids == tuple(self.user_ids)

assert UsersShared.de_json({}, bot) is None

def test_users_is_required_argument(self):
with pytest.raises(TypeError, match="`users` is a required argument"):
UsersShared(self.request_id, user_ids=self.user_ids)

def test_user_ids_deprecation_warning(self):
with pytest.warns(
PTBDeprecationWarning, match="'user_ids' was renamed to 'users' in Bot API 7.2"
):
users_shared = UsersShared(self.request_id, user_ids=self.user_ids, users=self.users)
with pytest.warns(
PTBDeprecationWarning, match="renamed the attribute 'user_ids' to 'users'"
):
users_shared.user_ids

def test_equality(self):
a = UsersShared(self.request_id, users=self.users)
b = UsersShared(self.request_id, users=self.users)
Expand Down

0 comments on commit ee6e82d

Please sign in to comment.