BUG: Python's Message.header fails for empty headers

Subject: BUG: Python's Message.header fails for empty headers

Date: Sun, 7 Jan 2024 23:49:17 +0100

To: notmuch@notmuchmail.org


From: Vojtěch Káně

Dear notmuch developers,
I'll start with a short description of how I encountered the bug, so my 
report is more understandable and also so that you can potentially 
direct me to other places where the bug actually resides (I am a bit 
skeptic I would find a bug in such a popular open source project as 
notmuch is).

I use Lieer https://github.com/gauteh/lieer to sync my maildir and nm 
database with Gmail. When I send a reply, Lieer checks whether the 
subject matches the original one and prints a warning otherwise (Google 
presumably knows better what a thread is and breaks it if the subject is 
different). The relevant snippet is:

>if 'In-Reply-To' in eml:
>  repl = eml['In-Reply-To'].strip().strip('<>')
>  self.vprint("looking for original message: %s" % repl)
>  with notmuch2.Database(mode = notmuch2.Database.MODE.READ_ONLY) as db:
>    try:
>      nmsg = db.find(repl)
>    except LookupError:
>      nmsg = None
>    if nmsg is not None:
>      (_, gids) = self.local.messages_to_gids([nmsg])
>      if nmsg.header('Subject') != eml['Subject']:
>        self.vprint ("warning: subject does not match, might not be able to associate with existing thread.")

But there is a catch: when the original message's subject is empty, the 
check fails with a LookupError. Stack trace follows. I use NixOS, so 
while the file paths are weird, they also guarantee reproducible 
package set.

>Traceback (most recent call last):
>  File "/nix/store/9l521wrzalkhqk46qn57z0wc5lnmdzwk-lieer-1.4/bin/.gmi-wrapped", line 25, in <module>
>    g.main ()
>  File "/nix/store/9l521wrzalkhqk46qn57z0wc5lnmdzwk-lieer-1.4/lib/python3.10/site-packages/lieer/gmailieer.py", line 230, in main
>    args.func (args)
>  File "/nix/store/9l521wrzalkhqk46qn57z0wc5lnmdzwk-lieer-1.4/lib/python3.10/site-packages/lieer/gmailieer.py", line 837, in send
>    if nmsg.header('Subject') != eml['Subject']:
>  File "/nix/store/y12m0r41lcwz1ksqs6gncra0djln92cj-python3.10-notmuch2-0.37/lib/python3.10/site-packages/notmuch2/_message.py", line 266, in header
>    raise LookupError

At first, this sounds reasonable: the subject is empty, so it is 
effectively missing. That would indicate a bug in Lieer itself and would 
be fixed by a try-catch block. Notmuch's source for Message.header, 
however, states:

>:returns: The header value, an empty string if the header is not present.
>:rtype: str

This makes an impression that no error should be raised and a harmless 
value (at least for the above-mentioned code) should be returned. Yet 
the docs continue with

>:raises LookupError: if the header is not present.

completely contradicting itself.

And so here the questions:
Is my confusion justified? What is the expected nm's behavior? Can we 
fix the docs and possible the implementation?

Thank you all for reading this long report and for your dedication to 
open source,
Vojta Káně

p.s. I am not subscribed to the mailing list, so please keep me in the 
notmuch mailing list -- notmuch@notmuchmail.org
To unsubscribe send an email to notmuch-leave@notmuchmail.org