Skip to content

@api_view decorator doesn't narrow the type of Request.method #67

@vpavlenko

Description

@vpavlenko

@mkurnikov @intgr @sobolevn Thanks for you work! I guess my question is related to #60

I wonder if there's a way for the decorator api_view to propagate the constant http_method_names as Union of Literals attached to Request.method? Consider the following example:

@api_view(["GET", "POST"])
def my_view(request: Request) -> Response:
    reveal_type(request.method)
    if request.method == "GET":
        return Response({})

    elif request.method == "POST":
        return Response({})

mypy yields

views.py:2: error: Missing return statement
views.py:3: note: Revealed type is 'Union[builtins.str, None]'

Or how would you suppress this error?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions