Skip to content

Commit

Permalink
🚨 make pyright happy
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyongyu authored Mar 21, 2024
1 parent 784eab9 commit 25272cb
Show file tree
Hide file tree
Showing 4,783 changed files with 5,549 additions and 12,241 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
6 changes: 2 additions & 4 deletions githubkit/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@
if TYPE_CHECKING:

class ModelBeforeValidator(Protocol):
def __call__(self, cls: Any, __value: Any) -> Any:
...
def __call__(self, cls: Any, values: Any) -> Any: ...

class CustomValidationClass(Protocol):
@classmethod
def __get_validators__(cls) -> Generator[Callable[..., Any], None, None]:
...
def __get_validators__(cls) -> Generator[Callable[..., Any], None, None]: ...

CVC = TypeVar("CVC", bound=CustomValidationClass)

Expand Down
124 changes: 103 additions & 21 deletions githubkit/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Type,
Union,
Generic,
Literal,
TypeVar,
Optional,
Generator,
Expand All @@ -22,6 +23,7 @@
import httpx
import hishel

from .utils import UNSET
from .response import Response
from .compat import to_jsonable_python
from .config import Config, get_config
Expand Down Expand Up @@ -56,8 +58,7 @@ def __init__(
auth: None = None,
*,
config: Config,
):
...
): ...

# token auth with config
@overload
Expand All @@ -66,8 +67,7 @@ def __init__(
auth: str,
*,
config: Config,
):
...
): ...

# other auth strategies with config
@overload
Expand All @@ -76,8 +76,7 @@ def __init__(
auth: A,
*,
config: Config,
):
...
): ...

# none auth without config
@overload
Expand All @@ -93,8 +92,7 @@ def __init__(
timeout: Optional[Union[float, httpx.Timeout]] = None,
http_cache: bool = True,
auto_retry: Union[bool, RetryDecisionFunc] = True,
):
...
): ...

# token auth without config
@overload
Expand All @@ -110,8 +108,7 @@ def __init__(
timeout: Optional[Union[float, httpx.Timeout]] = None,
http_cache: bool = True,
auto_retry: Union[bool, RetryDecisionFunc] = True,
):
...
): ...

# other auth strategies without config
@overload
Expand All @@ -127,8 +124,7 @@ def __init__(
timeout: Optional[Union[float, httpx.Timeout]] = None,
http_cache: bool = True,
auto_retry: Union[bool, RetryDecisionFunc] = True,
):
...
): ...

def __init__(
self,
Expand Down Expand Up @@ -323,25 +319,43 @@ async def _arequest(
raise RequestError(repr(e)) from e

# check and parse response
@overload
def _check(
self,
response: httpx.Response,
response_model: Type[T],
error_models: Optional[Dict[str, type]] = None,
) -> Response[T]: ...

@overload
def _check(
self,
response: httpx.Response,
response_model: Literal[UNSET] = UNSET,
error_models: Optional[Dict[str, type]] = None,
) -> Response[Any]: ...

def _check(
self,
response: httpx.Response,
response_model: Type[T] = Any,
response_model: Union[Type[T], Literal[UNSET]] = UNSET,
error_models: Optional[Dict[str, type]] = None,
) -> Response[T]:
) -> Union[Response[T], Response[Any]]:
if response.is_error:
error_models = error_models or {}
status_code = str(response.status_code)

error_model = error_models.get(
status_code,
error_models.get(
f"{status_code[:-2]}XX", error_models.get("default", Any)
f"{status_code[:-2]}XX", error_models.get("default", UNSET)
),
)
resp = Response(response, error_model)
resp = Response(response, Any if error_model is UNSET else error_model)
else:
resp = Response(response, response_model)
resp = Response(
response, Any if response_model is UNSET else response_model
)

# only check rate limit when response is 403 or 429
if response.status_code in (403, 429):
Expand Down Expand Up @@ -401,6 +415,7 @@ def _extract_retry_after(self, response: Response) -> timedelta:
return timedelta(seconds=60)

# sync request and check
@overload
def request(
self,
method: str,
Expand All @@ -413,9 +428,42 @@ def request(
json: Optional[Any] = None,
headers: Optional[HeaderTypes] = None,
cookies: Optional[CookieTypes] = None,
response_model: Type[T] = Any,
response_model: Type[T],
error_models: Optional[Dict[str, type]] = None,
) -> Response[T]:
) -> Response[T]: ...

@overload
def request(
self,
method: str,
url: URLTypes,
*,
params: Optional[QueryParamTypes] = None,
content: Optional[ContentTypes] = None,
data: Optional[dict] = None,
files: Optional[RequestFiles] = None,
json: Optional[Any] = None,
headers: Optional[HeaderTypes] = None,
cookies: Optional[CookieTypes] = None,
response_model: Literal[UNSET] = UNSET,
error_models: Optional[Dict[str, type]] = None,
) -> Response[Any]: ...

def request(
self,
method: str,
url: URLTypes,
*,
params: Optional[QueryParamTypes] = None,
content: Optional[ContentTypes] = None,
data: Optional[dict] = None,
files: Optional[RequestFiles] = None,
json: Optional[Any] = None,
headers: Optional[HeaderTypes] = None,
cookies: Optional[CookieTypes] = None,
response_model: Union[Type[T], Literal[UNSET]] = UNSET,
error_models: Optional[Dict[str, type]] = None,
) -> Union[Response[T], Response[Any]]:
retry_count: int = 0
while True:
try:
Expand Down Expand Up @@ -444,6 +492,40 @@ def request(
retry_count += 1

# async request and check
@overload
async def arequest(
self,
method: str,
url: URLTypes,
*,
params: Optional[QueryParamTypes] = None,
content: Optional[ContentTypes] = None,
data: Optional[dict] = None,
files: Optional[RequestFiles] = None,
json: Optional[Any] = None,
headers: Optional[HeaderTypes] = None,
cookies: Optional[CookieTypes] = None,
response_model: Type[T],
error_models: Optional[Dict[str, type]] = None,
) -> Response[T]: ...

@overload
async def arequest(
self,
method: str,
url: URLTypes,
*,
params: Optional[QueryParamTypes] = None,
content: Optional[ContentTypes] = None,
data: Optional[dict] = None,
files: Optional[RequestFiles] = None,
json: Optional[Any] = None,
headers: Optional[HeaderTypes] = None,
cookies: Optional[CookieTypes] = None,
response_model: Literal[UNSET] = UNSET,
error_models: Optional[Dict[str, type]] = None,
) -> Response[Any]: ...

async def arequest(
self,
method: str,
Expand All @@ -456,9 +538,9 @@ async def arequest(
json: Optional[Any] = None,
headers: Optional[HeaderTypes] = None,
cookies: Optional[CookieTypes] = None,
response_model: Type[T] = Any,
response_model: Union[Type[T], Literal[UNSET]] = UNSET,
error_models: Optional[Dict[str, type]] = None,
) -> Response[T]:
) -> Union[Response[T], Response[Any]]:
retry_count: int = 0
while True:
try:
Expand Down
3 changes: 1 addition & 2 deletions githubkit/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
from .graphql import GraphQLResponse


class GitHubException(Exception):
...
class GitHubException(Exception): ...


class AuthCredentialError(GitHubException):
Expand Down
63 changes: 8 additions & 55 deletions githubkit/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ def __init__(
auth: None = None,
*,
config: Config,
):
...
): ...

# token auth with config
@overload
Expand All @@ -60,8 +59,7 @@ def __init__(
auth: str,
*,
config: Config,
):
...
): ...

