UNSET
UNSET is considered legacy. For new code, we recommend using
Maybe instead, which provides better type safety and clearer
semantics for handling optional input fields.
UNSET is a sentinel value that can be used to represent an unset value in a
field or argument. Similar to undefined in JavaScript, this value can be used
to differentiate between a field that was not set and a field that was set to
None or null .
Basic Usage
import strawberry
@strawberry.inputclass UpdateUserInput: name: str | None = strawberry.UNSET phone: str | None = strawberry.UNSET
@strawberry.typeclass Mutation: @strawberry.mutation def update_user(self, input: UpdateUserInput) -> User: if input.name is not strawberry.UNSET: user.name = input.name if input.phone is not strawberry.UNSET: user.phone = input.phone return user Checking for UNSET
Use identity comparison to check if a value is unset:
# Correct way to checkif value is strawberry.UNSET: print("Value was not provided")
# Also correctif value is not strawberry.UNSET: print(f"Value was provided: {value}")
The is_unset() helper function has been deprecated. Use
value is strawberry.UNSET instead.
Limitations
UNSET has some limitations compared to Maybe :
- Type Safety:
UNSETdoesnβt work well with type checkers since itβs typed asAny - Ambiguous Null Handling: With
field: str | None = UNSET, itβs not immediately clear whetherNonemeans βset to nullβ or βnot providedβ - No Wrapper Type: Values are accessed directly, making it more difficult
to distinguish the source of a
Nonevalue
Migrating to Maybe
We recommend migrating to Maybe for new code. See the
migration guide for details.
Quick Comparison
| Aspect | UNSET | Maybe |
|---|---|---|
| Check absent | value is strawberry.UNSET | value is None |
| Access value | value (direct) | value.value (via Some) |
| Type annotation | T | None = UNSET | Maybe[T] or Maybe[T | None] |
| Null handling | Implicit | Explicit with T | None |
Related
- Maybe - The recommended alternative for input field handling
- Input Types - Defining input types