-
-
Notifications
You must be signed in to change notification settings - Fork 564
feat: Type-safe "optional-nullable" fields #3779 #3791
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
base: main
Are you sure you want to change the base?
Conversation
Reviewer's Guide by SourceryThis pull request introduces Updated class diagram for optional-nullable fieldsclassDiagram
class Maybe~T~
class UnsetType
class Union~T, UnsetType, None~
class TypeGuard~Union[T, None]~
UnsetType <|-- Maybe~T~
Union~T, UnsetType, None~ ..> Maybe~T~ : includes
TypeGuard~Union[T, None]~ ..> Union~T, UnsetType, None~ : checks type of
note for Maybe~T~ "Represents a value that can be either of type T, Unset, or None"
note for UnsetType "Represents an unset value"
note for Union~T, UnsetType, None~ "Type alias for T | UnsetType | None"
note for TypeGuard~Union[T, None]~ "Type guard to check if a value is not Unset"
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
for more information, see https://pre-commit.ci
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.
Hey @nrbnlulu - I've reviewed your changes - here's some feedback:
Overall Comments:
- Consider adding a changelog entry to describe the new
strawberry.Maybe
andstrawberry.isnt_unset
features.
Here's what I looked at during the review
- 🟡 General issues: 2 issues found
- 🟢 Security: all looks good
- 🟡 Testing: 2 issues found
- 🟢 Complexity: all looks good
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
|
||
Now you can use `strawberry.Maybe` and `strawberry.isnt_unset` to identify if a value was provided or not. | ||
|
||
i.e |
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.
suggestion (typo): Use "e.g." instead of "i.e."
"i.e." means "that is", while "e.g." means "for example". Since you're providing an example, "e.g." is more appropriate.
i.e | |
e.g. |
Thanks for adding the Here's a preview of the changelog: This release adds a new (preferable) way to handle optional updates. Up until Now you can use i.e import strawberry
@strawberry.type
class User:
name: str
phone: str | None
@strawberry.input
class UpdateUserInput:
name: str
phone: strawberry.Maybe[str]
@strawberry.type
class Mutation:
def update_user(self, info, input: UpdateUserInput) -> User:
reveal_type(input.phone) # Some[str | None] | None
if input.phone:
reveal_type(input.phone.value) # str | None
update_user_phone(input.phone.value)
return User(name=input.name, phone=input.phone) You can also use import strawberry
@strawberry.field
def filter_users(self, phone: strawberry.Maybe[str] = None) -> list[User]:
if phone:
return filter_users_by_phone(phone.value)
return get_all_users() Here's the tweet text:
|
Thanks for adding the Here's a preview of the changelog: This release adds a new (preferable) way to handle optional updates. Now you can use i.e import strawberry
@strawberry.type
class User:
name: str
phone: str | None
@strawberry.input
class UpdateUserInput:
name: str
phone: strawberry.Maybe[str]
@strawberry.type
class Mutation:
def update_user(self, info, input: UpdateUserInput) -> User:
if strawberry.isnt_unset(input.phone):
phone = (
input.phone
) # could be `str | None` in case we want to nullify the phone
return User(name=input.name, phone=phone) Here's the tweet text:
|
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3791 +/- ##
==========================================
- Coverage 95.24% 95.13% -0.12%
==========================================
Files 499 501 +2
Lines 32417 32591 +174
Branches 1681 1694 +13
==========================================
+ Hits 30877 31006 +129
- Misses 1275 1318 +43
- Partials 265 267 +2 🚀 New features to boost your workflow:
|
CodSpeed Performance ReportMerging #3791 will not alter performanceComparing Summary
|
…SET automatically.
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
bump |
/pre-release |
Pre-release👋 Releasing commit [9411124] to PyPi as pre-release! 📦 |
Pre-release👋 Pre-release 0.262.7.dev.1743345593 [9411124] has been released on PyPi! 🚀 poetry add strawberry-graphql==0.262.7.dev.1743345593 |
@nrbnlulu I was playing with this a bit, and I don't think I like the approach, not sure if it is I think we should have a Maybe type that can be used directly, instead of having to use strawberry.exists, import strawberry
@strawberry.type
class User:
name: str
phone: str | None
@strawberry.input
class UpdateUserInput:
name: str
phone: strawberry.Maybe[str | None]
@strawberry.type
class Mutation:
def update_user(self, info, input: UpdateUserInput) -> User:
reveal_type(input.phone) # strawberry.Maybe[str]
if input.phone:
reveal_type(input.phone) # str | None
...
match input.phone:
case strawberry.Some(phone):
reveal_type(phone) # str | None
...
case None:
# nothing to do
...
return User(name=input.name, phone=input.phone) what do you think? /cc @bellini666 |
yeah I took a different approach inspired by #3779 (comment) . I found it less involved.. |
I'd say let's go with |
@patrick91 | @bellini666 Done |
Description
Note that I renamed
not_unset
toexists
bc I think its easier to read albeit more common in commit8526fca
this is easily revertable...
Types of Changes
Issues Fixed or Closed by This PR
Checklist
Summary by Sourcery
Adds
strawberry.Maybe
andstrawberry.isnt_unset
to handle optional and nullable input fields in mutations, providing a more type-safe and intuitive way to determine if a value is present or absent. It also includes tests and documentation for the new feature.New Features:
strawberry.Maybe
andstrawberry.isnt_unset
to handle optional and nullable input fields in mutations, providing a more type-safe and intuitive way to determine if a value is present or absent.Enhancements:
strawberry.UNSET
withstrawberry.Maybe
andstrawberry.isnt_unset
for optional input fields, improving code readability and reducing potential errors.Documentation:
strawberry.Maybe
feature and how to use it.Tests:
strawberry.Maybe
with different scenarios, including setting values from None to Some, Some to None, and handling absent values.