Skip to content

Switch to explicit typing.Optional throughout #2092

Closed

Description

@michaeloliverx

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.

Activity

  1. changed the title [-]Issue with Mypy `--strict` and ``[/-] [+]Issue with Mypy `--strict` and `AsyncExitStack`[/+] on Feb 23, 2022
  2. florimondmanca commented on Feb 26, 2022

    @florimondmanca
    Contributor

    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.

  3. lovelydinosaur commented on Feb 28, 2022

    @lovelydinosaur
    Member

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

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

  4. changed the title [-]Issue with Mypy `--strict` and `AsyncExitStack`[/-] [+]Switch to explicit typing.Optional throughout[/+] on Feb 28, 2022
  5. lovelydinosaur commented on Feb 28, 2022

    @lovelydinosaur
    Member

    See also #1760, #1747

  6. nhoad commented on Mar 4, 2022

    @nhoad

    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.

  7. florimondmanca commented on Mar 5, 2022

    @florimondmanca
    Contributor

    @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

Metadata

Metadata

Assignees

No one assigned

    Labels

    user-experienceEnsuring that users have a good experience using the library

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions