Skip to content

Type hint for Request.query_params does not match documentation #1708

Closed
@jmullercuber

Description

@jmullercuber

The API documentation for the Request class indicates the query_params may be MultiDict or None: https://aws.github.io/chalice/api.html#Request.query_params

However, the type hint associated with the class definition does not match the above:

chalice/chalice/app.pyi

Lines 77 to 78 in 8df8b59

class Request:
query_params = ... # type: Dict[str, str]

The impact of this issue is that the mismatch makes it more difficult for developers to use the API as their IDE will suggest one thing about query_params (they don't need to check for None values) while in reality they do.

A minimal example of such is below, with comments.

from chalice import Chalice, Response

DEFAULT_NAME = "world"

app = Chalice(app_name="hello-world")


@app.route("/", methods=["GET"])
def hello_world():
    # This part is not obvious given chalice's current type hints
    # `or {}` is a work-around to ensure params is Dict type if chalice sets it as None
    params = app.current_request.query_params or {}

    # works for Dict/MultiDict, but not NoneType
    name = params.get("name", DEFAULT_NAME)
    
    return {"message": f"hello {name}"}

This is an issue on the latest release 1.22.3

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions