Skip to content

Type signature is overbroad for Response.get_json #2508

@dzcode

Description

@dzcode

The type signature for Response.get_json is currently:

    def get_json(self, force: bool = False, silent: bool = False) -> t.Optional[t.Any]:

However, if silent is False, as is the default, this is overbroad, since it will always return t.Any. This is problematic in calling code, since you'll get a type checking error if you do something like this:

error: Value of type "Optional[Any]" is not indexable  [index]
                data = rv.get_json()["data"]

This can be fixed by using an overload from the typing module when silent is the Literal[False]

Environment:

  • Python version: 3.9.6
  • Werkzeug version: 2.1.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions