Skip to content

Commit 0b62d40

Browse files
authored
✨ use PEP747 TypeForm
1 parent 5faae7b commit 0b62d40

5 files changed

Lines changed: 304 additions & 290 deletions

File tree

githubkit/compat.py

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from collections.abc import Generator
2-
from typing import TYPE_CHECKING, Any, Callable, Protocol, TypeVar, overload
2+
from typing import TYPE_CHECKING, Any, Callable, Protocol, TypeVar
3+
from typing_extensions import TypeForm
34

45
from pydantic import VERSION
56

@@ -36,24 +37,10 @@ class GitHubModel(BaseModel):
3637
class ExtraGitHubModel(GitHubModel):
3738
model_config = ConfigDict(extra="allow")
3839

39-
# Remove the overload once [PEP747](https://peps.python.org/pep-0747/) is accepted
40-
# We should use TypeForm here
41-
@overload
42-
def type_validate_python(type_: type[T], data: Any) -> T: ...
43-
44-
@overload
45-
def type_validate_python(type_: Any, data: Any) -> Any: ...
46-
47-
def type_validate_python(type_: type[T], data: Any) -> T:
40+
def type_validate_python(type_: TypeForm[T], data: Any) -> T:
4841
return TypeAdapter(type_).validate_python(data)
4942

50-
@overload
51-
def type_validate_json(type_: type[T], data: Any) -> T: ...
52-
53-
@overload
54-
def type_validate_json(type_: Any, data: Any) -> Any: ...
55-
56-
def type_validate_json(type_: type[T], data: Any) -> T:
43+
def type_validate_json(type_: TypeForm[T], data: Any) -> T:
5744
return TypeAdapter(type_).validate_json(data)
5845

5946
def model_dump(model: BaseModel, by_alias: bool = True) -> dict[str, Any]:
@@ -97,22 +84,10 @@ class ExtraGitHubModel(BaseModel):
9784
class Config:
9885
extra = Extra.allow
9986

100-
@overload
101-
def type_validate_python(type_: type[T], data: Any) -> T: ...
102-
103-
@overload
104-
def type_validate_python(type_: Any, data: Any) -> Any: ...
105-
106-
def type_validate_python(type_: type[T], data: Any) -> T:
87+
def type_validate_python(type_: TypeForm[T], data: Any) -> T:
10788
return parse_obj_as(type_, data)
10889

109-
@overload
110-
def type_validate_json(type_: type[T], data: Any) -> T: ...
111-
112-
@overload
113-
def type_validate_json(type_: Any, data: Any) -> Any: ...
114-
115-
def type_validate_json(type_: type[T], data: Any) -> T:
90+
def type_validate_json(type_: TypeForm[T], data: Any) -> T:
11691
return parse_raw_as(type_, data)
11792

11893
def to_jsonable_python(obj: Any) -> Any:

githubkit/core.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -354,26 +354,26 @@ async def _arequest(
354354
raise RequestError(e) from e
355355

356356
# check and parse response
357+
def _check_is_error(self, response: httpx.Response) -> bool:
358+
"""Check if the response is an error."""
359+
return response.is_error
360+
357361
@overload
358362
def _check(
359363
self,
360364
response: httpx.Response,
361365
response_model: type[T],
362-
error_models: Optional[Mapping[str, type]] = None,
366+
error_models: Optional[Mapping[str, Any]] = None,
363367
) -> Response[T]: ...
364368

365369
@overload
366370
def _check(
367371
self,
368372
response: httpx.Response,
369373
response_model: UnsetType = UNSET,
370-
error_models: Optional[Mapping[str, type]] = None,
374+
error_models: Optional[Mapping[str, Any]] = None,
371375
) -> Response[Any]: ...
372376

373-
def _check_is_error(self, response: httpx.Response) -> bool:
374-
"""Check if the response is an error."""
375-
return response.is_error
376-
377377
def _check(
378378
self,
379379
response: httpx.Response,

githubkit/utils.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from enum import Enum
22
from functools import partial
33
import inspect
4-
from typing import Any, Generic, Literal, Optional, TypeVar, final, overload
4+
from typing import Any, Generic, Literal, Optional, TypeVar, final
5+
from typing_extensions import TypeForm
56

67
from hishel._utils import generate_key
78
import httpcore
@@ -85,13 +86,7 @@ def is_async(obj: Any) -> bool:
8586
class TaggedUnion(Generic[T]):
8687
__slots__ = ("discriminator", "tag", "type_")
8788

88-
@overload
89-
def __init__(self, type_: type[T], discriminator: str, tag: str) -> None: ...
90-
91-
@overload
92-
def __init__(self, type_: Any, discriminator: str, tag: str) -> None: ...
93-
94-
def __init__(self, type_: type[T], discriminator: str, tag: str) -> None:
89+
def __init__(self, type_: TypeForm[T], discriminator: str, tag: str) -> None:
9590
self.type_ = type_
9691
self.discriminator = discriminator
9792
self.tag = tag

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ dependencies = [
1111
"anyio >=3.6.1, <5.0.0",
1212
"httpx >=0.23.0, <1.0.0",
1313
"hishel >=0.0.21, <=0.2.0",
14-
"typing-extensions >=4.11.0, <5.0.0",
14+
"typing-extensions >=4.13.0, <5.0.0",
1515
"pydantic >=1.9.1, <3.0.0, !=2.5.0, !=2.5.1",
1616
]
1717

@@ -147,6 +147,8 @@ typeCheckingMode = "standard"
147147
reportPrivateImportUsage = false
148148
reportShadowedImports = false
149149
disableBytesTypePromotions = true
150+
# enable PEP 747 `TypeForm` check
151+
enableExperimentalFeatures = true
150152

151153
pythonPlatform = "All"
152154
defineConstant = { PYDANTIC_V2 = true }

0 commit comments

Comments
 (0)