From 144d6bf0a42634170429fe0e9479a537637a7e55 Mon Sep 17 00:00:00 2001 From: Johannes Larsen Date: Thu, 24 Oct 2024 00:55:46 +0200 Subject: [PATCH] Fix return type of message without a preferred body The `get_body(...)` call (and hence the nominal return of the `get_body_part(...)` method) returns a email.message.EmailMessage. So if there is no body return an empty such class instead of a string, because the callers expects that it behaves like a message. E.g.: File "alot/db/utils.py", line 499, in extract_body_part **{'field_key': 'view'} if body_part.get_content_type() == 'text/plain' ^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'str' object has no attribute 'get_content_type' --- alot/db/utils.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/alot/db/utils.py b/alot/db/utils.py index 0f63cee5e..82c979dcd 100644 --- a/alot/db/utils.py +++ b/alot/db/utils.py @@ -476,7 +476,7 @@ def get_body_part(mail, mimetype=None): :param mail: the mail to use :type mail: :class:`email.message.EmailMessage` :returns: The combined text of any parts to be used - :rtype: str + :rtype: :class:`email.message.EmailMessage` """ if not mimetype: @@ -486,7 +486,9 @@ def get_body_part(mail, mimetype=None): body_part = mail.get_body(preferencelist) if body_part is None: # if no part matching preferredlist was found - return "" + empty = email.message.EmailMessage() + empty.set_payload("") + return empty return body_part