Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-68525: Implement email.message.Message.__repr__() #18127

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions Lib/email/message.py
Expand Up @@ -164,6 +164,10 @@ def __str__(self):
"""
return self.as_string()

def __repr__(self):
return f"{self.__class__.__name__} with {len(self._headers)} " \
f"headers and Content-Type {self._default_type}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't match the suggestion in the issue. self._default_type is also wrong, since that's just the default, not the actual content-type of the message (which comes from the headers).

What exactly goes in the repr should be discussed further in the issue.


def as_string(self, unixfrom=False, maxheaderlen=0, policy=None):
"""Return the entire formatted message as a string.

Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_email/test_email.py
Expand Up @@ -306,6 +306,13 @@ def test_as_string(self):
self.assertTrue(lines[0].startswith('From '))
self.assertEqual(text, NL.join(lines[1:]))

def test_repr(self):
msg = self._msgobj('msg_01.txt')
self.assertIn('Content-Type text/plain', repr(msg))
self.assertIn('Message', repr(msg))
self.assertEqual(repr(Message()),
'Message with 0 headers and Content-Type text/plain')

def test_as_string_policy(self):
msg = self._msgobj('msg_01.txt')
newpolicy = msg.policy.clone(linesep='\r\n')
Expand Down
@@ -0,0 +1 @@
Implement ``__repr__()`` for :class:`email.message.Message`.