Skip to content

Commit

Permalink
✨ support pydantic compat
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyongyu authored Jan 10, 2024
1 parent d0217b6 commit 8d038ce
Show file tree
Hide file tree
Showing 11 changed files with 708 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
os: [ubuntu-latest, windows-latest, macos-latest]
env: [pydantic-v2]
env: [pydantic-v1, pydantic-v2]
fail-fast: false
env:
OS: ${{ matrix.os }}
Expand Down
651 changes: 651 additions & 0 deletions envs/pydantic-v1/poetry.lock

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions envs/pydantic-v1/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[tool.poetry]
name = "githubkit-pydantic-v1"
version = "0.1.0"
description = "Private pydantic v1 test env for githubkit"
authors = ["yanyongyu <[email protected]>"]
license = "MIT"

[tool.poetry.dependencies]
python = "^3.8"

[tool.poetry.group.dev.dependencies]
pydantic = "^1.0.0"
githubkit-test = { path = "../test/", develop = false }
githubkit = { path = "../../", extras = ["all"], develop = true }

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
2 changes: 1 addition & 1 deletion envs/pydantic-v2/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions githubkit/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def __get_validators__(cls) -> Generator[Callable[..., Any], None, None]:

if PYDANTIC_V2: # pragma: pydantic-v2
from pydantic_core import CoreSchema, core_schema
from pydantic_core import to_jsonable_python as to_jsonable_python
from pydantic import (
BaseModel,
ConfigDict,
Expand Down Expand Up @@ -81,6 +82,7 @@ def custom_validation(class_: Type["CVC"]) -> Type["CVC"]:
return class_

else: # pragma: pydantic-v1
from pydantic.json import pydantic_encoder
from pydantic import Extra, BaseModel, parse_obj_as, parse_raw_as, root_validator

class GitHubModel(BaseModel):
Expand All @@ -97,6 +99,18 @@ def type_validate_python(type_: Type[T], data: Any) -> T:
def type_validate_json(type_: Type[T], data: Any) -> T:
return parse_raw_as(type_, data)

def to_jsonable_python(obj: Any) -> Any:
if isinstance(obj, dict):
return {k: to_jsonable_python(v) for k, v in obj.items()}

if isinstance(obj, (list, tuple)):
return [to_jsonable_python(item) for item in obj]

if obj is None or isinstance(obj, (int, float, str, bool)):
return obj

return pydantic_encoder(obj)

def model_dump(model: BaseModel, by_alias: bool = True) -> Dict[str, Any]:
return model.dict(by_alias=by_alias)

Expand Down
6 changes: 3 additions & 3 deletions githubkit/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import hishel

from .response import Response
from .utils import obj_to_jsonable
from .compat import to_jsonable_python
from .config import Config, get_config
from .auth import BaseAuthStrategy, TokenAuthStrategy, UnauthAuthStrategy
from .typing import (
Expand Down Expand Up @@ -272,7 +272,7 @@ def _request(
content=content,
data=data,
files=files,
json=obj_to_jsonable(json),
json=to_jsonable_python(json),
headers=headers,
cookies=cookies,
)
Expand Down Expand Up @@ -304,7 +304,7 @@ async def _arequest(
content=content,
data=data,
files=files,
json=obj_to_jsonable(json),
json=to_jsonable_python(json),
headers=headers,
cookies=cookies,
)
Expand Down
15 changes: 0 additions & 15 deletions githubkit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from typing import Any, Dict, Type, Generic, Literal, TypeVar, final

from pydantic import BaseModel
from pydantic_core import to_jsonable_python

from .compat import PYDANTIC_V2, custom_validation, type_validate_python

Expand Down Expand Up @@ -69,20 +68,6 @@ def is_async(obj: Any) -> bool:
return inspect.iscoroutinefunction(func_)


# FIXME: remove pydantic-core dependency
def obj_to_jsonable(obj: Any) -> Any:
if isinstance(obj, dict):
return {k: obj_to_jsonable(v) for k, v in obj.items()}

if isinstance(obj, (list, tuple)):
return [obj_to_jsonable(item) for item in obj]

if obj is None or isinstance(obj, (int, float, str, bool)):
return obj

return to_jsonable_python(obj)


class TaggedUnion(Generic[T]):
__slots__ = ("type_", "discriminator", "tag")

Expand Down
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ python = "^3.8"
hishel = "^0.0.21"
httpx = ">=0.23.0, <1.0.0"
typing-extensions = "^4.3.0"
pydantic = ">=2.0.0, <3.0.0, !=2.5.0, !=2.5.1"
pydantic = ">=1.9.1, <3.0.0, !=2.5.0, !=2.5.1"
anyio = { version = ">=3.6.1, <5.0.0", optional = true }
PyJWT = { version = "^2.4.0", extras = ["crypto"], optional = true }

Expand Down
4 changes: 3 additions & 1 deletion scripts/setup-envs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
poetry config virtualenvs.in-project true

# setup dev environment
echo "Setting up dev environment"
poetry install --all-extras && poetry run pre-commit install

# setup pydantic v2 test environment
for env in $(find envs/ -maxdepth 1 -mindepth 1 -type d -not -name test); do
for env in $(find ./envs/ -maxdepth 1 -mindepth 1 -type d -not -name test); do
echo "Setting up $env environment"
(cd $env && poetry install --no-root)
done
15 changes: 15 additions & 0 deletions scripts/update-envs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#! /usr/bin/env bash

# update test env
echo "Updating test env..."
(cd ./envs/test/ && poetry update)

# update dev env
echo "Updating dev env..."
poetry update

# update other envs
for env in $(find ./envs/ -maxdepth 1 -mindepth 1 -type d -not -name test); do
echo "Updating $env env..."
(cd $env && poetry update)
done

0 comments on commit 8d038ce

Please sign in to comment.