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

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

Closed
J-Westin opened this issue Jul 5, 2024 · 2 comments

Comments

@J-Westin
Copy link

J-Westin commented Jul 5, 2024

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)?

@J-Westin J-Westin changed the title MaybeNone in requests.Request.content type causes mypy to complain when disallow_any_expr is enabled MaybeNone in Response.content type in requests library causes mypy to fail with disallow_any_expr enabled Jul 5, 2024
@JelleZijlstra
Copy link
Member

This is unfortunate but intended. We use MaybeNone in cases where something could possibly be None but we don't want to force all users to check for it.

Mypy's disallow_any_expr is a rarely used option that is extremely difficult to use with many common code patterns, so it's acceptable for us to introduce Any in cases like this.

A more general solution could be python/typing#1096, but that idea isn't currently getting pursued.

As far as I know, #10575 is the only instance of MaybeNone usage so far

We also use it for sys.stdin, stdout, and stderr.

@Avasam
Copy link
Collaborator

Avasam commented Jul 9, 2024

As far as I know, #10575 is the only instance of MaybeNone usage so far

We also use it for sys.stdin, stdout, and stderr.

Also because we're having to wait for mypy 1.11 before using it in 3rd party stubs (replacing -> Any | None that have the same semantic).

You can read more about "The Any trick" here: https://github.com/python/typeshed/blob/main/CONTRIBUTING.md#the-any-trick

@srittau srittau closed this as not planned Won't fix, can't repro, duplicate, stale Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants