Skip to content
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

add support for default values with a field that is a pydantic model #3499

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

ppease
Copy link
Contributor

@ppease ppease commented May 14, 2024

In this PR, I am adding support for having a pydantic model be a default value for a field on an input. Previously this would not generate the proper schema.

Description

My original fix was modifying code in from_input_field inside of the schema converter. This is the point where we are instantiating a graphql input type, and I was checking to see if the default value had a model dump method on it. This seems a little better since it is still within the pydantic specific code. The issue I'm trying to solve is that when we print the schema from within print_input_value we use ast_from_value on the default value. We fall into the is_input_object_type case and then we check if there is an object definition for the value. If is_input_object_type evaluates to True then we call strawberry.asdict. I think we'd want to do something similar with a pydantic instance. Today we fail the check and end up returning None which causes the default value to not be printed in the schema. What I'm doing in this MR is detecting if our default value is a pydantic instance, and if it is then I am storing the serialized value for the pydantic model as the default value.

I'm curious to know if there is a better way to plug into the printing process because that feels more inline with what we are doing with dataclasses today. I could add code that I had previously into either print_input_value or ast_from_value, but that felt a little more hacky since we are putting pydantic specific stuff in higher level code.

Types of Changes

  • Core
  • Bugfix
  • New feature
  • Enhancement/optimization
  • Documentation

Issues Fixed or Closed by This PR

This fixes this issue

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • I have tested the changes and verified that they work and don't break anything (as well as I can manage).

@@ -17,6 +17,9 @@
IS_PYDANTIC_V2: bool = PYDANTIC_VERSION.startswith("2.")
IS_PYDANTIC_V1: bool = not IS_PYDANTIC_V2

if IS_PYDANTIC_V2:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is absolutely necessary, but when I was running the tests locally with pydantic v2 all of the mypy automated tests were failing without this import.

@botberry
Copy link
Member

botberry commented May 14, 2024

Hi, thanks for contributing to Strawberry 🍓!

We noticed that this PR is missing a RELEASE.md file. We use that to automatically do releases here on GitHub and, most importantly, to PyPI!

So as soon as this PR is merged, a release will be made 🚀.

Here's an example of RELEASE.md:

Release type: patch

Description of the changes, ideally with some examples, if adding a new feature.

Release type can be one of patch, minor or major. We use semver, so make sure to pick the appropriate type. If in doubt feel free to ask :)

Here's the tweet text:

🆕 Release (next) is out! Thanks to ppease for the PR 👏

Get it here 👉 https://strawberry.rocks/release/(next)

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @ppease - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 3 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

strawberry/schema/schema_converter.py Outdated Show resolved Hide resolved
strawberry/schema/schema_converter.py Outdated Show resolved Hide resolved
Comment on lines 20 to 21
if IS_PYDANTIC_V2:
import pydantic.v1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Clarify the purpose of importing pydantic.v1 under IS_PYDANTIC_V2 condition.

It seems counterintuitive to import a version-specific module conditionally based on the version check. Ensure this is intentional and document the rationale behind this decision.

Copy link

codspeed-hq bot commented May 14, 2024

CodSpeed Performance Report

Merging #3499 will not alter performance

Comparing ppease:feature/default-values (012f329) with main (60984d7)

Summary

✅ 12 untouched benchmarks

@ppease ppease changed the title add support for default values add support for default values with a field that is a pydantic model May 14, 2024
Copy link

codecov bot commented May 14, 2024

Codecov Report

Attention: Patch coverage is 97.36842% with 2 lines in your changes are missing coverage. Please review.

Project coverage is 96.50%. Comparing base (60984d7) to head (012f329).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3499      +/-   ##
==========================================
+ Coverage   94.43%   96.50%   +2.07%     
==========================================
  Files         506      511       +5     
  Lines       31783    33008    +1225     
  Branches     3656     5488    +1832     
==========================================
+ Hits        30014    31856    +1842     
+ Misses       1487      918     -569     
+ Partials      282      234      -48     

@@ -27,3 +29,72 @@ def filter(self, input: FilterInput) -> str:
assert not result.errors
assert result.data
assert result.data["filter"] == "Hello nope"


def test_input_with_nonscalar_field_default():
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally thought this had issues, but as long as you specify the default factory, this works as intended. I didn't see any tests explicitly testing this so I added one.

@ppease ppease marked this pull request as draft May 14, 2024 21:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pydantic models as a default values in inputs
2 participants