Skip to content

Input media subclasses init handling refactor #2573 #2717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
331c4f5
Temporarily enable tests for the v14 branch
Bibo-Joshi Jul 14, 2021
9dc30cf
Move and Rename TelegramDecryptionError to telegram.error.PassportDec…
harshil21 Aug 11, 2021
b089cc9
Add Code Comment Guidelines to Contribution Guide (#2612)
Poolitzer Aug 11, 2021
3a19383
Improve Type Hinting for CallbackContext (#2587)
revolter Aug 12, 2021
414a18c
Add Custom pytest Marker to Ease Development (#2628)
Poolitzer Aug 12, 2021
8edb49d
Make BasePersistence Methods Abstract (#2624)
starry-shivam Aug 12, 2021
cce9c18
Refactor Initialization of Persistence Classes (#2604)
Bibo-Joshi Aug 13, 2021
dc0207a
Remove `__dict__` from `__slots__` and drop Python 3.6 (#2619, #2636)
harshil21 Aug 19, 2021
4af252f
Remove day_is_strict argument of JobQueue.run_monthly (#2634)
iota-008 Aug 26, 2021
b6e4b54
Drop Non-CallbackContext API (#2617)
Poolitzer Aug 29, 2021
4359812
Fix Signatures and Improve test_official (#2643)
harshil21 Aug 29, 2021
dbdd35e
Remove Deprecated Functionality (#2644)
Bibo-Joshi Aug 30, 2021
a7c7c82
Add User Friendly Type Check For Init Of {Inline, Reply}KeyboardMarku…
harshil21 Sep 15, 2021
8ac65fc
Refine Dispatcher.dispatch_error (#2660)
Bibo-Joshi Sep 17, 2021
66f0604
Warnings Overhaul (#2662)
harshil21 Sep 20, 2021
4f21c06
Clear Up Import Policy (#2671)
Bibo-Joshi Sep 22, 2021
5542a46
Make InlineQuery.answer Raise ValueError (#2675)
Bibo-Joshi Sep 24, 2021
2cf9311
Doc Fixes (#2597)
Bibo-Joshi Oct 1, 2021
c966041
created _MediumBase class and media attribute mixins classes, refacto…
eldbud Oct 1, 2021
bcc62bb
Merge remote-tracking branch 'upstream/v14' into v14
eldbud Oct 3, 2021
7dc3e56
Merge branch 'v14' into InputMedia-subclasses-__init__-handling-refac…
eldbud Oct 4, 2021
d0eb6d4
committing hopefully
eldbud Oct 7, 2021
9ed515a
Merge remote-tracking branch 'upstream/v14' into v14
eldbud Oct 7, 2021
800603a
Merge branch 'v14' into InputMedia-subclasses-__init__-handling-refac…
eldbud Oct 7, 2021
898fe00
added ver doc
eldbud Oct 8, 2021
e2b7a4a
added ver doc
eldbud Oct 8, 2021
4aa2d5d
attended to review notes
eldbud Oct 9, 2021
4231b89
docs fix
eldbud Oct 9, 2021
6d04075
attending to review notes
eldbud Oct 11, 2021
20765d7
attending to review notes
eldbud Oct 11, 2021
d82473a
Merge remote-tracking branch 'upstream/v14' into v14
eldbud Oct 11, 2021
b6ea24d
merged from v14
eldbud Oct 11, 2021
d83b4f2
made base media modules protected
eldbud Oct 11, 2021
bab0f54
closes #2573
eldbud Oct 11, 2021
c1cb104
closes #2573
eldbud Oct 11, 2021
f688d91
fixed test_official[InputFile]
eldbud Oct 12, 2021
13e7dfb
fixed docs generation to include methods
eldbud Oct 14, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/source/telegram.animation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
telegram.Animation
==================

.. Also lists methods of _BaseThumbedMedium, but not the ones of TelegramObject

.. autoclass:: telegram.Animation
:members:
:show-inheritance:
:inherited-members: TelegramObject
3 changes: 3 additions & 0 deletions docs/source/telegram.audio.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
telegram.Audio
==============

.. Also lists methods of _BaseThumbedMedium, but not the ones of TelegramObject

.. autoclass:: telegram.Audio
:members:
:show-inheritance:
:inherited-members: TelegramObject
2 changes: 2 additions & 0 deletions docs/source/telegram.document.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

telegram.Document
=================
.. Also lists methods of _BaseThumbedMedium, but not the ones of TelegramObject

.. autoclass:: telegram.Document
:members:
:show-inheritance:
:inherited-members: TelegramObject
2 changes: 2 additions & 0 deletions docs/source/telegram.photosize.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

telegram.PhotoSize
==================
.. Also lists methods of _BaseThumbedMedium, but not the ones of TelegramObject

.. autoclass:: telegram.PhotoSize
:members:
:show-inheritance:
:inherited-members: TelegramObject
3 changes: 3 additions & 0 deletions docs/source/telegram.sticker.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
telegram.Sticker
================

.. Also lists methods of _BaseThumbedMedium, but not the ones of TelegramObject

.. autoclass:: telegram.Sticker
:members:
:show-inheritance:
:inherited-members: TelegramObject
3 changes: 3 additions & 0 deletions docs/source/telegram.video.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
telegram.Video
==============

.. Also lists methods of _BaseThumbedMedium, but not the ones of TelegramObject

.. autoclass:: telegram.Video
:members:
:show-inheritance:
:inherited-members: TelegramObject
3 changes: 3 additions & 0 deletions docs/source/telegram.videonote.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
telegram.VideoNote
==================

.. Also lists methods of _BaseThumbedMedium, but not the ones of TelegramObject

.. autoclass:: telegram.VideoNote
:members:
:show-inheritance:
:inherited-members: TelegramObject
3 changes: 3 additions & 0 deletions docs/source/telegram.voice.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
telegram.Voice
==============

.. Also lists methods of _BaseThumbedMedium, but not the ones of TelegramObject

.. autoclass:: telegram.Voice
:members:
:show-inheritance:
:inherited-members: TelegramObject
4 changes: 1 addition & 3 deletions telegram/_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,7 @@ def _insert_defaults( # pylint: disable=no-self-use
for key, val in data.items():
# 1)
if isinstance(val, InputMedia):
val.parse_mode = DefaultValue.get_value( # type: ignore[attr-defined]
val.parse_mode # type: ignore[attr-defined]
)
val.parse_mode = DefaultValue.get_value(val.parse_mode)
elif key == 'media' and isinstance(val, list):
for media in val:
media.parse_mode = DefaultValue.get_value(media.parse_mode)
Expand Down
82 changes: 82 additions & 0 deletions telegram/_files/_basemedium.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2021
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser Public License for more details.
#
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""Common base class for media objects"""
from typing import TYPE_CHECKING

from telegram import TelegramObject
from telegram._utils.defaultvalue import DEFAULT_NONE
from telegram._utils.types import JSONDict, ODVInput

if TYPE_CHECKING:
from telegram import Bot, File


class _BaseMedium(TelegramObject):
"""Base class for objects representing the various media file types.
Objects of this class are comparable in terms of equality. Two objects of this class are
considered equal, if their :attr:`file_unique_id` is equal.

Args:
file_id (:obj:`str`): Identifier for this file, which can be used to download
or reuse the file.
file_unique_id (:obj:`str`): Unique identifier for this file, which
is supposed to be the same over time and for different bots.
Can't be used to download or reuse the file.
file_size (:obj:`int`, optional): File size.
bot (:class:`telegram.Bot`, optional): The Bot to use for instance methods.

Attributes:
file_id (:obj:`str`): File identifier.
file_unique_id (:obj:`str`): Unique identifier for this file, which
is supposed to be the same over time and for different bots.
Can't be used to download or reuse the file.
file_size (:obj:`int`): Optional. File size.
bot (:class:`telegram.Bot`): Optional. The Bot to use for instance methods.

"""

__slots__ = ('bot', 'file_id', 'file_size', 'file_unique_id')

def __init__(
self, file_id: str, file_unique_id: str, file_size: int = None, bot: 'Bot' = None
):
# Required
self.file_id: str = str(file_id)
self.file_unique_id = str(file_unique_id)
# Optionals
self.file_size = file_size
self.bot = bot

self._id_attrs = (self.file_unique_id,)

def get_file(
self, timeout: ODVInput[float] = DEFAULT_NONE, api_kwargs: JSONDict = None
) -> 'File':
"""Convenience wrapper over :attr:`telegram.Bot.get_file`

For the documentation of the arguments, please see :meth:`telegram.Bot.get_file`.

Returns:
:class:`telegram.File`

Raises:
:class:`telegram.error.TelegramError`

"""
return self.bot.get_file(file_id=self.file_id, timeout=timeout, api_kwargs=api_kwargs)
85 changes: 85 additions & 0 deletions telegram/_files/_basethumbedmedium.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2021
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser Public License for more details.
#
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""Common base class for media objects with thumbnails"""
from typing import TYPE_CHECKING, TypeVar, Type, Optional

from telegram import PhotoSize
from telegram._files._basemedium import _BaseMedium
from telegram._utils.types import JSONDict

if TYPE_CHECKING:
from telegram import Bot

ThumbedMT = TypeVar('ThumbedMT', bound='_BaseThumbedMedium', covariant=True)


class _BaseThumbedMedium(_BaseMedium):
"""Base class for objects representing the various media file types that may include a
thumbnail.

Objects of this class are comparable in terms of equality. Two objects of this class are
considered equal, if their :attr:`file_unique_id` is equal.

Args:
file_id (:obj:`str`): Identifier for this file, which can be used to download
or reuse the file.
file_unique_id (:obj:`str`): Unique identifier for this file, which
is supposed to be the same over time and for different bots.
Can't be used to download or reuse the file.
file_size (:obj:`int`, optional): File size.
thumb (:class:`telegram.PhotoSize`, optional): Thumbnail as defined by sender.
bot (:class:`telegram.Bot`, optional): The Bot to use for instance methods.

Attributes:
file_id (:obj:`str`): File identifier.
file_unique_id (:obj:`str`): Unique identifier for this file, which
is supposed to be the same over time and for different bots.
Can't be used to download or reuse the file.
file_size (:obj:`int`): Optional. File size.
thumb (:class:`telegram.PhotoSize`): Optional. Thumbnail as defined by sender.
bot (:class:`telegram.Bot`): Optional. The Bot to use for instance methods.

"""

__slots__ = ('thumb',)

def __init__(
self,
file_id: str,
file_unique_id: str,
file_size: int = None,
thumb: PhotoSize = None,
bot: 'Bot' = None,
):
super().__init__(
file_id=file_id, file_unique_id=file_unique_id, file_size=file_size, bot=bot
)
self.thumb = thumb

@classmethod
def de_json(cls: Type[ThumbedMT], data: Optional[JSONDict], bot: 'Bot') -> Optional[ThumbedMT]:
"""See :meth:`telegram.TelegramObject.de_json`."""
data = cls._parse_data(data)

if not data:
return None

data['thumb'] = PhotoSize.de_json(data.get('thumb'), bot)

return cls(bot=bot, **data)
70 changes: 15 additions & 55 deletions telegram/_files/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@
# 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 represents a Telegram Animation."""
from typing import TYPE_CHECKING, Any, Optional
from typing import TYPE_CHECKING, Any

from telegram import PhotoSize, TelegramObject
from telegram._utils.defaultvalue import DEFAULT_NONE
from telegram._utils.types import JSONDict, ODVInput
from telegram import PhotoSize
from telegram._files._basethumbedmedium import _BaseThumbedMedium

if TYPE_CHECKING:
from telegram import Bot, File
from telegram import Bot


class Animation(TelegramObject):
class Animation(_BaseThumbedMedium):
"""This object represents an animation file (GIF or H.264/MPEG-4 AVC video without sound).

Objects of this class are comparable in terms of equality. Two objects of this class are
Expand Down Expand Up @@ -65,18 +64,7 @@ class Animation(TelegramObject):

"""

__slots__ = (
'bot',
'width',
'file_id',
'file_size',
'file_name',
'thumb',
'duration',
'mime_type',
'height',
'file_unique_id',
)
__slots__ = ('duration', 'height', 'file_name', 'mime_type', 'width')

def __init__(
self,
Expand All @@ -92,45 +80,17 @@ def __init__(
bot: 'Bot' = None,
**_kwargs: Any,
):
super().__init__(
file_id=file_id,
file_unique_id=file_unique_id,
file_size=file_size,
thumb=thumb,
bot=bot,
)
# Required
self.file_id = str(file_id)
self.file_unique_id = str(file_unique_id)
self.width = int(width)
self.height = int(height)
self.duration = duration
# Optionals
self.thumb = thumb
self.file_name = file_name
# Optional
self.mime_type = mime_type
self.file_size = file_size
self.bot = bot

self._id_attrs = (self.file_unique_id,)

@classmethod
def de_json(cls, data: Optional[JSONDict], bot: 'Bot') -> Optional['Animation']:
"""See :meth:`telegram.TelegramObject.de_json`."""
data = cls._parse_data(data)

if not data:
return None

data['thumb'] = PhotoSize.de_json(data.get('thumb'), bot)

return cls(bot=bot, **data)

def get_file(
self, timeout: ODVInput[float] = DEFAULT_NONE, api_kwargs: JSONDict = None
) -> 'File':
"""Convenience wrapper over :attr:`telegram.Bot.get_file`

For the documentation of the arguments, please see :meth:`telegram.Bot.get_file`.

Returns:
:class:`telegram.File`

Raises:
:class:`telegram.error.TelegramError`

"""
return self.bot.get_file(file_id=self.file_id, timeout=timeout, api_kwargs=api_kwargs)
self.file_name = file_name
Loading