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

Switch to explicit typing.Optional throughout #2092

Closed
michaeloliverx opened this issue Feb 22, 2022 · 5 comments · Fixed by #2096
Closed

Switch to explicit typing.Optional throughout #2092

michaeloliverx opened this issue Feb 22, 2022 · 5 comments · Fixed by #2096
Labels
user-experience Ensuring that users have a good experience using the library

Comments

@michaeloliverx
Copy link
Member

michaeloliverx commented Feb 22, 2022

See the following snippet:

from contextlib import AsyncExitStack

import httpx


async def main() -> None:
    async with AsyncExitStack() as stack:
        http_client = await stack.enter_async_context(httpx.AsyncClient())
        reveal_type(http_client)

Output with strict = false:

repro.py:9: note: Revealed type is "httpx._client.AsyncClient*"

Output with strict = true:

repro.py:8: error: Need type annotation for "http_client"  [var-annotated]
repro.py:8: error: Argument 1 to "enter_async_context" of "AsyncExitStack" has incompatible type "AsyncClient"; expected "AsyncContextManager[<nothing>]"  [arg-type]
repro.py:9: note: Revealed type is "Any"
Found 2 errors in 1 file (checked 1 source file)

I initially raised an issue at python/mypy#12226 but it looks to be an issue with httpx. They said

The annotations at

async def __aexit__(
need to be changed to Optional.

@michaeloliverx michaeloliverx changed the title Issue with Mypy --strict and `` Issue with Mypy --strict and AsyncExitStack Feb 23, 2022
@florimondmanca
Copy link
Member

Hi @michaeloliverx,

Yup, this seems correct and well scoped, I guess at this point a PR attempting to fix that type annotation to Optional and see if it resolves this item would be on point.

@florimondmanca florimondmanca added the user-experience Ensuring that users have a good experience using the library label Feb 26, 2022
@tomchristie
Copy link
Member

Perhaps we should re-title this as the more general case.

Something like "Switch to explicit typing.Optional throughout" (?)

@michaeloliverx michaeloliverx changed the title Issue with Mypy --strict and AsyncExitStack Switch to explicit typing.Optional throughout Feb 28, 2022
@tomchristie
Copy link
Member

See also #1760, #1747

@nhoad
Copy link

nhoad commented Mar 4, 2022

If no one has picked this up yet, I'm happy to take a crack at it.

I've been working on it a little bit and I've got the total number of errors from mypy --strict httpx down from 437 to 207, just by adding typing.Optional or adding None to some already existing typing.Union definitions (e.g. httpx._types.HeaderTypes).

Happy to continue the work and open a PR if people are interested.

@florimondmanca
Copy link
Member

florimondmanca commented Mar 5, 2022

@nhoad Hi, FYI it looks like there's initial work on this here: #2096

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
user-experience Ensuring that users have a good experience using the library
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants