cpython/Lib/test/test_email
Miss Islington (bot) 3bd2818664
[3.13] gh-67022: Document bytes/str inconsistency in email.header.decode_header() and suggest email.headerregistry.HeaderRegistry as a sane alternative (GH-92900) (#135549)
gh-67022: Document bytes/str inconsistency in email.header.decode_header() and suggest email.headerregistry.HeaderRegistry as a sane alternative (GH-92900)

* gh-67022: Document bytes/str inconsistency in email.header.decode_header()

This function's possible return types have been surprising and error-prone
for the entirety of its Python 3.x history. It can return either:

1. `typing.List[typing.Tuple[bytes, typing.Optional[str]]]` of length >1
2. or `typing.List[typing.Tuple[str, None]]`, of length exactly 1

This means that any user of this function must be prepared to accept either
`bytes` or `str` for the first member of the 2-tuples it returns, which is a
very surprising behavior in Python 3.x, particularly given that the second
member of the tuple is supposed to represent the charset/encoding of the
first member.

This patch documents the behavior of this function, and adds test cases
to demonstrate it.

As discussed in bpo-22833, this cannot be changed in a backwards-compatible
way, and some users of this function depend precisely on the existing
behavior.

Add warnings about obsolescence of 'email.header.decode_header' and 'email.header.make_header' functions.

Recommend use of `email.headerregistry.HeaderRegistry` instead, as suggested
in https://github.com/python/cpython/pull/92900#discussion_r1112472177
(cherry picked from commit 60181f4ed0)

Co-authored-by: Dan Lenski <dlenski@gmail.com>
2025-06-15 16:02:43 -04:00
..
data gh-106186: Don't report MultipartInvariantViolationDefect for valid multipart emails when parsing header only (#107016) 2023-07-23 12:25:18 +02:00
__init__.py [3.13] gh-71339: Use new assertion methods in the email tests (GH-129055) (GH-132501) 2025-05-22 09:40:04 +00:00
__main__.py
test__encoded_words.py bpo-43323: Fix UnicodeEncodeError in the email module (GH-32137) 2022-04-30 13:17:23 +03:00
test__header_value_parser.py [3.13] gh-134155: fix AttributeError in email._header_value_parser.get_address (GH-134194) (#135192) 2025-06-06 14:11:15 -04:00
test_asian_codecs.py
test_contentmanager.py [3.13] gh-71339: Use new assertion methods in the email tests (GH-129055) (GH-132501) 2025-05-22 09:40:04 +00:00
test_defect_handling.py [3.13] gh-71339: Use new assertion methods in the email tests (GH-129055) (GH-132501) 2025-05-22 09:40:04 +00:00
test_email.py [3.13] gh-67022: Document bytes/str inconsistency in email.header.decode_header() and suggest email.headerregistry.HeaderRegistry as a sane alternative (GH-92900) (#135549) 2025-06-15 16:02:43 -04:00
test_generator.py [3.13] gh-121650: Encode newlines in headers, and verify headers are sound (GH-122233) (#122484) 2024-08-06 19:06:41 +02:00
test_headerregistry.py [3.13] gh-98188: Fix EmailMessage.get_payload to decode data when CTE value has extra text (GH-127547) (#128528) 2025-01-07 12:43:04 -05:00
test_inversion.py bpo-30681: Support invalid date format or value in email Date header (GH-22090) 2020-10-26 17:31:06 -07:00
test_message.py [3.13] gh-124452: Fix header mismatches when folding/unfolding with email message (GH-125919) (#126917) 2024-11-17 15:06:18 -05:00
test_parser.py
test_pickleable.py
test_policy.py [3.13] gh-121650: Encode newlines in headers, and verify headers are sound (GH-122233) (#122484) 2024-08-06 19:06:41 +02:00
test_utils.py gh-113027: Fix timezone check in test_variable_tzname in test_email (GH-113835) 2024-01-12 11:46:17 +01:00
torture_test.py bpo-5846: Do not use obsolete unittest functions. (GH-28303) 2021-09-13 10:49:53 +03:00