Skip to content

Feature: support sequence and mapping input #176

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

Merged
merged 2 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions codegen/templates/rest/_param.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
{{ query_params(endpoint) }}
{{ header_params(endpoint) }}
{{ cookie_params(endpoint) }}
headers: Optional[dict[str, str]] = None,
headers: Optional[Mapping[str, str]] = None,
{{ endpoint.request_body.get_raw_definition() }}
{% endmacro %}

Expand All @@ -47,7 +47,7 @@ headers: Optional[dict[str, str]] = None,
{{ header_params(endpoint) }}
{{ cookie_params(endpoint) }}
data: UnsetType = UNSET,
headers: Optional[dict[str, str]] = None,
headers: Optional[Mapping[str, str]] = None,
{{ body_params(model, endpoint.param_names) }}
{% endmacro %}

Expand All @@ -57,7 +57,7 @@ headers: Optional[dict[str, str]] = None,
{{ query_params(endpoint) }}
{{ header_params(endpoint) }}
{{ cookie_params(endpoint) }}
headers: Optional[dict[str, str]] = None,
headers: Optional[Mapping[str, str]] = None,
{%- if endpoint.request_body %}
{{ endpoint.request_body.get_endpoint_definition() }},
{%- if endpoint.request_body.allowed_models %}
Expand Down
1 change: 1 addition & 0 deletions codegen/templates/rest/client.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from __future__ import annotations

from collections.abc import Mapping
from weakref import ref
from typing import TYPE_CHECKING, Literal, Optional, Annotated, overload

Expand Down
15 changes: 8 additions & 7 deletions codegen/templates/webhooks/_namespace.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import hmac
import json
import importlib
from collections.abc import Mapping
from typing_extensions import TypeAlias
from typing import TYPE_CHECKING, Any, Union, Literal, overload

Expand Down Expand Up @@ -81,7 +82,7 @@ class WebhookNamespace:
return type_validate_json(Event, payload)

@staticmethod
def parse_obj_without_name(payload: dict[str, Any]) -> "WebhookEvent":
def parse_obj_without_name(payload: Mapping[str, Any]) -> "WebhookEvent":
"""Parse the webhook payload dict without event name.

Note:
Expand All @@ -91,7 +92,7 @@ class WebhookNamespace:
the result may not be the correct type as expected.

Args:
payload (dict[str, Any]): webhook payload dict.
payload (Mapping[str, Any]): webhook payload dict.
"""

from ._types import WebhookEvent
Expand All @@ -101,27 +102,27 @@ class WebhookNamespace:
{% for name in event_names %}
@overload
@staticmethod
def parse_obj(name: Literal["{{ name }}"], payload: dict[str, Any]) -> "{{ pascal_case(name) }}Event":
def parse_obj(name: Literal["{{ name }}"], payload: Mapping[str, Any]) -> "{{ pascal_case(name) }}Event":
...
{% endfor %}

@overload
@staticmethod
def parse_obj(name: EventNameType, payload: dict[str, Any]) -> "WebhookEvent":
def parse_obj(name: EventNameType, payload: Mapping[str, Any]) -> "WebhookEvent":
...

@overload
@staticmethod
def parse_obj(name: str, payload: dict[str, Any]) -> "WebhookEvent":
def parse_obj(name: str, payload: Mapping[str, Any]) -> "WebhookEvent":
...

@staticmethod
def parse_obj(name: Union[EventNameType, str], payload: dict[str, Any]) -> "WebhookEvent":
def parse_obj(name: Union[EventNameType, str], payload: Mapping[str, Any]) -> "WebhookEvent":
"""Parse the webhook payload dict with event name.

Args:
name (EventNameType): event name.
payload (dict[str, Any]): webhook payload dict.
payload (Mapping[str, Any]): webhook payload dict.
"""

if name not in VALID_EVENT_NAMES:
Expand Down
14 changes: 7 additions & 7 deletions githubkit/auth/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from collections.abc import AsyncGenerator, Generator
from collections.abc import AsyncGenerator, Generator, Sequence
from dataclasses import dataclass
from datetime import datetime, timedelta, timezone
from typing import TYPE_CHECKING, ClassVar, Optional, Union
Expand Down Expand Up @@ -35,8 +35,8 @@ class AppAuth(httpx.Auth):
client_id: Optional[str] = None
client_secret: Optional[str] = None
installation_id: Union[Unset, int] = UNSET
repositories: Union[Unset, list[str]] = UNSET
repository_ids: Union[Unset, list[int]] = UNSET
repositories: Union[Unset, Sequence[str]] = UNSET
repository_ids: Union[Unset, Sequence[int]] = UNSET
permissions: Union[Unset, "AppPermissionsType"] = UNSET

