Skip to content

Commit c6eb99c

Browse files
anoadragon453dependabot[bot]
andauthoredJun 13, 2024··
Bump mypy from 1.8.0 to 1.9.0 (#17297)
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
1 parent 5db3eec commit c6eb99c

File tree

4 files changed

+82
-40
lines changed

4 files changed

+82
-40
lines changed
 

‎changelog.d/17297.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Bump `mypy` from 1.8.0 to 1.9.0.

‎poetry.lock

+28-28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/push/test_email.py

+29-8
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,24 @@ def test_unsubscribe(self, use_post: bool) -> None:
205205

206206
# Multipart: plain text, base 64 encoded; html, base 64 encoded
207207
multipart_msg = email.message_from_bytes(msg)
208-
txt = multipart_msg.get_payload()[0].get_payload(decode=True).decode()
209-
html = multipart_msg.get_payload()[1].get_payload(decode=True).decode()
208+
209+
# Extract the text (non-HTML) portion of the multipart Message,
210+
# as a Message.
211+
txt_message = multipart_msg.get_payload(i=0)
212+
assert isinstance(txt_message, email.message.Message)
213+
214+
# Extract the actual bytes from the Message object, and decode them to a `str`.
215+
txt_bytes = txt_message.get_payload(decode=True)
216+
assert isinstance(txt_bytes, bytes)
217+
txt = txt_bytes.decode()
218+
219+
# Do the same for the HTML portion of the multipart Message.
220+
html_message = multipart_msg.get_payload(i=1)
221+
assert isinstance(html_message, email.message.Message)
222+
html_bytes = html_message.get_payload(decode=True)
223+
assert isinstance(html_bytes, bytes)
224+
html = html_bytes.decode()
225+
210226
self.assertIn("/_synapse/client/unsubscribe", txt)
211227
self.assertIn("/_synapse/client/unsubscribe", html)
212228

@@ -347,12 +363,17 @@ def test_room_notifications_include_avatar(self) -> None:
347363
# That email should contain the room's avatar
348364
msg: bytes = args[5]
349365
# Multipart: plain text, base 64 encoded; html, base 64 encoded
350-
html = (
351-
email.message_from_bytes(msg)
352-
.get_payload()[1]
353-
.get_payload(decode=True)
354-
.decode()
355-
)
366+
367+
# Extract the html Message object from the Multipart Message.
368+
# We need the asserts to convince mypy that this is OK.
369+
html_message = email.message_from_bytes(msg).get_payload(i=1)
370+
assert isinstance(html_message, email.message.Message)
371+
372+
# Extract the `bytes` from the html Message object, and decode to a `str`.
373+
html = html_message.get_payload(decode=True)
374+
assert isinstance(html, bytes)
375+
html = html.decode()
376+
356377
self.assertIn("_matrix/media/v1/thumbnail/DUMMY_MEDIA_ID", html)
357378

358379
def test_empty_room(self) -> None:

‎tests/rest/client/test_account.py

+24-4
Original file line numberDiff line numberDiff line change
@@ -427,13 +427,23 @@ def _get_link_from_email(self) -> str:
427427
text = None
428428
for part in mail.walk():
429429
if part.get_content_type() == "text/plain":
430-
text = part.get_payload(decode=True).decode("UTF-8")
430+
text = part.get_payload(decode=True)
431+
if text is not None:
432+
# According to the logic table in `get_payload`, we know that
433+
# the result of `get_payload` will be `bytes`, but mypy doesn't
434+
# know this and complains. Thus, we assert the type.
435+
assert isinstance(text, bytes)
436+
text = text.decode("UTF-8")
437+
431438
break
432439

433440
if not text:
434441
self.fail("Could not find text portion of email to parse")
435442

436-
assert text is not None
443+
# `text` must be a `str`, after being decoded and determined just above
444+
# to not be `None` or an empty `str`.
445+
assert isinstance(text, str)
446+
437447
match = re.search(r"https://example.com\S+", text)
438448
assert match, "Could not find link in email"
439449

@@ -1209,13 +1219,23 @@ def _get_link_from_email(self) -> str:
12091219
text = None
12101220
for part in mail.walk():
12111221
if part.get_content_type() == "text/plain":
1212-
text = part.get_payload(decode=True).decode("UTF-8")
1222+
text = part.get_payload(decode=True)
1223+
if text is not None:
1224+
# According to the logic table in `get_payload`, we know that
1225+
# the result of `get_payload` will be `bytes`, but mypy doesn't
1226+
# know this and complains. Thus, we assert the type.
1227+
assert isinstance(text, bytes)
1228+
text = text.decode("UTF-8")
1229+
12131230
break
12141231

12151232
if not text:
12161233
self.fail("Could not find text portion of email to parse")
12171234

1218-
assert text is not None
1235+
# `text` must be a `str`, after being decoded and determined just above
1236+
# to not be `None` or an empty `str`.
1237+
assert isinstance(text, str)
1238+
12191239
match = re.search(r"https://example.com\S+", text)
12201240
assert match, "Could not find link in email"
12211241

0 commit comments

Comments
 (0)
Please sign in to comment.