Skip to content
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

Enhancement: allow route handlers to return None if return_dto is set without configuring partial #3428

Open
LonelyVikingMichael opened this issue Apr 26, 2024 · 0 comments
Labels
Enhancement This is a new feature or request

Comments

@LonelyVikingMichael
Copy link
Contributor

LonelyVikingMichael commented Apr 26, 2024

Summary

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.

Basic Example

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'    

Drawbacks and Impact

This enhancement might cause issues for users following this pattern and actively catching attribute or key errors. People are crazy.

Unresolved questions

No response


Note

While we are open for sponsoring on GitHub Sponsors and
OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.

Check out all issues funded or available for funding on our Polar.sh dashboard

  • If you would like to see an issue prioritized, make a pledge towards it!
  • We receive the pledge once the issue is completed & verified
  • This, along with engagement in the community, helps us know which features are a priority to our users.
Fund with Polar
@LonelyVikingMichael LonelyVikingMichael added the Enhancement This is a new feature or request label Apr 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement This is a new feature or request
Projects
None yet
Development

No branches or pull requests

1 participant