1717from email .mime .text import MIMEText
1818from email .mime .multipart import MIMEMultipart
1919from email .header import Header , decode_header as decode_header_
20- from email .utils import parseaddr , formatdate , escapesre , specialsre
20+ from email .utils import parseaddr , formatdate
21+ from email .utils import escapesre , specialsre # type: ignore[attr-defined] # private but stable
2122
2223from . import USER_AGENT
2324from .exc import HTTPLoaderError
@@ -40,8 +41,8 @@ def to_unicode(x: Any, charset: str | None = sys.getdefaultencoding(),
4041 if not isinstance (x , bytes ):
4142 return str (x )
4243 if charset is None and allow_none_charset :
43- return x
44- return x .decode (charset , errors )
44+ return x # type: ignore[return-value] # returns bytes when allow_none_charset=True
45+ return x .decode (charset , errors ) # type: ignore[arg-type] # charset is str here
4546
4647
4748def to_bytes (x : str | bytes | bytearray | memoryview | None ,
@@ -127,8 +128,8 @@ def get_fqdn(self) -> str:
127128
128129def decode_header (value : str | bytes , default : str = "utf-8" , errors : str = 'strict' ) -> str :
129130 """Decode the specified header value"""
130- value = to_native (value , charset = default , errors = errors )
131- return "" .join ([to_unicode (text , charset or default , errors ) for text , charset in decode_header_ (value )])
131+ value = to_native (value , charset = default , errors = errors ) # type: ignore[assignment]
132+ return "" .join ([to_unicode (text , charset or default , errors ) for text , charset in decode_header_ (value )]) # type: ignore[misc, arg-type]
132133
133134
134135class MessageID :
@@ -212,7 +213,7 @@ def parse_name_and_email(obj: str | tuple[str | None, str] | list[str],
212213
213214def sanitize_email (addr : str , encoding : str = 'ascii' , parse : bool = False ) -> str :
214215 if parse :
215- _ , addr = parseaddr (to_unicode (addr ))
216+ _ , addr = parseaddr (to_unicode (addr )) # type: ignore[arg-type]
216217 try :
217218 addr .encode ('ascii' )
218219 except UnicodeEncodeError : # IDN
@@ -228,7 +229,7 @@ def sanitize_email(addr: str, encoding: str = 'ascii', parse: bool = False) -> s
228229
229230def sanitize_address (addr : str | tuple [str , str ], encoding : str = 'ascii' ) -> str :
230231 if isinstance (addr , str ):
231- addr = parseaddr (to_unicode (addr ))
232+ addr = parseaddr (to_unicode (addr )) # type: ignore[arg-type]
232233 nm , addr = addr
233234 # This try-except clause is needed on Python 3 < 3.2.4
234235 # http://bugs.python.org/issue14291
@@ -266,21 +267,21 @@ def as_bytes(self, unixfrom: bool = False, linesep: str = '\n') -> bytes:
266267 return fp .getvalue ()
267268
268269
269- class SafeMIMEText (MIMEMixin , MIMEText ):
270+ class SafeMIMEText (MIMEMixin , MIMEText ): # type: ignore[misc] # intentional override
270271 def __init__ (self , text : str , subtype : str , charset : str ) -> None :
271272 self .encoding = charset
272273 MIMEText .__init__ (self , text , subtype , charset )
273274
274275
275- class SafeMIMEMultipart (MIMEMixin , MIMEMultipart ):
276+ class SafeMIMEMultipart (MIMEMixin , MIMEMultipart ): # type: ignore[misc] # intentional override
276277 def __init__ (self , _subtype : str = 'mixed' , boundary : str | None = None ,
277278 _subparts : list [Any ] | None = None ,
278279 encoding : str | None = None , ** _params : Any ) -> None :
279280 self .encoding = encoding
280281 MIMEMultipart .__init__ (self , _subtype , boundary , _subparts , ** _params )
281282
282283
283- DEFAULT_REQUESTS_PARAMS = dict (allow_redirects = True ,
284+ DEFAULT_REQUESTS_PARAMS : dict [ str , Any ] = dict (allow_redirects = True ,
284285 verify = False , timeout = 10 ,
285286 headers = {'User-Agent' : USER_AGENT })
286287
@@ -299,7 +300,7 @@ def fetch_url(url: str, valid_http_codes: tuple[int, ...] = (200, ),
299300
300301def encode_header (value : str | Any , charset : str = 'utf-8' ) -> str | Any :
301302 if isinstance (value , str ):
302- value = to_unicode (value , charset = charset ).rstrip ()
303+ value = to_unicode (value , charset = charset ).rstrip () # type: ignore[union-attr]
303304 _r = Header (value , charset )
304305 return str (_r )
305306 else :
0 commit comments