Gauging interest in my IMAP4 IDLE implementation for imaplib

Hi, folks.

Anyone who has used python to check an email account is probably aware of imaplib, its useful-but-basic support for the protocol, and its unfortunate lack of certain features that are widely expected and implemented in IMAP servers today.

One such feature is the IDLE command (defined in rfc2177 and rfc9051), which would allow the server to push events to a client as they occur, avoiding the need to continually poll for mailbox changes. I think it’s fair to say this would make the module significantly more useful, as it comes up every so often in the python mailing lists and bug tracker.

As can be seen in issue 55454, several people have suggested solutions for this in the past, either by submitting small patches or by proposing the total replacement of imaplib. I recently found myself needing IMAP IDLE, so I spent some time looking over the patches in that bug report, and exploring the three alternative IMAP libraries that were proposed in this python-ideas thread.

IMHO, none of the those proposed solutions is appropriate for the standard library, since they all suffer from one or more of these problems:

  • Leaving edge cases unhandled.
  • Imposing new runtime complexities (notably multithreading) on programs and libraries that already use imaplib.
  • Using standard library functions in ways that python’s documentation warns against.
  • Presenting an error-prone or awkward API to the programmer.

Since I wasn’t satisfied with what was available, I wrote my own. Here are some notable differences from the other approaches I’ve seen:

  • It’s an extension to imaplib, rather than a replacement.
  • It doesn’t introduce additional threads.
  • It doesn’t impose new requirements on the use of imaplib’s existing methods.
  • It passes the unit tests in CPython’s test/test_imaplib.py module.
  • It works on Windows, Linux, and other unix-like systems.
  • It makes IDLE available on all of imaplib’s client variants (including IMAP4_stream).
  • The interface is pythonic and easy to use.

To get an idea of its capabilities and how it looks to use it, please see the examples I wrote up in this comment on the issue tracker.

Two caveats:

  • Due to a Windows limitation, the special case of IMAP4_stream running on Windows lacks a duraton/timeout feature. (This is the stdin/stdout pipe connection variant; timeouts work fine for socket-based connections, even on Windows.) I have documented it where appropriate.
  • The file-like imaplib instance attributes are chagned from buffered to unbuffered mode. This could potentially break any client code that uses those objects directly without expecting partial reads/writes. However, these attributes are undocumented. As such, I think they are fair game for changes, and any code that uses them directly (circumventing the documented interface) has only itself to blame if it breaks. While this case could be supported through additional work, it would also add complexity to the library, which I feel is worth avoiding if possible.

I’m writing here to gauge interest in this work. I already use it daily, and I think it would be a helpful improvement to imaplib. If others agree, I could prepare a public repo so folks could experiment with it, and potentially submit it as a patch to the standard library.

If submitting it as a contribution, I would need some guidance from python maintainers, since I haven’t contributed code to Python before. (At least, not that I recall.)

Thanks for reading.

7 Likes

Hi @forestix - thanks for taking an interest in improving imaplib. There was a time years ago when I had a lot more time and interest in the module as well, but that’s no longer the case. Also, it’s pretty clear to me from a search of the issue tracker and git blame on the file that imaplib just doesn’t get much in the way of substantial support, and hasn’t for a very long time.

To be honest, I’m not quite sure why imaplib wasn’t included in PEP 594 and slated for removal from the stdlib. Maybe @brettcannon can chime in on that[1].

I don’t seem much hope for the fate of imaplib in the stdlib improving, and very likely the best way forward for supporting the IMAP protocol in Python is as a third party library, published through PyPI. Have you considered collaborating with the other three alternative implementations or publishing your own, possibly by vendoring or forking the stdlib module?

I’m sorry I can’t help more.


  1. I don’t think the other author of PEP 594, Christian Heimes is on DPO ↩︎

Christian created the list, I just pushed the PEP over the finish line. My guess is someone said, “please don’t”, and so Christian kept it (he was pretty conservative in the face of pushback).

