Mercurial > public > mercurial-scm > hg
comparison mercurial/patch.py @ 52898:9b85f6efcc1d
typing: add a cast to `mercurial.patch` to unconfuse pytype 2024.10.11
I've seen this in other, more modern versions of pytype than what we use in CI-
it thinks the return of `get_payload()` is `Message | bytes`, and then complains
about `if not payload.endswith(b'\n')` below, saying `Message` doesn't have that
function. That's correct, but according to the table in `get_payload()` that
documents what the method returns, the only possible values are `bytes | None`
when `decode=True` and the `i` argument isn't passed. And `None` isn't possible
if we trust the `Content-Type` header.
While here, I added a type hint for the `part` variable, since PyCharm couldn't
figure out what was going on here, and that made navigating to the code
impossible.
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Tue, 11 Feb 2025 21:21:17 -0500 |
parents | 279e217d6041 |
children |
comparison
equal
deleted
inserted
replaced
52897:4fa0d89d1bdb | 52898:9b85f6efcc1d |
---|---|
44 procutil, | 44 procutil, |
45 stringutil, | 45 stringutil, |
46 ) | 46 ) |
47 | 47 |
48 if typing.TYPE_CHECKING: | 48 if typing.TYPE_CHECKING: |
49 import email | |
50 | |
49 from typing import ( | 51 from typing import ( |
50 Any, | 52 Any, |
51 Iterator, | 53 Iterator, |
52 ) | 54 ) |
53 | 55 |
277 if data[b'user']: | 279 if data[b'user']: |
278 ui.debug(b'From: %s\n' % data[b'user']) | 280 ui.debug(b'From: %s\n' % data[b'user']) |
279 diffs_seen = 0 | 281 diffs_seen = 0 |
280 ok_types = (b'text/plain', b'text/x-diff', b'text/x-patch') | 282 ok_types = (b'text/plain', b'text/x-diff', b'text/x-patch') |
281 message = b'' | 283 message = b'' |
284 | |
285 part: email.message.Message | |
282 for part in msg.walk(): | 286 for part in msg.walk(): |
283 content_type = pycompat.bytestr(part.get_content_type()) | 287 content_type = pycompat.bytestr(part.get_content_type()) |
284 ui.debug(b'Content-Type: %s\n' % content_type) | 288 ui.debug(b'Content-Type: %s\n' % content_type) |
285 if content_type not in ok_types: | 289 if content_type not in ok_types: |
286 continue | 290 continue |
287 payload = part.get_payload(decode=True) | 291 |
292 # When decode=True, the only possible return types are bytes or None | |
293 # for a multipart message. But it can't be multipart here, because the | |
294 # Content-Type was just checked. | |
295 payload = typing.cast(bytes, part.get_payload(decode=True)) | |
288 m = diffre.search(payload) | 296 m = diffre.search(payload) |
289 if m: | 297 if m: |
290 hgpatch = False | 298 hgpatch = False |
291 hgpatchheader = False | 299 hgpatchheader = False |
292 ignoretext = False | 300 ignoretext = False |