# other auth strategies with config
@overload
Expand All @@ -70,8 +68,7 @@ def __init__(
auth: A,
*,
config: Config,
):
...
): ...

# none auth without config
@overload
Expand All @@ -87,8 +84,7 @@ def __init__(
timeout: Optional[Union[float, httpx.Timeout]] = None,
http_cache: bool = True,
auto_retry: Union[bool, RetryDecisionFunc] = True,
):
...
): ...

# token auth without config
@overload
Expand All @@ -104,8 +100,7 @@ def __init__(
timeout: Optional[Union[float, httpx.Timeout]] = None,
http_cache: bool = True,
auto_retry: Union[bool, RetryDecisionFunc] = True,
):
...
): ...

# other auth strategies without config
@overload
Expand All @@ -121,11 +116,9 @@ def __init__(
timeout: Optional[Union[float, httpx.Timeout]] = None,
http_cache: bool = True,
auto_retry: Union[bool, RetryDecisionFunc] = True,
):
...
): ...

def __init__(self, *args, **kwargs):
...
def __init__(self, *args, **kwargs): ...

# copy github instance with other auth
def with_auth(self, auth: A_o) -> "GitHub[A_o]":
Expand Down Expand Up @@ -163,44 +156,4 @@ async def async_graphql(
)

# rest pagination
@overload
@staticmethod
def paginate(
request: R[CP, List[RT]],
page: int = 1,
per_page: int = 100,
map_func: None = None,
*args: CP.args,
**kwargs: CP.kwargs,
) -> Paginator[RT]:
...

@overload
@staticmethod
def paginate(
request: R[CP, CT],
page: int = 1,
per_page: int = 100,
map_func: Callable[[Response[CT]], List[RT]] = ..., # type: ignore
*args: CP.args,
**kwargs: CP.kwargs,
) -> Paginator[RT]:
...

@staticmethod
def paginate(
request: R[CP, CT],
page: int = 1,
per_page: int = 100,
map_func: Optional[Callable[[Response[CT]], List[RT]]] = None,
*args: CP.args,
**kwargs: CP.kwargs,
) -> Paginator[RT]:
return Paginator(
request,
page,
per_page,
map_func,
*args,
**kwargs, # type: ignore
)
paginate = Paginator
7 changes: 5 additions & 2 deletions githubkit/lazy_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,13 @@ def create_module(self, spec: ModuleSpec) -> Optional[ModuleType]:
module.__lazy_vars_mapping__ = {}
return module

def exec_module(self, module: LazyModule) -> None:
def exec_module(self, module: ModuleType) -> None:
super().exec_module(module)

if getattr(module, "__lazy_vars_validated__", None) is None:
if (
isinstance(module, LazyModule)
and getattr(module, "__lazy_vars_validated__", None) is None
):
structure = getattr(module, "__lazy_vars__", None)
if isinstance(structure, dict) and all(
isinstance(key, str) and isinstance(value, (list, tuple, set))
Expand Down
Loading

0 comments on commit 25272cb

Please sign in to comment.