On Sat 2024-04-06 11:40:14 +0800, Sean Whitton wrote:
> On Thu 04 Apr 2024 at 06:37pm -04, Daniel Kahn Gillmor wrote:
>
>> On Wed 2024-04-03 13:03:19 +0800, Sean Whitton wrote:
>>> Thanks, but can you sign this off?  Ty!
>>
>> Sure, attached.  Let me know if you need anything different.
>
> Thanks.  Unfortunately, it doesn't seem to fix the FTBFS, on sid.

Here is a replacement patch, tested now against mypy 1.9.0-4.  It also
updates the typechecking for imap-dl for the same version of mypy.

     --dkg

From 6d27aa566d64a3e8cbc6afa4e61f5f8178edb14e Mon Sep 17 00:00:00 2001
From: Daniel Kahn Gillmor <d...@fifthhorseman.net>
Date: Tue, 30 Jan 2024 15:40:58 -0500
Subject: [PATCH] email-print-mime-structure, imap-dl: clean types with mypy
 1.9.0

(and, update copyright years)

Signed-off-by: Daniel Kahn Gillmor <d...@fifthhorseman.net>
---
 email-print-mime-structure | 22 ++++++++++++++--------
 imap-dl                    | 14 +++++++++-----
 2 files changed, 23 insertions(+), 13 deletions(-)

diff --git a/email-print-mime-structure b/email-print-mime-structure
index b7646e0..3263da9 100755
--- a/email-print-mime-structure
+++ b/email-print-mime-structure
@@ -2,7 +2,7 @@
 # PYTHON_ARGCOMPLETE_OK
 # -*- coding: utf-8 -*-
 
-# Copyright (C) 2019 Daniel Kahn Gillmor
+# Copyright (C) 2019-2024 Daniel Kahn Gillmor
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -39,6 +39,7 @@ import subprocess
 
 from argparse import ArgumentParser, Namespace
 from typing import Optional, Union, List, Tuple, Any
+from types import ModuleType
 from email.charset import Charset
 from email.message import Message
 
@@ -47,8 +48,9 @@ try:
 except ImportError:
     pgpy = None
 
+argcomplete:Optional[ModuleType]
 try:
-    import argcomplete #type: ignore
+    import argcomplete
 except ImportError:
     argcomplete = None
 
@@ -74,7 +76,7 @@ class MimePrinter(object):
             # FIXME: it looks like we are counting chars here, not bytes:
             nbytes = len(z.as_string())
         else:
-            payload:Union[List[Message], str, bytes, None] = z.get_payload()
+            payload = z.get_payload()
             if not isinstance(payload, (str,bytes)):
                 raise TypeError(f'expected payload to be either str or bytes, got {type(payload)}')
             # FIXME: it looks like we are counting chars here, not bytes:
@@ -106,7 +108,7 @@ class MimePrinter(object):
         else:
             if z.get_content_type().lower() == 'application/pkcs7-mime' and \
                str(z.get_param('smime-type')).lower() == 'signed-data':
-                bodypart:Union[List[Message],str,bytes,None] = z.get_payload(decode=True)
+                bodypart = z.get_payload(decode=True)
                 if isinstance(bodypart, bytes):
                     unwrapped = self.pipe_transform(bodypart, ['certtool', '--p7-show-data', '--p7-info', '--inder'])
                     if unwrapped:
@@ -118,7 +120,7 @@ class MimePrinter(object):
 
 
     def decrypt_part(self, msg:Message, flavor:EncType) -> Optional[Message]:
-        ciphertext:Union[List[Message],str,bytes,None] = msg.get_payload(decode=True)
+        ciphertext = msg.get_payload(decode=True)
         cryptopayload:Optional[Message] = None
         if not isinstance(ciphertext, bytes):
             logging.warning('encrypted part was not a leaf mime part somehow')
@@ -178,14 +180,18 @@ class MimePrinter(object):
                 prefix = prefix.rpartition('└')[0] + ' '
             if prefix.endswith('├'):
                 prefix = prefix.rpartition('├')[0] + '│'
-            parts:Union[List[Message], str, bytes, None] = z.get_payload()
+            parts = z.get_payload()
             if not isinstance(parts, list):
                 raise TypeError(f'parts was {type(parts)}, expected List[Message]')
             i = 0
             while (i < len(parts)-1):
-                self.print_tree(parts[i], prefix + '├', z, i+1)
+                msg = parts[i]
+                if isinstance(msg, Message):
+                    self.print_tree(msg, prefix + '├', z, i+1)
                 i += 1
-            self.print_tree(parts[i], prefix + '└', z, i+1)
+            msg = parts[i]
+            if isinstance(msg, Message):
+                self.print_tree(msg, prefix + '└', z, i+1)
             # FIXME: show epilogue?
         else:
             self.print_part(z, prefix+'─╴', parent, num)
diff --git a/imap-dl b/imap-dl
index fac7487..824c21d 100755
--- a/imap-dl
+++ b/imap-dl
@@ -2,7 +2,7 @@
 # PYTHON_ARGCOMPLETE_OK
 # -*- coding: utf-8 -*-
 
-# Copyright (C) 2019-2020 Daniel Kahn Gillmor
+# Copyright (C) 2019-2024 Daniel Kahn Gillmor
 # Copyright (C) 2020      Red Hat, Inc.
 #
 # This program is free software: you can redistribute it and/or modify
@@ -52,14 +52,17 @@ import statistics
 import configparser
 
 from typing import Dict, List, Optional, Tuple, Union
+from types import ModuleType
 
+argcomplete:Optional[ModuleType]
 try:
-    import argcomplete #type: ignore
+    import argcomplete
 except ImportError:
     argcomplete = None
 
+gssapi:Optional[ModuleType]
 try:
-    import gssapi # type: ignore
+    import gssapi
 except ModuleNotFoundError:
     gssapi = None
 
@@ -96,15 +99,16 @@ def auth_builtin(username:str, imap:imaplib.IMAP4,
     except Exception as e:
         raise Exception(f'login failed with {e} as user {username} on {server}')
 
-if gssapi:
+if gssapi is not None:
     # imaplib auth methods need to be in the form of callables, and they all
     # requre both additional parameters and storage beyond what the function
     # interface provides.
     class GSSAPI_handler():
-        gss_vc:gssapi.SecurityContext
         username:str
 
         def __init__(self, server:str, username:str) -> None:
+            if gssapi is None:
+                raise Exception("Impossible state -- gssapi module is not loaded")
             name = gssapi.Name(f'imap@{server}',
                                gssapi.NameType.hostbased_service)
             self.gss_vc = gssapi.SecurityContext(usage="initiate", name=name)
-- 
2.43.0

Attachment: signature.asc
Description: PGP signature

Reply via email to