JWT_CACHE_KEY: ClassVar[LiteralString] = "githubkit:auth:app:{issuer}:jwt"
Expand Down Expand Up @@ -277,8 +277,8 @@ def __post_init__(self):
def as_installation(
self,
installation_id: int,
repositories: Union[Unset, list[str]] = UNSET,
repository_ids: Union[Unset, list[int]] = UNSET,
repositories: Union[Unset, Sequence[str]] = UNSET,
repository_ids: Union[Unset, Sequence[int]] = UNSET,
permissions: Union[Unset, "AppPermissionsType"] = UNSET,
) -> "AppInstallationAuthStrategy":
return AppInstallationAuthStrategy(
Expand Down Expand Up @@ -318,8 +318,8 @@ class AppInstallationAuthStrategy(BaseAuthStrategy):
installation_id: int
client_id: Optional[str] = None
client_secret: Optional[str] = None
repositories: Union[Unset, list[str]] = UNSET
repository_ids: Union[Unset, list[int]] = UNSET
repositories: Union[Unset, Sequence[str]] = UNSET
repository_ids: Union[Unset, Sequence[int]] = UNSET
permissions: Union[Unset, "AppPermissionsType"] = UNSET

def __post_init__(self):
Expand Down
9 changes: 6 additions & 3 deletions githubkit/auth/oauth.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from collections.abc import AsyncGenerator, Coroutine, Generator
from collections.abc import AsyncGenerator, Coroutine, Generator, Sequence
from dataclasses import dataclass, field
from datetime import datetime, timedelta, timezone
from time import sleep
Expand Down Expand Up @@ -29,7 +29,7 @@


def create_device_code(
github: "GitHubCore", client_id: str, scopes: Optional[list[str]] = None
github: "GitHubCore", client_id: str, scopes: Optional[Sequence[str]] = None
) -> Generator[httpx.Request, httpx.Response, dict[str, Any]]:
"""Create a device code for OAuth."""
base_url = get_oauth_base_url(github.config.base_url)
Expand Down Expand Up @@ -567,7 +567,10 @@ def as_web_user(
self, code: str, redirect_uri: Optional[str] = None
) -> "OAuthWebAuthStrategy":
return OAuthWebAuthStrategy(
self.client_id, self.client_secret, code, redirect_uri
client_id=self.client_id,
client_secret=self.client_secret,
code=code,
redirect_uri=redirect_uri,
)

def get_auth_flow(self, github: "GitHubCore") -> httpx.Auth:
Expand Down
5 changes: 3 additions & 2 deletions githubkit/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections.abc import Sequence
from dataclasses import dataclass, fields
from typing import Any, Optional, Union
from typing_extensions import Self
Expand Down Expand Up @@ -42,7 +43,7 @@ def build_base_url(base_url: Optional[Union[str, httpx.URL]]) -> httpx.URL:


def build_accept(
accept_format: Optional[str] = None, previews: Optional[list[str]] = None
accept_format: Optional[str] = None, previews: Optional[Sequence[str]] = None
) -> str:
if accept_format:
accept_format = (
Expand Down Expand Up @@ -99,7 +100,7 @@ def get_config(
*,
base_url: Optional[Union[str, httpx.URL]] = None,
accept_format: Optional[str] = None,
previews: Optional[list[str]] = None,
previews: Optional[Sequence[str]] = None,
user_agent: Optional[str] = None,
follow_redirects: bool = True,
timeout: Optional[Union[float, httpx.Timeout]] = None,
Expand Down
28 changes: 14 additions & 14 deletions githubkit/core.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from collections.abc import AsyncGenerator, Generator
from collections.abc import AsyncGenerator, Generator, Mapping, Sequence
from contextlib import asynccontextmanager, contextmanager
from contextvars import ContextVar
from datetime import datetime, timedelta, timezone
Expand Down Expand Up @@ -77,7 +77,7 @@ def __init__(
*,
base_url: Optional[Union[str, httpx.URL]] = None,
accept_format: Optional[str] = None,
previews: Optional[list[str]] = None,
previews: Optional[Sequence[str]] = None,
user_agent: Optional[str] = None,
follow_redirects: bool = True,
timeout: Optional[Union[float, httpx.Timeout]] = None,
Expand All @@ -96,7 +96,7 @@ def __init__(
*,
base_url: Optional[Union[str, httpx.URL]] = None,
accept_format: Optional[str] = None,
previews: Optional[list[str]] = None,
previews: Optional[Sequence[str]] = None,
user_agent: Optional[str] = None,
follow_redirects: bool = True,
timeout: Optional[Union[float, httpx.Timeout]] = None,
Expand All @@ -115,7 +115,7 @@ def __init__(
*,
base_url: Optional[Union[str, httpx.URL]] = None,
accept_format: Optional[str] = None,
previews: Optional[list[str]] = None,
previews: Optional[Sequence[str]] = None,
user_agent: Optional[str] = None,
follow_redirects: bool = True,
timeout: Optional[Union[float, httpx.Timeout]] = None,
Expand All @@ -133,7 +133,7 @@ def __init__(
config: Optional[Config] = None,
base_url: Optional[Union[str, httpx.URL]] = None,
accept_format: Optional[str] = None,
previews: Optional[list[str]] = None,
previews: Optional[Sequence[str]] = None,
user_agent: Optional[str] = None,
follow_redirects: bool = True,
timeout: Optional[Union[float, httpx.Timeout]] = None,
Expand Down Expand Up @@ -338,22 +338,22 @@ def _check(
self,
response: httpx.Response,
response_model: type[T],
error_models: Optional[dict[str, type]] = None,
error_models: Optional[Mapping[str, type]] = None,
) -> Response[T]: ...

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

def _check(
self,
response: httpx.Response,
response_model: Union[type[T], UnsetType] = UNSET,
error_models: Optional[dict[str, type]] = None,
error_models: Optional[Mapping[str, type]] = None,
) -> Union[Response[T], Response[Any]]:
if response.is_error:
error_models = error_models or {}
Expand Down Expand Up @@ -443,7 +443,7 @@ def request(
headers: Optional[HeaderTypes] = None,
cookies: Optional[CookieTypes] = None,
response_model: type[T],
error_models: Optional[dict[str, type]] = None,
error_models: Optional[Mapping[str, type]] = None,
) -> Response[T]: ...

@overload
Expand All @@ -460,7 +460,7 @@ def request(
headers: Optional[HeaderTypes] = None,
cookies: Optional[CookieTypes] = None,
response_model: UnsetType = UNSET,
error_models: Optional[dict[str, type]] = None,
error_models: Optional[Mapping[str, type]] = None,
) -> Response[Any]: ...

def request(
Expand All @@ -476,7 +476,7 @@ def request(
headers: Optional[HeaderTypes] = None,
cookies: Optional[CookieTypes] = None,
response_model: Union[type[T], UnsetType] = UNSET,
error_models: Optional[dict[str, type]] = None,
error_models: Optional[Mapping[str, type]] = None,
) -> Union[Response[T], Response[Any]]:
"""Send a request.

Expand Down Expand Up @@ -525,7 +525,7 @@ async def arequest(
headers: Optional[HeaderTypes] = None,
cookies: Optional[CookieTypes] = None,
response_model: type[T],
error_models: Optional[dict[str, type]] = None,
error_models: Optional[Mapping[str, type]] = None,
) -> Response[T]: ...

@overload
Expand All @@ -542,7 +542,7 @@ async def arequest(
headers: Optional[HeaderTypes] = None,
cookies: Optional[CookieTypes] = None,
response_model: UnsetType = UNSET,
error_models: Optional[dict[str, type]] = None,
error_models: Optional[Mapping[str, type]] = None,
) -> Response[Any]: ...

async def arequest(
Expand All @@ -558,7 +558,7 @@ async def arequest(
headers: Optional[HeaderTypes] = None,
cookies: Optional[CookieTypes] = None,
response_model: Union[type[T], UnsetType] = UNSET,
error_models: Optional[dict[str, type]] = None,
error_models: Optional[Mapping[str, type]] = None,
) -> Union[Response[T], Response[Any]]:
"""Asynchronously send a request.

Expand Down
8 changes: 4 additions & 4 deletions githubkit/github.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from collections.abc import Awaitable
from collections.abc import Awaitable, Sequence
from functools import cached_property
from typing import TYPE_CHECKING, Any, Callable, Optional, TypeVar, Union, overload
from typing_extensions import ParamSpec
Expand Down Expand Up @@ -70,7 +70,7 @@ def __init__(
*,
base_url: Optional[Union[str, httpx.URL]] = None,
accept_format: Optional[str] = None,
previews: Optional[list[str]] = None,
previews: Optional[Sequence[str]] = None,
user_agent: Optional[str] = None,
follow_redirects: bool = True,
timeout: Optional[Union[float, httpx.Timeout]] = None,
Expand All @@ -89,7 +89,7 @@ def __init__(
*,
base_url: Optional[Union[str, httpx.URL]] = None,
accept_format: Optional[str] = None,
previews: Optional[list[str]] = None,
previews: Optional[Sequence[str]] = None,
user_agent: Optional[str] = None,
follow_redirects: bool = True,
timeout: Optional[Union[float, httpx.Timeout]] = None,
Expand All @@ -108,7 +108,7 @@ def __init__(
*,
base_url: Optional[Union[str, httpx.URL]] = None,
accept_format: Optional[str] = None,
previews: Optional[list[str]] = None,
previews: Optional[Sequence[str]] = None,
user_agent: Optional[str] = None,
follow_redirects: bool = True,
timeout: Optional[Union[float, httpx.Timeout]] = None,
Expand Down
5 changes: 3 additions & 2 deletions githubkit/graphql/paginator.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections.abc import Mapping
from typing import TYPE_CHECKING, Any, Optional, TypedDict
from typing_extensions import Self
from weakref import ref
Expand Down Expand Up @@ -30,13 +31,13 @@ def __init__(
self,
graphql: "GraphQLNamespace",
query: str,
variables: Optional[dict[str, Any]] = None,
variables: Optional[Mapping[str, Any]] = None,
) -> None:
self._graphql_ref = ref(graphql)
self.query = query

self._has_next_page: bool = True
self._current_variables = variables.copy() if variables is not None else {}
self._current_variables = dict(variables) if variables is not None else {}

@property
def _graphql(self) -> "GraphQLNamespace":
Expand Down
Loading
Loading