Skip to content

Commit a7182d0

Browse files
committed
Reduce type: ignore comments from 26 to 14
Replace suppressions with proper fixes where possible: - Use isinstance checks instead of hasattr for type narrowing - Use assert for None-safety where input guarantees non-None - Access self._data directly instead of getattr - Remove redundant to_unicode calls on already-str values - Use explicit if/else instead of tricky and/or expressions - Use dict[] instead of .get() where key is guaranteed present Remaining 14 ignores are genuine mypy limitations: vendored dkim API, private stdlib attrs, intentional method overrides, and decorator typing.
1 parent 22fc286 commit a7182d0

4 files changed

Lines changed: 28 additions & 14 deletions

File tree

emails/backend/smtp/backend.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def __init__(self, ssl: bool = False, fail_silently: bool = True,
5050
self.smtp_cls_kwargs = kwargs
5151

5252
self.host: str | None = kwargs.get('host')
53-
self.port: int = kwargs.get('port') # type: ignore[assignment]
53+
self.port: int = kwargs['port'] # always set as int two lines above
5454
self.fail_silently = fail_silently
5555
self.mail_options = mail_options or []
5656

emails/signers.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ def get_sign_header(self, message: bytes) -> tuple[str, str] | None:
5858
# pydkim returns string, so we should split
5959
s = self.get_sign_string(message)
6060
if s:
61-
(header, value) = to_native(s).split(': ', 1) # type: ignore[union-attr]
61+
native = to_native(s)
62+
assert native is not None # s is bytes, to_native always returns str
63+
(header, value) = native.split(': ', 1)
6264
if value.endswith("\r\n"):
6365
value = value[:-2]
6466
return header, value
@@ -73,7 +75,9 @@ def sign_message(self, msg: MIMEMultipart) -> MIMEMultipart:
7375
# but py3 smtplib requires str to send DATA command (#
7476
# so we have to convert msg.as_string
7577

76-
dkim_header = self.get_sign_header(to_bytes(msg.as_string())) # type: ignore[arg-type]
78+
msg_bytes = to_bytes(msg.as_string())
79+
assert msg_bytes is not None
80+
dkim_header = self.get_sign_header(msg_bytes)
7781
if dkim_header:
7882
msg._headers.insert(0, dkim_header) # type: ignore[attr-defined]
7983
return msg
@@ -87,12 +91,20 @@ def sign_message_string(self, message_string: str) -> str:
8791
# but py3 smtplib requires str to send DATA command
8892
# so we have to convert message_string
8993

90-
s = self.get_sign_string(to_bytes(message_string)) # type: ignore[arg-type]
91-
return s and to_native(s) + message_string or message_string # type: ignore[operator]
94+
msg_bytes = to_bytes(message_string)
95+
assert msg_bytes is not None
96+
s = self.get_sign_string(msg_bytes)
97+
if s:
98+
header = to_native(s)
99+
assert header is not None
100+
return header + message_string
101+
return message_string
92102

93103
def sign_message_bytes(self, message_bytes: bytes) -> bytes:
94104
"""
95105
Insert DKIM header to message bytes
96106
"""
97107
s = self.get_sign_bytes(message_bytes)
98-
return s and to_bytes(s) + message_bytes or message_bytes # type: ignore[operator]
108+
if s:
109+
return s + message_bytes
110+
return message_bytes

emails/store/store.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def remove(self, uri: BaseFile | str) -> None:
5454
del self._filenames[filename]
5555
del self._files[uri]
5656

57-
def unique_filename(self, filename: str | None, uri: str | None = None) -> str:
57+
def unique_filename(self, filename: str | None, uri: str | None = None) -> str | None:
5858

5959
if filename in self._filenames:
6060
n = 1
@@ -66,9 +66,10 @@ def unique_filename(self, filename: str | None, uri: str | None = None) -> str:
6666
if filename not in self._filenames:
6767
break
6868

69-
self._filenames[filename] = uri # type: ignore[index]
69+
if filename is not None:
70+
self._filenames[filename] = uri
7071

71-
return filename # type: ignore[return-value]
72+
return filename
7273

7374
def add(self, value: BaseFile | dict[str, Any], replace: bool = False) -> BaseFile:
7475

emails/utils.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,9 @@ def get_fqdn(self) -> str:
128128

129129
def decode_header(value: str | bytes, default: str = "utf-8", errors: str = 'strict') -> str:
130130
"""Decode the specified 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]
131+
native = to_native(value, charset=default, errors=errors)
132+
assert native is not None # to_native returns None only for None input
133+
return "".join([to_unicode(text, charset or default, errors) for text, charset in decode_header_(native)]) # type: ignore[misc]
133134

134135

135136
class MessageID:
@@ -213,7 +214,7 @@ def parse_name_and_email(obj: str | tuple[str | None, str] | list[str],
213214

214215
def sanitize_email(addr: str, encoding: str = 'ascii', parse: bool = False) -> str:
215216
if parse:
216-
_, addr = parseaddr(to_unicode(addr)) # type: ignore[arg-type]
217+
_, addr = parseaddr(addr)
217218
try:
218219
addr.encode('ascii')
219220
except UnicodeEncodeError: # IDN
@@ -229,7 +230,7 @@ def sanitize_email(addr: str, encoding: str = 'ascii', parse: bool = False) -> s
229230

230231
def sanitize_address(addr: str | tuple[str, str], encoding: str = 'ascii') -> str:
231232
if isinstance(addr, str):
232-
addr = parseaddr(to_unicode(addr)) # type: ignore[arg-type]
233+
addr = parseaddr(addr)
233234
nm, addr = addr
234235
# This try-except clause is needed on Python 3 < 3.2.4
235236
# http://bugs.python.org/issue14291
@@ -300,7 +301,7 @@ def fetch_url(url: str, valid_http_codes: tuple[int, ...] = (200, ),
300301

301302
def encode_header(value: str | Any, charset: str = 'utf-8') -> str | Any:
302303
if isinstance(value, str):
303-
value = to_unicode(value, charset=charset).rstrip() # type: ignore[union-attr]
304+
value = value.rstrip()
304305
_r = Header(value, charset)
305306
return str(_r)
306307
else:

0 commit comments

Comments
 (0)