This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: smtplib send_message should gives more clear exception if the msg is None
Type: enhancement Stage: test needed
Components: Library (Lib) Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: barry, maxking, oon, r.david.murray, terry.reedy
Priority: normal Keywords:

Created on 2021-01-02 04:23 by oon, last changed 2022-04-11 14:59 by admin.

Messages (2)
msg384216 - (view) Author: oon arfiandwi (oon) * Date: 2021-01-02 04:23
I want to enhance the error message on the send_message method of class SMTP, in module smtplib.

One of the parameters on send_message is msg which is email.message.Message object, this is a required argument. If the msg is None, currently it will raise "AttributeError: 'NoneType' object has no attribute 'get_all'".
This message is not straight forward and a bit hard to debug.

As the easy fix with additional check if msg is None in send_message method.

I want to add PR on this, also with the additional test(s). This is my first issue, appreciate any feedback. thank you.
msg384692 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-01-08 21:34
I am not familiar with either smtp or email, but I can make some general comments.

1. "AttributeError: 'NoneType' object has no attribute 'get_all'" seems pretty straightforward to me.  Hard to debug?  Depends on knowledge and experience. When coming from library code, it nearly always means that one called a library function with an invalid argument.  None, with only standard dunder attributes, is perhaps the most common bad argument.  Look in the traceback to find the bad call in one's own code, where one must have passed None, even if not by that name.  In this case, if one does not know that 'get_all' is a method of the two email message classes, this can be discovered in the document index.

2. It is not a bug for a function to expect users to know the above and in effect say "Don't do that!" when an attribute reference fails.  So I see this as an enhancement request, not a bugfix report.  But why add a check for None to this particular function?

3. Python functions often use duck-typing, allowing arguments that act sufficiently well like the documented requirement.  While https://docs.python.org/3/library/smtplib.html#smtplib.SMTP.send_message  says that msg should be a Message object, this may not be an exact requirement.  Indeed, https://docs.python.org/3/library/smtplib.html#smtplib.SMTP.send_message starts with "The Message class is very similar to the EmailMessage class ...".  So I wonder, "Is EmailMessage is also allowed?"  Or, "Do  any users pass instances of custom message classes?"

If so, perhaps the doc should be augmented.  But checking only for Message would be wrong, and would break code passing non-Message instances.
History
Date User Action Args
2022-04-11 14:59:39adminsetgithub: 86973
2021-01-08 21:34:29terry.reedysetnosy: + barry, r.david.murray, terry.reedy, maxking
messages: + msg384692

type: behavior -> enhancement
stage: test needed
2021-01-02 04:23:16ooncreate