diff --git a/Doc/library/email.headerregistry.rst b/Doc/library/email.headerregistry.rst index 1cdc0b80c19dc5c..b4e1a43a17309bb 100644 --- a/Doc/library/email.headerregistry.rst +++ b/Doc/library/email.headerregistry.rst @@ -306,6 +306,22 @@ variant, :attr:`~.BaseHeader.max_count` is set to 1. ``quoted-printable``. See :rfc:`2045` for more information. +.. class:: ReferencesHeader + + Handles the :mailheader:`In-Reply-To` and :mailheader:`References` headers. + + .. attribute:: message_ids + + A tuple of the message identifiers in the header, in order. Each one is + in its ```` form, without the comments and folding + white space that may surround it in the header, so it can be compared + with the value of another message's :mailheader:`Message-ID` header or + with the result of :func:`email.utils.make_msgid`. Identifiers that + cannot be parsed are left out and reported in :attr:`~BaseHeader.defects`. + + .. versionadded:: next + + .. class:: HeaderRegistry(base_class=BaseHeader, \ default_class=UnstructuredHeader, \ @@ -343,6 +359,8 @@ variant, :attr:`~.BaseHeader.max_count` is set to 1. :content-disposition: ContentDispositionHeader :content-transfer-encoding: ContentTransferEncodingHeader :message-id: MessageIDHeader + :in-reply-to: ReferencesHeader + :references: ReferencesHeader ``HeaderRegistry`` has the following methods: diff --git a/Doc/whatsnew/3.16.rst b/Doc/whatsnew/3.16.rst index 336171ca041f906..34094233f1788ec 100644 --- a/Doc/whatsnew/3.16.rst +++ b/Doc/whatsnew/3.16.rst @@ -499,6 +499,16 @@ difflib (Contributed by Tomasz Kazimierczak in :gh:`118150`.) +email +----- + +* :class:`email.headerregistry.ReferencesHeader`, used for the + :mailheader:`In-Reply-To` and :mailheader:`References` headers, has a new + :attr:`~email.headerregistry.ReferencesHeader.message_ids` attribute: a tuple + of the message identifiers the header holds. + (Contributed by Sudeep Gowda in :gh:`148797`.) + + encodings --------- diff --git a/Lib/email/headerregistry.py b/Lib/email/headerregistry.py index 48cd85a65ba9f61..ded4e633667e2f3 100644 --- a/Lib/email/headerregistry.py +++ b/Lib/email/headerregistry.py @@ -543,6 +543,17 @@ def parse(cls, value, kwds): kwds['decoded'] = str(parse_tree) kwds['defects'].extend(parse_tree.all_defects) + def init(self, *args, **kw): + super().init(*args, **kw) + # Comments and folding whitespace around a msg-id are not part of it. + self._message_ids = tuple( + ''.join(str(token) for token in msg_id if token.token_type != 'cfws') + for msg_id in self._parse_tree.message_ids) + + @property + def message_ids(self): + return self._message_ids + # The header factory # diff --git a/Lib/test/test_email/test_headerregistry.py b/Lib/test/test_email/test_headerregistry.py index c118fd5e20d048f..37f425bf6cab4dc 100644 --- a/Lib/test/test_email/test_headerregistry.py +++ b/Lib/test/test_email/test_headerregistry.py @@ -3,6 +3,8 @@ import unittest from email import errors from email import policy +from email import utils +from email import message_from_string from email.message import Message from test.test_email import TestEmailBase, parameterize from email import headerregistry @@ -1688,6 +1690,59 @@ def test_group_comparison(self): self.assertTrue(g == ALWAYS_EQ) +class TestReferencesHeader(TestHeaderBase): + + def test_references_message_ids(self): + h = self.make_header('References', + ' (the first) ') + self.assertEqual(h.message_ids, ('', '')) + self.assertDefectsEqual(h.defects, []) + + def test_in_reply_to_message_ids(self): + h = self.make_header('In-Reply-To', ' ') + self.assertEqual(h.message_ids, ('',)) + + def test_message_ids_match_make_msgid(self): + msgid = utils.make_msgid() + h = self.make_header('In-Reply-To', msgid) + self.assertEqual(h.message_ids, (msgid,)) + + def test_message_ids_id_right_forms(self): + h = self.make_header('References', + ' ') + self.assertEqual(h.message_ids, + ('', '')) + + def test_message_ids_comma_separated(self): + h = self.make_header('References', ',') + self.assertEqual(h.message_ids, ('', '')) + self.assertDefectsEqual(h.defects, [errors.InvalidHeaderDefect]) + + def test_message_ids_skip_invalid(self): + h = self.make_header('References', ' not-a-msg-id') + self.assertEqual(h.message_ids, ('',)) + self.assertDefectsEqual(h.defects, [errors.InvalidHeaderDefect]) + + def test_message_ids_from_parsed_message(self): + msg = message_from_string(textwrap.dedent('''\ + In-Reply-To: + References: + + + body + '''), policy=policy.default) + [in_reply_to] = msg['In-Reply-To'].message_ids + *_, last_reference = msg['References'].message_ids + self.assertEqual(in_reply_to, last_reference) + self.assertEqual(msg['References'].message_ids, + ('', '')) + + def test_message_ids_read_only(self): + h = self.make_header('References', '') + with self.assertRaises(AttributeError): + h.message_ids = () + + class TestFolding(TestHeaderBase): def test_address_display_names(self): diff --git a/Misc/NEWS.d/next/Library/2026-11-28-09-17-35.gh-issue-148797.HYkxDi.rst b/Misc/NEWS.d/next/Library/2026-11-28-09-17-35.gh-issue-148797.HYkxDi.rst new file mode 100644 index 000000000000000..6e37065ffdf2525 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-28-09-17-35.gh-issue-148797.HYkxDi.rst @@ -0,0 +1,3 @@ +Add :attr:`email.headerregistry.ReferencesHeader.message_ids`, a tuple of the +message identifiers in an :mailheader:`In-Reply-To` or :mailheader:`References` +header.