-
-
Notifications
You must be signed in to change notification settings - Fork 535
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
Pydantic V2 Support #2978
Comments
Support for It's defined in the original v1 implementation in: strawberry/strawberry/experimental/pydantic/object_type.py Lines 207 to 212 in bb35e09
strawberry/strawberry/experimental/pydantic/object_type.py Lines 279 to 282 in bb35e09
and there are also some references to it in strawberry/experimental/pydantic/conversion.py#L62, However, none are present in thejaminator's fork I suppose we could just add it back in. It also seems like there aren't any tests for this functionality, since all seem to have passed. |
@rgon would you be interested in adding those tests? 😊 (also, thanks a lot for testing the pre-release!) |
Thanks alot for testing it out! Appreciate it! This test should work. Could you try it out? def test_can_convert_input_types_to_pydantic():
class User(BaseModel):
age: int
password: Optional[str]
@strawberry.experimental.pydantic.input(User)
class UserInput:
age: strawberry.auto
password: strawberry.auto
data = UserInput(age=1, password=None)
user = data.to_pydantic()
assert user.age == 1
assert user.password is None Are you getting the errors at runtime or due to mypy / pyright? Or are you talking about the specific custom redefinition of |
@patrick91 I will have to take a shot at this. Not an expert at that, but will take a shot at this in a few days if you don't mind a little post-PR guidance :) @thejaminator no worries! Yes, I'm talking about defining custom @strawberry.experimental.pydantic.type(model=User)
class UserType:
id: strawberry.auto
# Flatten the content dict into specific fields in the query
content_name: Optional[str] = None
content_description: Optional[str] = None
@staticmethod
def from_pydantic(instance: User, extra: Dict[str, Any] = None) -> "UserType":
data = instance.dict()
content = data.pop("content")
data.update({f"content_{k.value}": v for k, v in content.items()})
return UserType(**data)
def to_pydantic(self) -> User:
data = dataclasses.asdict(self)
# Pull out the content_* fields into a dict
content = {}
for enum_member in ContentType:
key = f"content_{enum_member.value}"
if data.get(key) is not None:
content[enum_member.value] = data.pop(key)
return User(content=content, **data) I see no runtime/mypy errors. However, when implementing a custom 're'-serializer of sorts, with this pre-release these methods had no effect (not even print statements were called). Tracing the function back, I noticed that the check for custom-defined conversion methods is not yet implemented. The test you sent works perfectly. |
@rgon thank you!
Thats weird. because I havent changed anything at all for that logic. it should be the same as V1. Not sure why that worked before but it doesn't now Besides the custom to_pydantic function, are there other issues you've faced? If not, I can merge in the current PR earlier :) |
is there a version that works with Pydantic v2 yet? :D very much appreciated !!! |
@AlexTo we just released one! Feel free to open an issue if you find bugs! I'll close this issue 😊 |
@patrick91 that's awesome, will update right away, thank you |
To try it out,
Please report any problems faced migrating in this mega issue
Current yet to be merged MR here
Upvote & Fund
The text was updated successfully, but these errors were encountered: