Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Response envelope type hints #242

Open
willshang76 opened this issue Oct 23, 2024 · 1 comment
Open

Response envelope type hints #242

willshang76 opened this issue Oct 23, 2024 · 1 comment
Labels
help wanted Extra attention is needed task Something that has to be done at some point

Comments

@willshang76
Copy link

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!

@ramnes
Copy link
Owner

ramnes commented Dec 18, 2024

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 #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 ramnes changed the title mypy check complains Value of type "Any | Awaitable[Any]" is not indexable Response envelope type hints Dec 18, 2024
@ramnes ramnes added help wanted Extra attention is needed task Something that has to be done at some point labels Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed task Something that has to be done at some point
Projects
None yet
Development

No branches or pull requests

2 participants