strawberry
strawberry copied to clipboard
UNSET default value does not work for optional fields when using strawberry.field()
When using UNSET to differentiate between null/None and value not set, everything works when setting UNSET as default directly:
title: typing.Optional[str] = UNSET
but when using strawberry.field(), so we can add field description:
title: typing.Optional[str] = strawberry.field(description="Item title", default=UNSET)
request throws error:
"SampleClass.__init__() missing 1 required positional argument: 'title'"
Probably because StrawberryField class converts default argument from UNSET to dataclasses.MISSING, which does not behave the same.
edit: using title: typing.Optional[str] = dataclasses.MISSING
works without error, so my assumption about this conversion beeing the problem may be wrong.
Strawberry version 0.130.4 Python 3.10
Hi @pavelbrych, this is a bug, if needed the current workaround is this:
title: typing.Optional[str] = strawberry.field(description="Item title", default_factory=lambda: UNSET)
Duplicate of #439, and fixed in #2128.