-
-
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
[Draft] Add types (POC) #243
Draft
superleesa
wants to merge
5
commits into
ramnes:main
Choose a base branch
from
superleesa:add-types
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
664bd5d
fix: `abstractclassmethod` -> `abstractmethod`
superleesa d1626f7
Add `ClientType` and `ResponseType`
superleesa ee3e34d
use generic in BaseClient and cast request output to `ResponseType`
superleesa 74b3a60
change bound of `ResponseType` to `Mapping` from `Dict` (`TypedDict` …
superleesa bd566ac
use `Mapping` for `pick` too
superleesa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
"""Synchronous and asynchronous clients for Notion's API.""" | ||
import json | ||
import logging | ||
from abc import abstractclassmethod | ||
from abc import abstractmethod | ||
from dataclasses import dataclass | ||
from types import TracebackType | ||
from typing import Any, Dict, List, Optional, Type, Union | ||
from typing import Any, Dict, List, Generic, Optional, Type, Union | ||
|
||
import httpx | ||
from httpx import Request, Response | ||
|
@@ -24,7 +24,7 @@ | |
is_api_error_code, | ||
) | ||
from notion_client.logging import make_console_logger | ||
from notion_client.typing import SyncAsync | ||
from notion_client.typing import ClientType, ResponseType, SyncAsync | ||
|
||
|
||
@dataclass | ||
|
@@ -52,7 +52,7 @@ class ClientOptions: | |
notion_version: str = "2022-06-28" | ||
|
||
|
||
class BaseClient: | ||
class BaseClient(Generic[ClientType]): | ||
def __init__( | ||
self, | ||
client: Union[httpx.Client, httpx.AsyncClient], | ||
|
@@ -71,12 +71,12 @@ def __init__( | |
self._clients: List[Union[httpx.Client, httpx.AsyncClient]] = [] | ||
self.client = client | ||
|
||
self.blocks = BlocksEndpoint(self) | ||
self.databases = DatabasesEndpoint(self) | ||
self.users = UsersEndpoint(self) | ||
self.pages = PagesEndpoint(self) | ||
self.search = SearchEndpoint(self) | ||
self.comments = CommentsEndpoint(self) | ||
self.blocks = BlocksEndpoint[ClientType](self) | ||
self.databases = DatabasesEndpoint[ClientType](self) | ||
self.users = UsersEndpoint[ClientType](self) | ||
self.pages = PagesEndpoint[ClientType](self) | ||
self.search = SearchEndpoint[ClientType](self) | ||
self.comments = CommentsEndpoint[ClientType](self) | ||
|
||
@property | ||
def client(self) -> Union[httpx.Client, httpx.AsyncClient]: | ||
|
@@ -131,15 +131,16 @@ def _parse_response(self, response: Response) -> Any: | |
|
||
return body | ||
|
||
@abstractclassmethod | ||
@abstractmethod | ||
def request( | ||
self, | ||
path: str, | ||
method: str, | ||
cast_to: Type[ResponseType], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we avoid to add this new |
||
query: Optional[Dict[Any, Any]] = None, | ||
body: Optional[Dict[Any, Any]] = None, | ||
auth: Optional[str] = None, | ||
) -> SyncAsync[Any]: | ||
) -> SyncAsync[ResponseType]: | ||
# noqa | ||
pass | ||
|
||
|
@@ -181,17 +182,18 @@ def request( | |
self, | ||
path: str, | ||
method: str, | ||
cast_to: Type[ResponseType], | ||
query: Optional[Dict[Any, Any]] = None, | ||
body: Optional[Dict[Any, Any]] = None, | ||
auth: Optional[str] = None, | ||
) -> Any: | ||
) -> ResponseType: | ||
"""Send an HTTP request.""" | ||
request = self._build_request(method, path, query, body, auth) | ||
try: | ||
response = self.client.send(request) | ||
except httpx.TimeoutException: | ||
raise RequestTimeoutError() | ||
return self._parse_response(response) | ||
return cast_to(self._parse_response(response)) | ||
|
||
|
||
class AsyncClient(BaseClient): | ||
|
@@ -231,14 +233,15 @@ async def request( | |
self, | ||
path: str, | ||
method: str, | ||
cast_to: Type[ResponseType], | ||
query: Optional[Dict[Any, Any]] = None, | ||
body: Optional[Dict[Any, Any]] = None, | ||
auth: Optional[str] = None, | ||
) -> Any: | ||
) -> ResponseType: | ||
"""Send an HTTP request asynchronously.""" | ||
request = self._build_request(method, path, query, body, auth) | ||
try: | ||
response = await self.client.send(request) | ||
except httpx.TimeoutException: | ||
raise RequestTimeoutError() | ||
return self._parse_response(response) | ||
return cast_to(self._parse_response(response)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
"""Custom type definitions for notion-sdk-py.""" | ||
from typing import Awaitable, TypeVar, Union | ||
from typing import TYPE_CHECKING, Any, Awaitable, Mapping, TypeVar, Union | ||
|
||
if TYPE_CHECKING: # pragma: no cover | ||
from notion_client.client import BaseClient | ||
|
||
T = TypeVar("T") | ||
SyncAsync = Union[T, Awaitable[T]] | ||
|
||
ClientType = TypeVar("ClientType", bound=BaseClient) | ||
ResponseType = TypeVar("ResponseType", bound=Mapping[Any, Any]) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch