jeremyephron / simplegmail

A simple Gmail API client for applications in Python
MIT License
339 stars 73 forks source link

Cannot negate boolean queries with False #61

Open MooseV2 opened 2 years ago

MooseV2 commented 2 years ago

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)'
MooseV2 commented 2 years ago

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.