From a81c0c99867b212ea1d16011bc976934f842b9c2 Mon Sep 17 00:00:00 2001 From: dosisod <39638017+dosisod@users.noreply.github.com> Date: Mon, 20 Mar 2023 23:24:02 -0700 Subject: [PATCH 1/3] Add wrapper to prep data for JSON encoding: Closes #21. --- githubkit/core.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/githubkit/core.py b/githubkit/core.py index 165931df9..71a341ef0 100644 --- a/githubkit/core.py +++ b/githubkit/core.py @@ -17,6 +17,7 @@ ) import httpx +from pydantic.json import pydantic_encoder from .response import Response from .config import Config, get_config @@ -237,7 +238,7 @@ def _request( content=content, data=data, files=files, - json=json, + json=self._convert(json), headers=headers, cookies=cookies, ) @@ -269,7 +270,7 @@ async def _arequest( content=content, data=data, files=files, - json=json, + json=self._convert(json), headers=headers, cookies=cookies, ) @@ -298,6 +299,24 @@ def _check( raise RequestFailed(rep) return Response(response, response_model) + def _convert(self, obj: Any) -> Any: + if isinstance(obj, dict): + for k, v in obj.items(): + obj[k] = self._convert(v) + + return obj + + if isinstance(obj, list): + for i, item in enumerate(obj): + obj[i] = self._convert(item) + + return obj + + if isinstance(obj, (int, float, str, bool, bytes, type(None))): + return obj + + return pydantic_encoder(obj) + # sync request and check def request( self, From c5b5b6b2a934497a7784cf271f4fb213ea6d52d1 Mon Sep 17 00:00:00 2001 From: dosisod <39638017+dosisod@users.noreply.github.com> Date: Mon, 20 Mar 2023 23:30:18 -0700 Subject: [PATCH 2/3] Remove `bytes` --- githubkit/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/githubkit/core.py b/githubkit/core.py index 71a341ef0..3928dbe04 100644 --- a/githubkit/core.py +++ b/githubkit/core.py @@ -312,7 +312,7 @@ def _convert(self, obj: Any) -> Any: return obj - if isinstance(obj, (int, float, str, bool, bytes, type(None))): + if isinstance(obj, (int, float, str, bool, type(None))): return obj return pydantic_encoder(obj) From 84d32bc31a4bbedbee09d8ad6b00eccad7f7c006 Mon Sep 17 00:00:00 2001 From: dosisod <39638017+dosisod@users.noreply.github.com> Date: Mon, 20 Mar 2023 23:56:56 -0700 Subject: [PATCH 3/3] Use list/dict comprehensions --- githubkit/core.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/githubkit/core.py b/githubkit/core.py index 3928dbe04..8f01215df 100644 --- a/githubkit/core.py +++ b/githubkit/core.py @@ -301,18 +301,12 @@ def _check( def _convert(self, obj: Any) -> Any: if isinstance(obj, dict): - for k, v in obj.items(): - obj[k] = self._convert(v) + return {k: self._convert(v) for k, v in obj.items()} - return obj - - if isinstance(obj, list): - for i, item in enumerate(obj): - obj[i] = self._convert(item) - - return obj + if isinstance(obj, (list, tuple)): + return [self._convert(item) for item in obj] - if isinstance(obj, (int, float, str, bool, type(None))): + if obj is None or isinstance(obj, (int, float, str, bool)): return obj return pydantic_encoder(obj)