-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support both Pydantic v1 and v2 #24
Conversation
No changes required for migration, hence the PR name: "permit" v2. |
Hi @ff137 ! thanks for this great contribution, I'll have someone form the team review soon. |
Cool! Thanks. GenericModel is logged as a deprecation warning, which can be fixed. |
Signed-off-by: ff137 <[email protected]>
@orweis I've looked again, and the error was Fortunately, that import is unused! So, I've cleaned up unused imports, and tests pass locally for me. I just notice there will indeed be deprecation warnings that can spam logs for users. |
@ff137 I'm convinced :) |
Cool! Thank you. I had some ideas for supporting both v1 and v2 of pydantic. Quite inundated with other work atm, but I'll wrap this one up in the next few days |
So, the deprecated GenericModel usage can be handled quite simply: import pydantic
from packaging import version
from pydantic import BaseModel
# Check pydantic version to handle deprecated GenericModel
if version.parse(pydantic.VERSION) < version.parse("2.0.0"):
from pydantic.generics import GenericModel
class RpcResponse(GenericModel, Generic[ResponseT]):
result: ResponseT
result_type: Optional[str]
call_id: Optional[UUID] = None
else:
class RpcResponse(BaseModel, Generic[ResponseT]):
result: ResponseT
result_type: Optional[str]
call_id: Optional[UUID] = None That should be the only adjustment necessary to support pydantic 2 without deprecation warnings. Note: This requires |
@orweis @roekatz There we go! I added a way to support both pydantic v1 and v2, without deprecation warnings. Previously it complained about |
A similar approach was followed to add support in the pubsub library: permitio/fastapi_websocket_pubsub#65 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ff137 That's great!
Had one request and one suggestion, see my comments.
Otherwise let's merge that already :)
Updated and suggestions applied in _pubsub PR as well 👌 |
Amazing! :) |
Unpin pydantic in the requirements to allow for v2.
Added helper methods to utils in order to:
.model_dump_json()
, or old deprecated name.json()
, for serialisation.model_validate()
, or old deprecated name.parse_obj()
, for deserialisationGenericModel
in pydantic v1, orBaseModel
in v2