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

Type signature is overbroad for Response.get_json #2508

Closed
dzcode opened this issue Sep 9, 2022 · 0 comments
Closed

Type signature is overbroad for Response.get_json #2508

dzcode opened this issue Sep 9, 2022 · 0 comments
Labels
Milestone

Comments

@dzcode
Copy link
Contributor

dzcode commented Sep 9, 2022

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
@dzcode dzcode mentioned this issue Sep 9, 2022
6 tasks
@davidism davidism added this to the 2.2.3 milestone Oct 12, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants