-
-
Notifications
You must be signed in to change notification settings - Fork 141
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
Type Hint Responses [WIP] #200
Conversation
Hey @guacs! Thanks a lot for this first iteration. Quick thoughts:
Can't guarantee anything but I may try to work on a PoC as well. :) In any case, please feel free to modify the code directly, that would actually help me to understand your ideas by providing me a diff to read! |
Yeah I definitely agree that the code duplication would definitely be a burden. I'll create a PoC for just the On the point of the types, I'll try my best to keep it in sync with the one that is in the typescript client, but there might be some changes that will need to be made to accommodate for the differences in the type system. |
1c4d239
to
97f41ae
Compare
@ramnes I have the basic setup with just the user responses typed. The I ran the tests and all of them passed except for one test in helpers with the following error:
|
Also, regarding the types I had some doubts:
Also, please note that we will have to create some new types like |
Looks better, thanks! I'm still quite reluctant to duplicate the whole endpoints file to handle types. If I understand correctly, the duplication is only due to "the |
So let's assume the Mypy complains with the following: Pyright also complains with the following: |
Btw, mypy and pyright issues are for the following code: from notion_client.client import AsyncClient
async def main():
async_client = AsyncClient()
user = await async_client.users.retrieve("user-id") |
What about something like this? Mypy seems happy: class UsersEndpoint(Endpoint):
- def list(self, **kwargs: Any) -> SyncAsync[Any]:
+ @overload
+ async def list(self, **kwargs: Any) -> List[UserObjectResponse]:
+ ...
+
+ def list(self, **kwargs: Any) -> List[UserObjectResponse]:
"""Return a paginated list of [Users](https://developers.notion.com/reference/user) for the workspace.
*[🔗 Endpoint documentation](https://developers.notion.com/reference/get-users)* Or something like this, if we don't want to lie to type checkers: class UsersEndpoint(Endpoint):
- def list(self, **kwargs: Any) -> SyncAsync[Any]:
+ @overload
+ def list(self, **kwargs: Any) -> Awaitable[List[UserObjectResponse]]:
+ ...
+
+ def list(self, **kwargs: Any) -> List[UserObjectResponse]:
"""Return a paginated list of [Users](https://developers.notion.com/reference/user) for the workspace. |
Mypy is not complaining about the code the user would write, but it is raising an error for the implementation within the library though. Do you have just one
|
Yeah right, this is not the proper way to use After a bit more digging, here is what I ended up with: ramnes:1a8bdd4...ramnes:fe90378 With such a import asyncio
import os
from pprint import pprint
from typing import TYPE_CHECKING
from notion_client import AsyncClient, Client
def main_sync() -> None:
notion = Client(auth=os.environ["NOTION_TOKEN"])
users = notion.users.list()
if TYPE_CHECKING:
reveal_type(notion.users.parent)
reveal_type(users)
pprint(users)
async def main_async() -> None:
notion = AsyncClient(auth=os.environ["NOTION_TOKEN"])
users = await notion.users.list()
if TYPE_CHECKING:
reveal_type(notion.users.parent)
reveal_type(users)
pprint(users)
main_sync()
asyncio.run(main_async()) It correctly reveals the types:
Thoughts? Everything is in the |
This works perfectly! Yeah this is a far better approach. I think we can just go with this instead of the different implementations for sync and async. |
Now I think we just need to make a decision regarding these points, and we can progress with the changes. |
Cool, happy that you like it! Just a quick note: I think we should use Regarding the types, I'd prefer that we stay consistent with the TypeScript SDK names. Thanks! |
The PR is stale so I'll close it, but feel free to open a new PR if you feel like it! |
This is WIP PR for #199.
@ramnes, I just wanted to confirm whether this kind of an approach is fine or not. Also, I was thinking that instead of having to maintain a separate async and synchronous versions of the endpoints, we could use something like unasyncd.
Once this is confirmed, I'll go ahead and get to typing all the responses by comparing with the official docs and the official Javascript client.