Skip to content

Commit 65e2d96

Browse files
committed
Simplify decode_header: remove to_native/to_unicode wrappers
Use direct isinstance check and bytes.decode() instead of to_native/to_unicode. Clearer control flow, no type: ignore needed.
1 parent c3c76bf commit 65e2d96

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

emails/utils.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,14 @@ def get_fqdn(self) -> str:
139139

140140
def decode_header(value: str | bytes, default: str = "utf-8", errors: str = 'strict') -> str:
141141
"""Decode the specified header value"""
142-
native = to_native(value, charset=default, errors=errors)
143-
assert native is not None # to_native returns None only for None input
142+
if isinstance(value, bytes):
143+
value = value.decode(default, errors)
144144
parts: list[str] = []
145-
for text, charset in decode_header_(native):
146-
decoded = to_unicode(text, charset or default, errors)
147-
assert decoded is not None
148-
parts.append(decoded)
145+
for text, charset in decode_header_(value):
146+
if isinstance(text, bytes):
147+
parts.append(text.decode(charset or default, errors))
148+
else:
149+
parts.append(text)
149150
return "".join(parts)
150151

151152

0 commit comments

Comments
 (0)