Skip to content

Type annotations: I think werkzeug.wrappers.Request.json can never be None #3067

Closed

Description

@oprypin

I am talking about this property: https://werkzeug.palletsprojects.com/en/stable/wrappers/#werkzeug.wrappers.Request.json

@property
def json(self) -> t.Any | None:

I think it can never actually return None, because it calls get_json() with all defaults, which is silent=False, making it never return None and raise in those cases instead.

This issue is still current in the latest release and latest unreleased code.

I am using Pyright, and it complains about usages such as flask.request.json["foo"].

Activity

  1. ThiefMaster commented on Nov 23, 2025

    @ThiefMaster
    Member

    w/o checking the code: What if the request payload is literally null which, when parsed as json, becomes None?

  2. oprypin commented on Nov 23, 2025

    @oprypin
    Author

    @ThiefMaster Uhh yes, that's technically true.
    However, the intentions of the type annotations around here are clearly the following:

    • Any means "any JSON type" (without special-casing null)
    • Any | None means the same, OR that it was not provided.

    You can see this logic applied in the definitions of get_json() right below.

  3. oprypin commented on Nov 23, 2025

    @oprypin
    Author

    Or from the pragmatic perspective:

    The code some_json["foo"] has many reasons to fail, and the situation where it's null should get equal treatment to the situation where it's a list instead.
    Preventing only the None case is not meaningful when the other case can still be let through.

  4. added this to the 3.1.4 milestone on Nov 27, 2025
  5. locked as resolved and limited conversation to collaborators on Dec 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions