Skip to content

Commit a3c57f7

Browse files
committed
Be more accepting of input types
- Most `dict` parameters can be any mapping type - Most `list` parameters can be any iterable, or if stored, any sequence
1 parent 8f44dc0 commit a3c57f7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+6953
-6831
lines changed

codegen/templates/rest/_param.py.jinja

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
{{ query_params(endpoint) }}
3737
{{ header_params(endpoint) }}
3838
{{ cookie_params(endpoint) }}
39-
headers: Optional[dict[str, str]] = None,
39+
headers: Optional[Mapping[str, str]] = None,
4040
{{ endpoint.request_body.get_raw_definition() }}
4141
{% endmacro %}
4242

@@ -47,7 +47,7 @@ headers: Optional[dict[str, str]] = None,
4747
{{ header_params(endpoint) }}
4848
{{ cookie_params(endpoint) }}
4949
data: UnsetType = UNSET,
50-
headers: Optional[dict[str, str]] = None,
50+
headers: Optional[Mapping[str, str]] = None,
5151
{{ body_params(model, endpoint.param_names) }}
5252
{% endmacro %}
5353

@@ -57,7 +57,7 @@ headers: Optional[dict[str, str]] = None,
5757
{{ query_params(endpoint) }}
5858
{{ header_params(endpoint) }}
5959
{{ cookie_params(endpoint) }}
60-
headers: Optional[dict[str, str]] = None,
60+
headers: Optional[Mapping[str, str]] = None,
6161
{%- if endpoint.request_body %}
6262
{{ endpoint.request_body.get_endpoint_definition() }},
6363
{%- if endpoint.request_body.allowed_models %}

codegen/templates/rest/client.py.jinja

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from __future__ import annotations
1010

11+
from collections.abc import Mapping
1112
from weakref import ref
1213
from typing import TYPE_CHECKING, Literal, Optional, Annotated, overload
1314

codegen/templates/webhooks/_namespace.py.jinja

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import hmac
66
import json
77
import importlib
8+
from collections.abc import Mapping
89
from typing_extensions import TypeAlias
910
from typing import TYPE_CHECKING, Any, Union, Literal, overload
1011

@@ -81,7 +82,7 @@ class WebhookNamespace:
8182
return type_validate_json(Event, payload)
8283

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

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

9394
Args:
94-
payload (dict[str, Any]): webhook payload dict.
95+
payload (Mapping[str, Any]): webhook payload dict.
9596
"""
9697

9798
from ._types import WebhookEvent
@@ -101,27 +102,27 @@ class WebhookNamespace:
101102
{% for name in event_names %}
102103
@overload
103104
@staticmethod
104-
def parse_obj(name: Literal["{{ name }}"], payload: dict[str, Any]) -> "{{ pascal_case(name) }}Event":
105+
def parse_obj(name: Literal["{{ name }}"], payload: Mapping[str, Any]) -> "{{ pascal_case(name) }}Event":
105106
...
106107
{% endfor %}
107108

108109
@overload
109110
@staticmethod
110-
def parse_obj(name: EventNameType, payload: dict[str, Any]) -> "WebhookEvent":
111+
def parse_obj(name: EventNameType, payload: Mapping[str, Any]) -> "WebhookEvent":
111112
...
112113

113114
@overload
114115
@staticmethod
115-
def parse_obj(name: str, payload: dict[str, Any]) -> "WebhookEvent":
116+
def parse_obj(name: str, payload: Mapping[str, Any]) -> "WebhookEvent":
116117
...
117118

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

122123
Args:
123124
name (EventNameType): event name.
124-
payload (dict[str, Any]): webhook payload dict.
125+
payload (Mapping[str, Any]): webhook payload dict.
125126
"""
126127

127128
if name not in VALID_EVENT_NAMES:
@@ -131,7 +132,7 @@ class WebhookNamespace:
131132
return type_validate_python(Event, payload)
132133

133134
@staticmethod
134-
def normalize_payload(payload: Union[GitHubModel, dict[str, Any]]) -> str:
135+
def normalize_payload(payload: Union[GitHubModel, Mapping[str, Any]]) -> str:
135136
"""Normalize the webhook payload to string.
136137

137138
Note:
@@ -140,7 +141,7 @@ class WebhookNamespace:
140141
Always use raw data posted by GitHub Webhooks.
141142

142143
Args:
143-
payload (Union[GitHubModel, dict[str, Any]]): webhook payload.
144+
payload (Union[GitHubModel, Mapping[str, Any]]): webhook payload.
144145

145146
Returns:
146147
str: normalized payload string.
@@ -151,14 +152,14 @@ class WebhookNamespace:
151152
@staticmethod
152153
def sign(
153154
secret: str,
154-
payload: Union[GitHubModel, dict[str, Any], str, bytes],
155+
payload: Union[GitHubModel, Mapping[str, Any], str, bytes],
155156
method: Literal["sha256", "sha1"] = "sha256",
156157
) -> str:
157158
"""Sign the webhook payload.
158159

159160
Args:
160161
secret (str): webhook secret.
161-
payload (Union[GitHubModel, dict[str, Any], str, bytes]): webhook payload.
162+
payload (Union[GitHubModel, Mapping[str, Any], str, bytes]): webhook payload.
162163
method (str): sha256 or sha1. Defaults to sha256.
163164

164165
Returns:
@@ -177,7 +178,7 @@ class WebhookNamespace:
177178
@staticmethod
178179
def verify(
179180
secret: str,
180-
payload: Union[GitHubModel, dict[str, Any], str, bytes],
181+
payload: Union[GitHubModel, Mapping[str, Any], str, bytes],
181182
signature: str,
182183
) -> bool:
183184
"""Verify the webhook payload.
@@ -189,7 +190,7 @@ class WebhookNamespace:
189190

190191
Args:
191192
secret (str): webhook secret.
192-
payload (Union[GitHubModel, dict[str, Any], str, bytes]): webhook payload.
193+
payload (Union[GitHubModel, Mapping[str, Any], str, bytes]): webhook payload.
193194
signature (str): webhook signature.
194195

195196
Returns:

githubkit/auth/app.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from collections.abc import AsyncGenerator, Generator
1+
from collections.abc import AsyncGenerator, Generator, Iterable, Sequence
22
from dataclasses import dataclass
33
from datetime import datetime, timedelta, timezone
44
from typing import TYPE_CHECKING, ClassVar, Optional, Union
@@ -35,8 +35,8 @@ class AppAuth(httpx.Auth):
3535
client_id: Optional[str] = None
3636
client_secret: Optional[str] = None
3737
installation_id: Union[Unset, int] = UNSET
38-
repositories: Union[Unset, list[str]] = UNSET
39-
repository_ids: Union[Unset, list[int]] = UNSET
38+
repositories: Union[Unset, Sequence[str]] = UNSET
39+
repository_ids: Union[Unset, Sequence[int]] = UNSET
4040
permissions: Union[Unset, "AppPermissionsType"] = UNSET
4141

4242
JWT_CACHE_KEY: ClassVar[LiteralString] = "githubkit:auth:app:{issuer}:jwt"
@@ -277,8 +277,8 @@ def __post_init__(self):
277277
def as_installation(
278278
self,
279279
installation_id: int,
280-
repositories: Union[Unset, list[str]] = UNSET,
281-
repository_ids: Union[Unset, list[int]] = UNSET,
280+
repositories: Union[Unset, Iterable[str]] = UNSET,
281+
repository_ids: Union[Unset, Iterable[int]] = UNSET,
282282
permissions: Union[Unset, "AppPermissionsType"] = UNSET,
283283
) -> "AppInstallationAuthStrategy":
284284
return AppInstallationAuthStrategy(
@@ -287,8 +287,8 @@ def as_installation(
287287
installation_id,
288288
self.client_id,
289289
self.client_secret,
290-
repositories,
291-
repository_ids,
290+
UNSET if repositories is UNSET else list(repositories),
291+
UNSET if repository_ids is UNSET else list(repository_ids),
292292
permissions,
293293
)
294294

@@ -318,8 +318,8 @@ class AppInstallationAuthStrategy(BaseAuthStrategy):
318318
installation_id: int
319319
client_id: Optional[str] = None
320320
client_secret: Optional[str] = None
321-
repositories: Union[Unset, list[str]] = UNSET
322-
repository_ids: Union[Unset, list[int]] = UNSET
321+
repositories: Union[Unset, Sequence[str]] = UNSET
322+
repository_ids: Union[Unset, Sequence[int]] = UNSET
323323
permissions: Union[Unset, "AppPermissionsType"] = UNSET
324324

325325
def __post_init__(self):

githubkit/auth/oauth.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from collections.abc import AsyncGenerator, Coroutine, Generator
1+
from collections.abc import AsyncGenerator, Coroutine, Generator, Iterable
22
from dataclasses import dataclass, field
33
from datetime import datetime, timedelta, timezone
44
from time import sleep
@@ -29,7 +29,7 @@
2929

3030

3131
def create_device_code(
32-
github: "GitHubCore", client_id: str, scopes: Optional[list[str]] = None
32+
github: "GitHubCore", client_id: str, scopes: Optional[Iterable[str]] = None
3333
) -> Generator[httpx.Request, httpx.Response, dict[str, Any]]:
3434
"""Create a device code for OAuth."""
3535
base_url = get_oauth_base_url(github.config.base_url)

githubkit/config.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from collections.abc import Iterable
12
from dataclasses import dataclass, fields
23
from typing import Any, Optional, Union
34
from typing_extensions import Self
@@ -42,7 +43,7 @@ def build_base_url(base_url: Optional[Union[str, httpx.URL]]) -> httpx.URL:
4243

4344

4445
def build_accept(
45-
accept_format: Optional[str] = None, previews: Optional[list[str]] = None
46+
accept_format: Optional[str] = None, previews: Optional[Iterable[str]] = None
4647
) -> str:
4748
if accept_format:
4849
accept_format = (
@@ -99,7 +100,7 @@ def get_config(
99100
*,
100101
base_url: Optional[Union[str, httpx.URL]] = None,
101102
accept_format: Optional[str] = None,
102-
previews: Optional[list[str]] = None,
103+
previews: Optional[Iterable[str]] = None,
103104
user_agent: Optional[str] = None,
104105
follow_redirects: bool = True,
105106
timeout: Optional[Union[float, httpx.Timeout]] = None,

githubkit/core.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from collections.abc import AsyncGenerator, Generator
1+
from collections.abc import AsyncGenerator, Generator, Iterable, Mapping
22
from contextlib import asynccontextmanager, contextmanager
33
from contextvars import ContextVar
44
from datetime import datetime, timedelta, timezone
@@ -77,7 +77,7 @@ def __init__(
7777
*,
7878
base_url: Optional[Union[str, httpx.URL]] = None,
7979
accept_format: Optional[str] = None,
80-
previews: Optional[list[str]] = None,
80+
previews: Optional[Iterable[str]] = None,
8181
user_agent: Optional[str] = None,
8282
follow_redirects: bool = True,
8383
timeout: Optional[Union[float, httpx.Timeout]] = None,
@@ -96,7 +96,7 @@ def __init__(
9696
*,
9797
base_url: Optional[Union[str, httpx.URL]] = None,
9898
accept_format: Optional[str] = None,
99-
previews: Optional[list[str]] = None,
99+
previews: Optional[Iterable[str]] = None,
100100
user_agent: Optional[str] = None,
101101
follow_redirects: bool = True,
102102
timeout: Optional[Union[float, httpx.Timeout]] = None,
@@ -115,7 +115,7 @@ def __init__(
115115
*,
116116
base_url: Optional[Union[str, httpx.URL]] = None,
117117
accept_format: Optional[str] = None,
118-
previews: Optional[list[str]] = None,
118+
previews: Optional[Iterable[str]] = None,
119119
user_agent: Optional[str] = None,
120120
follow_redirects: bool = True,
121121
timeout: Optional[Union[float, httpx.Timeout]] = None,
@@ -133,7 +133,7 @@ def __init__(
133133
config: Optional[Config] = None,
134134
base_url: Optional[Union[str, httpx.URL]] = None,
135135
accept_format: Optional[str] = None,
136-
previews: Optional[list[str]] = None,
136+
previews: Optional[Iterable[str]] = None,
137137
user_agent: Optional[str] = None,
138138
follow_redirects: bool = True,
139139
timeout: Optional[Union[float, httpx.Timeout]] = None,
@@ -338,22 +338,22 @@ def _check(
338338
self,
339339
response: httpx.Response,
340340
response_model: type[T],
341-
error_models: Optional[dict[str, type]] = None,
341+
error_models: Optional[Mapping[str, type]] = None,
342342
) -> Response[T]: ...
343343

344344
@overload
345345
def _check(
346346
self,
347347
response: httpx.Response,
348348
response_model: UnsetType = UNSET,
349-
error_models: Optional[dict[str, type]] = None,
349+
error_models: Optional[Mapping[str, type]] = None,
350350
) -> Response[Any]: ...
351351

352352
def _check(
353353
self,
354354
response: httpx.Response,
355355
response_model: Union[type[T], UnsetType] = UNSET,
356-
error_models: Optional[dict[str, type]] = None,
356+
error_models: Optional[Mapping[str, type]] = None,
357357
) -> Union[Response[T], Response[Any]]:
358358
if response.is_error:
359359
error_models = error_models or {}
@@ -443,7 +443,7 @@ def request(
443443
headers: Optional[HeaderTypes] = None,
444444
cookies: Optional[CookieTypes] = None,
445445
response_model: type[T],
446-
error_models: Optional[dict[str, type]] = None,
446+
error_models: Optional[Mapping[str, type]] = None,
447447
) -> Response[T]: ...
448448

449449
@overload
@@ -460,7 +460,7 @@ def request(
460460
headers: Optional[HeaderTypes] = None,
461461
cookies: Optional[CookieTypes] = None,
462462
response_model: UnsetType = UNSET,
463-
error_models: Optional[dict[str, type]] = None,
463+
error_models: Optional[Mapping[str, type]] = None,
464464
) -> Response[Any]: ...
465465

466466
def request(
@@ -476,7 +476,7 @@ def request(
476476
headers: Optional[HeaderTypes] = None,
477477
cookies: Optional[CookieTypes] = None,
478478
response_model: Union[type[T], UnsetType] = UNSET,
479-
error_models: Optional[dict[str, type]] = None,
479+
error_models: Optional[Mapping[str, type]] = None,
480480
) -> Union[Response[T], Response[Any]]:
481481
"""Send a request.
482482
@@ -525,7 +525,7 @@ async def arequest(
525525
headers: Optional[HeaderTypes] = None,
526526
cookies: Optional[CookieTypes] = None,
527527
response_model: type[T],
528-
error_models: Optional[dict[str, type]] = None,
528+
error_models: Optional[Mapping[str, type]] = None,
529529
) -> Response[T]: ...
530530

531531
@overload
@@ -542,7 +542,7 @@ async def arequest(
542542
headers: Optional[HeaderTypes] = None,
543543
cookies: Optional[CookieTypes] = None,
544544
response_model: UnsetType = UNSET,
545-
error_models: Optional[dict[str, type]] = None,
545+
error_models: Optional[Mapping[str, type]] = None,
546546
) -> Response[Any]: ...
547547

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

githubkit/github.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from collections.abc import Awaitable
1+
from collections.abc import Awaitable, Iterable, Mapping
22
from functools import cached_property
33
from typing import TYPE_CHECKING, Any, Callable, Optional, TypeVar, Union, overload
44
from typing_extensions import ParamSpec
@@ -70,7 +70,7 @@ def __init__(
7070
*,
7171
base_url: Optional[Union[str, httpx.URL]] = None,
7272
accept_format: Optional[str] = None,
73-
previews: Optional[list[str]] = None,
73+
previews: Optional[Iterable[str]] = None,
7474
user_agent: Optional[str] = None,
7575
follow_redirects: bool = True,
7676
timeout: Optional[Union[float, httpx.Timeout]] = None,
@@ -89,7 +89,7 @@ def __init__(
8989
*,
9090
base_url: Optional[Union[str, httpx.URL]] = None,
9191
accept_format: Optional[str] = None,
92-
previews: Optional[list[str]] = None,
92+
previews: Optional[Iterable[str]] = None,
9393
user_agent: Optional[str] = None,
9494
follow_redirects: bool = True,
9595
timeout: Optional[Union[float, httpx.Timeout]] = None,
@@ -108,7 +108,7 @@ def __init__(
108108
*,
109109
base_url: Optional[Union[str, httpx.URL]] = None,
110110
accept_format: Optional[str] = None,
111-
previews: Optional[list[str]] = None,
111+
previews: Optional[Iterable[str]] = None,
112112
user_agent: Optional[str] = None,
113113
follow_redirects: bool = True,
114114
timeout: Optional[Union[float, httpx.Timeout]] = None,
@@ -140,7 +140,7 @@ def graphql(self) -> GraphQLNamespace:
140140

141141
# alias for graphql.arequest
142142
async def async_graphql(
143-
self, query: str, variables: Optional[dict[str, Any]] = None
143+
self, query: str, variables: Optional[Mapping[str, Any]] = None
144144
) -> dict[str, Any]:
145145
return await self.graphql.arequest(query, variables)
146146

0 commit comments

Comments
 (0)