-
-
Notifications
You must be signed in to change notification settings - Fork 660
Change Maybe to Some[T] | None instead of Some[T | None] | None
#3961
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a080d46
Change `Maybe` to `Some[T] | None` instead of `Some[T | None] | None`
patrick91 cc1998c
Codemod
patrick91 3166654
Update tests
patrick91 7c9e290
Fix type
patrick91 9e41ac7
Fix
patrick91 2a3d6bf
Fix pipe
patrick91 963a46c
Docs
patrick91 0fa28a9
Make sure we can't pass null
patrick91 b87accf
Use validation rules
patrick91 fe6ba7c
Apply suggestion from @patrick91
patrick91 b9c0180
Add release notes
patrick91 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| --- | ||
| title: 0.279.0 Breaking Changes | ||
| slug: breaking-changes/0.279.0 | ||
| --- | ||
|
|
||
| # v0.279.0 Breaking Changes | ||
|
|
||
| This release changes the `strawberry.Maybe` type definition to provide a more | ||
| consistent and intuitive API for handling optional fields. | ||
|
|
||
| ## What Changed | ||
|
|
||
| The `Maybe` type definition has been changed from: | ||
|
|
||
| ```python | ||
| Maybe: TypeAlias = Union[Some[Union[T, None]], None] | ||
| ``` | ||
|
|
||
| to: | ||
|
|
||
| ```python | ||
| Maybe: TypeAlias = Union[Some[T], None] | ||
| ``` | ||
|
|
||
| ## Impact on Your Code | ||
|
|
||
| ### Type Annotations | ||
|
|
||
| If you were using `Maybe[T]` and expecting to handle explicit `null` values, you | ||
| now need to explicitly declare this with `Maybe[T | None]`: | ||
|
|
||
| ```python | ||
| # Before (0.278.0 and earlier) | ||
| field: strawberry.Maybe[str] # Could handle Some(None) | ||
|
|
||
| # After (0.279.0+) | ||
| field: strawberry.Maybe[str] # Only handles Some("value") or None (absent) | ||
| field: strawberry.Maybe[str | None] # Handles Some("value"), Some(None), or None | ||
| ``` | ||
|
|
||
| ### Runtime Behavior | ||
|
|
||
| The runtime behavior changes to provide more consistent field checking: | ||
|
|
||
| - `Maybe[str]` now represents "field present with non-null value" or "field | ||
| absent" | ||
| - `Maybe[str | None]` represents "field present with value", "field present but | ||
| null", or "field absent" | ||
|
|
||
| This means `Maybe[str]` can no longer receive explicit `null` values - they will | ||
| cause a validation error. | ||
|
|
||
| ### Consistent Field Checking | ||
|
|
||
| This change provides a single, consistent way to check if a field was provided, | ||
| regardless of whether the field allows null values: | ||
|
|
||
| ```python | ||
| @strawberry.input | ||
| class UpdateUserInput: | ||
| # Can be provided with a value or not provided at all | ||
| name: strawberry.Maybe[str] | ||
|
|
||
| # Can be provided with a value, provided as null, or not provided at all | ||
| phone: strawberry.Maybe[str | None] | ||
|
|
||
|
|
||
| @strawberry.mutation | ||
| def update_user(input: UpdateUserInput) -> User: | ||
| # Same checking pattern for both fields | ||
| if input.name is not None: # Field was provided | ||
| user.name = input.name.value # Type checker knows this is str | ||
|
|
||
| if input.phone is not None: # Field was provided | ||
| user.phone = input.phone.value # Type checker knows this is str | None | ||
| ``` | ||
|
|
||
| The key benefit is that `if field is not None` now consistently means "field was | ||
| provided" for all Maybe fields. | ||
|
|
||
| ## Migration | ||
|
|
||
| ### Automatic Migration | ||
|
|
||
| Strawberry provides a codemod to automatically update your code: | ||
|
|
||
| ```bash | ||
| strawberry upgrade maybe-optional | ||
| ``` | ||
|
|
||
| The codemod will automatically convert `Maybe[T]` to `Maybe[T | None]` to | ||
| maintain the previous behavior. | ||
|
|
||
| ### Manual Migration | ||
|
|
||
| Review your `Maybe` usage and decide whether you need the union type: | ||
|
|
||
| ```python | ||
| # If you only need "present" vs "absent" (most common case) | ||
| field: strawberry.Maybe[str] | ||
|
|
||
| # If you need "present with value", "present but null", and "absent" | ||
| field: strawberry.Maybe[str | None] | ||
| ``` | ||
|
|
||
| ## Why This Change? | ||
|
|
||
| After extensive discussion, we decided that changing `Maybe` to `Some[T] | None` | ||
| (instead of `Some[T | None] | None`) provides the best developer experience | ||
| because: | ||
|
|
||
| 1. **Consistent field checking**: You can always use `if field is not None:` to | ||
| check if a field was provided, regardless of whether the field allows null | ||
| values | ||
|
|
||
| 2. **Preserves existing behavior**: You can still get the previous behavior by | ||
| using `Maybe[T | None]` when you need to handle explicit nulls | ||
|
|
||
| 3. **Better type safety**: `Maybe[str]` now properly indicates that the field | ||
| cannot be null, while `Maybe[str | None]` explicitly allows nulls | ||
|
|
||
| 4. **Cleaner API**: There's now "one true way" to check if a field was provided, | ||
| making the API more intuitive | ||
|
|
||
| The new approach provides both simplicity for common cases and flexibility for | ||
| complex scenarios where null handling is needed. | ||
|
|
||
| ## Need Help? | ||
|
|
||
| - See the [Maybe documentation](../types/maybe.md) for comprehensive usage | ||
| examples | ||
| - Check the | ||
| [migration guide](../types/maybe.md#migration-from-previous-versions) for | ||
| detailed migration instructions | ||
| - If you encounter issues, please | ||
| [report them on GitHub](https://github.com/strawberry-graphql/strawberry/issues) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.