strawberry icon indicating copy to clipboard operation
strawberry copied to clipboard

UNSET default value does not work for optional fields when using strawberry.field()

Open pavelbrych opened this issue 1 year ago • 1 comments

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

pavelbrych avatar Sep 15 '22 10:09 pavelbrych

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)

patrick91 avatar Sep 15 '22 11:09 patrick91

Duplicate of #439, and fixed in #2128.

coady avatar Oct 18 '22 01:10 coady