simplegmail
simplegmail copied to clipboard
Cannot negate boolean queries with False
I would like to construct a query with negations, such as finding messages which are NOT starred.
construct_query takes in a dictionary that supports various boolean labels, but setting them to False does not add the negation operator (-).
Actual:
>>> construct_query({"starred": True})
'is:starred'
>>> construct_query({"starred": False})
'is:starred'
>>> construct_query({
"starred": False,
"snoozed": False,
"unread": False,
"important": False,
"attachment": False,
"drive": False,
"docs": False,
"sheets": False,
"slides": False
})
'(is:starred is:snoozed is:unread is:important has:attachment has:drive has:document has:spreadsheet has:presentation)'
Expected:
>>> construct_query({"starred": True})
'is:starred'
>>> construct_query({"starred": False})
'-is:starred'
>>> construct_query({
"starred": False,
"snoozed": False,
"unread": False,
"important": False,
"attachment": False,
"drive": False,
"docs": False,
"sheets": False,
"slides": False
})
'(-is:starred -is:snoozed -is:unread -is:important -has:attachment -has:drive -has:document -has:spreadsheet -has:presentation)'
As a workaround for now, you can compose these queries manually, ie
construct_query({"exact_phrase": "-is:starred"})
This does add quotes to the query string ("-is:starred" vs -is:starred) but Gmail doesn't seem to mind.