Refer to the example below.
A route handler annotated to return Data | None
, along with return_dto=DataDTO
being set will raise AttributeError
if None
is returned - if the DTO config partial
attribute is False
. The same goes for returning an empty dict, except this will raise a KeyError
instead.
A workaround is to define a duplicate DTO with partial
set True
.
class FooModel(Struct):
bar: str
class FooDTO(MsgspecDTO[FooModel):
config = DTOConfig(partial=False) # default but okay
@get("/foo", return_dto=FooDTO)
def get_foo() -> FooModel | None:
# some condition evaluates `foo` to `None`
return None # raises AttributeError: `NoneType` has no attribute 'bar'
This enhancement might cause issues for users following this pattern and actively catching attribute or key errors. People are crazy.
No response
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too