Skip to content
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

Effective attachment #766

Merged
merged 4 commits into from Aug 7, 2017
Merged
Changes from 2 commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+41 −0
Diff settings

Always

Just for now

Copy path View file
@@ -27,6 +27,9 @@
from telegram.utils.helpers import escape_html, escape_markdown, to_timestamp, from_timestamp


_UNDEFINED = object()


class Message(TelegramObject):
"""
This object represents a message.
@@ -170,6 +173,7 @@ class Message(TelegramObject):
successful_payment (:class:`telegram.SuccessfulPayment`, optional): Message is a service
message about a successful payment, information about the payment.
"""
_effective_attachment = _UNDEFINED

def __init__(self,
message_id,
@@ -301,6 +305,31 @@ def de_json(cls, data, bot):

return cls(bot=bot, **data)

@property
def effective_attachment(self):
"""
Optional[Union[:class:`telegram.Audio`, :class:`telegram.Game`, :class:`telegram.Document`,

This comment has been minimized.

Copy link
@jsmnbom

jsmnbom Aug 1, 2017

Member

Shouldn't it be:

 :class:`telegram.Audio` | :class:`telegram.Game` | :class:`telegram.Document` [...]

I'm not sure our sphinx parser understands the optional and union very well

Edit:
After testing it, it understands neither, so something like below might be clearest actually:

:class:`telegram.Audio` or :class:`telegram.Game` or :class:`telegram.Document` or
    :class:`telegram.Photo` or :class:`telegram.Sticker` or :class:`telegram.Video` or
    :class:`telegram.Voice` or :class:`telegram.VideoNote` or :class:`telegram.Contact` or
    :class:`telegram.Location` or :class:`telegram.Venue` or :class:`telegram.Invoice` or
    :class:`telegram.SuccessfulPayment`: The attachment that this message was
    sent with. May be ``None`` if no attachment was sent.
:class:`telegram.Photo`, :class:`telegram.Sticker`, :class:`telegram.Video`,

This comment has been minimized.

Copy link
@Eldinnie

Eldinnie Aug 1, 2017

Member

telegram.Photo does not exists. Should be List[:class:`telegram.PhotoSize`]

:class:`telegram.Voice`, :class:`telegram.VideoNote`, :class:`telegram.Contact`,
:class:`telegram.Location`, :class:`telegram.Venue`, :class:`telegram.Invoice`,
:class:`telegram.SuccessfulPayment`]]: The attachment that this message was sent with. May
be ``None`` if no attachment was sent.

This comment has been minimized.

Copy link
@Eldinnie

Eldinnie Aug 1, 2017

Member

space too much at start of line, makes weird docs.

"""
if self._effective_attachment is not _UNDEFINED:
return self._effective_attachment

for i in (self.audio, self.game, self.document, self.photo, self.sticker,
self.video, self.voice, self.video_note, self.contact, self.location,
self.venue, self.invoice, self.successful_payment):
if i is not None:
self._effective_attachment = i
break
else:
self._effective_attachment = None

return self._effective_attachment

def __getitem__(self, item):
if item in self.__dict__.keys():
return self.__dict__[item]
Copy path View file
@@ -205,6 +205,18 @@ def test_equality(self):
self.assertNotEqual(a, e)
self.assertNotEqual(hash(a), hash(e))

def test_effective_attachment(self):
for i in ('audio', 'game', 'document', 'photo', 'sticker', 'video', 'voice', 'video_note',
'contact', 'location', 'venue', 'invoice', 'invoice', 'successful_payment'):
dummy = object()
kwargs = {i: dummy}
msg = telegram.Message(1, telegram.User(1, ""), None, None, **kwargs)
self.assertIs(msg.effective_attachment, dummy,
'invalid {} effective attachment'.format(i))

msg = telegram.Message(1, telegram.User(1, ""), None, None)
self.assertIsNone(msg.effective_attachment)


if __name__ == '__main__':
unittest.main()
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.