I’m glad it wasn’t removed with PEP 594, as IMAP is still in wide use, and imaplib has been a great help in my current project.

Have you considered collaborating with the other three alternative implementations or publishing your own, possibly by vendoring or forking the stdlib module?

The value I get from imaplib comes in no small part from the fact that it’s built in to the standard library. That allows me to use it without adding external dependencies, and therefore without the risks that come with them (nor the additional complexity of distribution, though that is perhaps the lesser of the issues).

Python’s “Batteries Included” philosophy is important to me. My motivation for sharing this work is to contribute to that tradition, so that others can benefit from it as I have. Redirecting my efforts into a pypi package wouldn’t accomplish that, so I have no incentive to spend my time on it.

If this is unwelcome in the standard library, I expect I’ll just keep using it privately.

5 Likes

Cool. My initial thought still stands - realistically I doubt imaplib has much future for good support in the stdlib. Someone please correct me if I’m wrong! But if I’m not, then eventually it should get deprecated and removed.

FYI: For my IMAP project I could not use imaplib and now use IMAPClient · PyPI that worked for my use case.

I would hope I’m not the only one who sees an enormous and healthy gap between “this stdlib module probably won’t be improved much” and “this stdlib module will be removed”. :slight_smile: The modules removed under PEP 594 weren’t just lacking development or maintenance, they were mostly implementations of formats or protocols which themselves were obsolete, or at least vastly declining in use. IMAP as a protocol is still in wide use.

3 Likes

I think it would be great to get your IDLE implementation into the stdlib and also more generally, again have someone who could maintain the module going forward.

Would you be up to this, @forestix ?


Overall, I think we need start being more welcoming about stdlib community contributions again and also consider making it easier for non-core-devs to maintain such contributions (or make it easier to become a core dev for this purpose).

I’m not a fan of trying to move the stdlib out of core Python and into PyPI modules. The “batteries included” approach is one of the key parts of Python’s success story (together with the clean syntax and its great C API) and should upheld as much as reasonably possible.

There is a big difference in having to download and maintain a whole ballpark of packages from PyPI in order to get a script deployed vs. simply pointing Python at it and having it run: You really don’t want to have to design, install and maintain a separate venv installation for every single script you write in Python.

Add to this the complexity of having to maintain accurate supply chain records (think EU Cyber Resilience Act), and you will see even clearer how much advantage Python’s “batteries included” feature has over other competing languages.

When it comes to IMAP4, which is still the number one protocol for accessing emails, why would you want to go through all those hoops to get a Python script running ? If we drop IMAP support from the stdlib, we’d be making it much harder to write simple scripts which take input from mail boxes and require adding a lot of extra overhead for little gain to get to the same functionality.

18 Likes

Just chiming in as someone who has used IMAP IDLE in a Python project and would have loved to have that in the standard library. :slightly_smiling_face:

In general, my bias is from formerly working in air-gapped environments where “batteries included” was especially appreciated.

(That said, I understand that accepting changes implies an ongoing maintenance commitment and can appreciate that as well.)

I don’t think I can reasonably commit to taking on maintenance of imaplib.

(However, I would be happy to address any issues in my idle extension if it were accepted into the library, of course.)

Thanks, this sounds like a good start.

Could you create a PR on Github for this ? The discussion can then continue there.


As for general maintenance of stdlib modules which currently don’t have an active maintainer, I’d suggest to volunteer for this either here on the Ideas category or over in the Core Dev category.

There are definitely a number of stdlib modules which could use more eyes and help with resolving issues. Please have a look at these resources to get you started:

  • Experts list (not quite up to date, but provides a good overview when trying to find unmaintained modules)

  • Code owners file (this is used by Github to determine who to contact for PRs based on the files in the change set)

  • CPython issues tracker (to get an idea of which modules have lots of open issues); e.g. imaplib issues

Thanks.

Done.