Skip to content

MaybeNone in Response.content type in requests library causes mypy to fail with disallow_any_expr enabled #12280

Closed as not planned
@J-Westin

Description

@J-Westin

I'm working on a project that uses the requests library and fairly strict mypy settings, including disallow_any_expr=True. After the integration of #10575, which sets the type of Response.content to bytes | MaybeNone (with MaybeNone being an alias of Any), the mypy check on my project fails.

Here is a minimal example:

# any_content.py
import requests

def print_some_content() -> None:
    response = requests.get("https://www.google.com/")

    if response.status_code == 200:
        print(response.content)

Running the check:

pip install mypy requests types-requests && mypy any_content.py --disallow-any-expr

produces the followng mypy output:

any_content.py:8: error: Expression type contains "Any" (has type "Union[bytes, Any]")  [misc]
Found 1 error in 1 file (checked 1 source file)

Downgrading types-requests from version 2.32.0.20240622 to 2.32.0.20240602 removes this error, confirming it is caused by #10575.

As far as I know, #10575 is the only instance of MaybeNone usage so far, so I can't be sure if this is the intended behaviour. Is there a reason we are marking Response.content with an alias of Any rather than something that resolves to None in mypy? If this is the intended behaviour, how am I supposed to handle this Any value (other than # type: ignore)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions