You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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)?
The text was updated successfully, but these errors were encountered:
J-Westin
changed the title
MaybeNone in requests.Request.content type causes mypy to complain when disallow_any_expr is enabledMaybeNone in Response.content type in requests library causes mypy to fail with disallow_any_expr enabled
Jul 5, 2024
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
I'm working on a project that uses the
requests
library and fairly strict mypy settings, includingdisallow_any_expr=True
. After the integration of #10575, which sets the type ofResponse.content
tobytes | MaybeNone
(withMaybeNone
being an alias ofAny
), the mypy check on my project fails.Here is a minimal example:
Running the check:
pip install mypy requests types-requests && mypy any_content.py --disallow-any-expr
produces the followng mypy output:
Downgrading
types-requests
from version2.32.0.20240622
to2.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 markingResponse.content
with an alias ofAny
rather than something that resolves toNone
in mypy? If this is the intended behaviour, how am I supposed to handle thisAny
value (other than# type: ignore
)?The text was updated successfully, but these errors were encountered: