Skip to content

@depends modifies function return type when it's None #16

@FieryRMS

Description

@FieryRMS

MRE

@depends
def with_test():
    pass

@app.get("/test", status_code=status.HTTP_204_NO_CONTENT)
@with_test
async def test() -> None:
    pass

Error:

File ".venv\Lib\site-packages\fastapi\routing.py", line 757, in __init__
    assert is_body_allowed_for_status_code(status_code), (
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
AssertionError: Status code 204 must not have a response body

adding a print statement just before this assertion shows,

print(self, self.response_model)
APIRoute(path='/test', name='test', methods=['GET']) <class 'NoneType'>

Expected

APIRoute(path='/test', name='test', methods=['GET']) None

Concerned Code:

new_signature = original_signature.replace(
parameters=tuple(all_params.values()),
return_annotation=hints.get("return", original_signature.return_annotation),
)

Possible Fix

 new_signature = original_signature.replace( 
     parameters=tuple(all_params.values()), 
     return_annotation=original_signature.return_annotation, 
 ) 

Workaround

@depends
def with_test():
    pass

@router.get("/test", status_code=status.HTTP_204_NO_CONTENT, response_model=None)
@with_test
async def test() -> None:
    pass

or

@depends
def with_test():
    pass

@router.get("/test", status_code=status.HTTP_204_NO_CONTENT)
@with_test
async def test():
    pass

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions