notion-sdk-py icon indicating copy to clipboard operation
notion-sdk-py copied to clipboard

Response envelope type hints

Open willshang76 opened this issue 1 year ago • 1 comments

When I use the sync client like below

from notion_client import Client

self.notion_token = notion_token or os.environ.get("NOTION_TOKEN")

self.notion_client = Client(auth=self.notion_token)

response = self.notion_client.search(
    filter={"property": "object", "value": "page"},
    start_cursor=cursor,
)
all_pages_info.extend(response["results"])

The mypy check complains 'Value of type "Any | Awaitable[Any]" is not indexable' for the line response = self.notion_client.search and all_pages_info.extend(response["results"]).

I wonder how to solve this. Thanks!

willshang76 avatar Oct 23 '24 06:10 willshang76

Hey, thanks for raising the issue and sorry for the long time to answer.

Your code has many other Mypy errors as is, but it can be replicated with this simplified one:

import os

from notion_client import Client

notion_token = os.environ.get("NOTION_TOKEN")
notion_client = Client(auth=notion_token)
response = notion_client.search()
print(response["results"])

Results in:

example.py:8: error: Value of type "Any | Awaitable[Any]" is not indexable  [index]
Found 1 error in 1 file (checked 1 source file)

So basically what's happening here is that we don't implement response type hints, and our current SyncAsync[Any] workaround you can see everywhere in api_endpoints.py makes Mypy unhappy because it's not precise enough to determine if response["results"] should be allowed or not (i.e. if it's indexable, i.e. if it's a dict).

Honestly it's an error I'm quite surprised to not have encountered yet, but it definitely should be fixed. Even without implementing / supporting all the response type hints, we should definitely replace our Any by something a bit more sensible, it's long overdue.

Some work has already been started / discussed in #199, #200 and #243. I'd be happy if we could land something, even if it just adds the overall message enveloppes and not the precise response types.

PR welcome!

ramnes avatar Dec 18 '24 10:12 ramnes