-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support both Pydantic v1 and v2 (#65)
* unpin to allow pydantic v2 * 🎨 set default value * ⬆️ upgrade fastapi-websocket-rpc * version bump * ➕ add `packaging` as dependency * 🎨 add util methods for supporting both pydantic 1 and 2 * use `get_model_serializer` to support pydantic v1 and 2 * 🎨 black formatting * 🎨 imports * 🎨 add helper method for printing model dict in pydantic v1 and 2 * 🎨 support kwargs in helper methods * 🎨 helper method to print model dict * revert version bump * 🎨 update pydantic helper methods to return result in one call * 🎨 update to use new pydantic helper methods
- Loading branch information
Showing
5 changed files
with
45 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import pydantic | ||
from packaging import version | ||
|
||
|
||
# Helper methods for supporting Pydantic v1 and v2 | ||
def is_pydantic_pre_v2(): | ||
return version.parse(pydantic.VERSION) < version.parse("2.0.0") | ||
|
||
|
||
def pydantic_serialize(model, **kwargs): | ||
if is_pydantic_pre_v2(): | ||
return model.json(**kwargs) | ||
else: | ||
return model.model_dump_json(**kwargs) | ||
|
||
|
||
def pydantic_to_dict(model, **kwargs): | ||
if is_pydantic_pre_v2(): | ||
return model.dict(**kwargs) | ||
else: | ||
return model.model_dump(**kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
fastapi-websocket-rpc>=0.1.24,<1 | ||
fastapi-websocket-rpc>=0.1.25,<1 | ||
packaging>=20.4 | ||
permit-broadcaster[redis,postgres,kafka]>=0.2.5,<3 | ||
pydantic>=1.9.1,<2 | ||
websockets>=10.3,<11 | ||
pydantic>=1.9.1 | ||
websockets>=10.3,<11 |