zerver/lib/email_mirror.py: Improve annotation in python 3.

Add asserts and if statements to help mypy.
This commit is contained in:
Eklavya Sharma 2016-07-04 20:43:24 +05:30 committed by Tim Abbott
parent 4f221c21a0
commit 5e81a4d93f
2 changed files with 15 additions and 10 deletions

View File

@ -42,7 +42,6 @@ exclude_py2 = []
exclude_py3 = """ exclude_py3 = """
zerver/lib/actions.py zerver/lib/actions.py
zerver/lib/ccache.py zerver/lib/ccache.py
zerver/lib/email_mirror.py
zerver/lib/notifications.py zerver/lib/notifications.py
zerver/lib/tornado_ioloop_logging.py zerver/lib/tornado_ioloop_logging.py
zerver/management/commands/create_stream.py zerver/management/commands/create_stream.py

View File

@ -19,7 +19,7 @@ from zerver.lib.str_utils import force_text
from zerver.models import Stream, Recipient, get_user_profile_by_email, \ from zerver.models import Stream, Recipient, get_user_profile_by_email, \
get_user_profile_by_id, get_display_recipient, get_recipient, \ get_user_profile_by_id, get_display_recipient, get_recipient, \
Message, Realm, UserProfile Message, Realm, UserProfile
from six import text_type from six import text_type, binary_type
import six import six
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -188,9 +188,10 @@ def get_message_part_by_type(message, content_type):
for idx, part in enumerate(message.walk()): for idx, part in enumerate(message.walk()):
if part.get_content_type() == content_type: if part.get_content_type() == content_type:
content = part.get_payload(decode=True) content = part.get_payload(decode=True)
assert isinstance(content, binary_type)
if charsets[idx]: if charsets[idx]:
content = content.decode(charsets[idx], errors="ignore") text = content.decode(charsets[idx], errors="ignore")
return content return text
def extract_body(message): def extract_body(message):
# type: (message.Message) -> text_type # type: (message.Message) -> text_type
@ -232,12 +233,17 @@ def extract_and_upload_attachments(message, realm):
content_type = part.get_content_type() content_type = part.get_content_type()
filename = part.get_filename() filename = part.get_filename()
if filename: if filename:
s3_url = upload_message_image(filename, content_type, attachment = part.get_payload(decode=True)
part.get_payload(decode=True), if isinstance(attachment, binary_type):
user_profile, s3_url = upload_message_image(filename, content_type,
target_realm=realm) attachment,
formatted_link = u"[%s](%s)" % (filename, s3_url) user_profile,
attachment_links.append(formatted_link) target_realm=realm)
formatted_link = u"[%s](%s)" % (filename, s3_url)
attachment_links.append(formatted_link)
else:
logger.warning("Payload is not bytes (invalid attachment %s in message from %s)." %
(filename, message.get("From")))
return u"\n".join(attachment_links) return u"\n".join(attachment_links)