-
-
Notifications
You must be signed in to change notification settings - Fork 25
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 Pydantic v2 #38
Comments
The error reported with class Test(BaseModel):
field: Union[int, str] = Field(default=1, pattern="^pkg") This is not related to the |
After some experiment... It works with the from typing import Union, Annotated
from pydantic import BaseModel, Field, ConfigDict
class Test(BaseModel):
field: Union[int, Annotated[str, Field(pattern="^pkg")]] = Field(default=1)
model_config = ConfigDict(strict=True) I have reported this problem to pydantic issue. |
The same issue as pydantic/pydantic#6577. |
I guess that makes sense, though I'd argue any object with a How trivial would it be to add codegen support for this |
Since the new constrant logic of pydantic v2, we may need to bind the constrants directly on the type instead of the field, and generate the type using |
Thanks to @sudosubin, githubkit now supports pydantic v2. But, before this feature can be released, i need to investigate how to make it compatible with pydantic v1 and v2. I will refactor the codegen for future improvements including lazy loading #11 after another project i'm currently working on finished. |
Pydantic v2 just came out, so it would be nice if githubkit could support the newer, faster version of Pydantic. I attempted to upgrade it myself, but there are a few bugs and non-trivial design decisions that would need to be made to support Pydantic v2, so I thought I'd bring this up now.
The migration guide does a good job of explaining what's changed, and how to upgrade your code. I'm just showing what I've encountered thus far, though there is probably additional work that needs to be done.
First up,
regex
is being renamed topattern
inField()
objects. Easy enough to fix, just change it in the codegen.Second thing I ran into is that
parse_raw_as
has been removed, and instead you need to use aTypeAdapter
:Lastly (or at least, where I stopped) is with
Missing[]
. This is a bug, as Pydantic v2 does not seem to like literal values likeLiteral[UNSET]
. For example, the following code is throwing an error:This produces:
Replacing
Missing[str]
withUnion[Literal["xyz"], str]
still fails, butOptional[str]
works fine.This seems to be tracked in pydantic/pydantic#6601. The error looks slightly different, though it still is an issue with
Literal
values inUnion
s.I'm happy to work on a PR for this, though I thought I would get your input on a few things before I go ahead:
Literal
issue doesn't get fixed soon (though it probably should), do you see any potential workarounds? We could just useOptional[str]
, thoughNone
doesn't convey the same thing asUNSET
.Let me know what your thoughts are on this, thanks!
The text was updated successfully, but these errors were encountered: