mypy icon indicating copy to clipboard operation
mypy copied to clipboard

(🐞) Type becomes `Any` when context managers type is inferred after the usage

Open KotlinIsland opened this issue 3 years ago • 0 comments

from typing import Iterator, Callable
from contextlib import contextmanager

class E:
    a: str


def f(e: E) -> None:
    with cm():
        if e.a:
            pass

    reveal_type(e.a)  # Revealed type is "builtins.str"

    def f2() -> None:
        pass

    reveal_type(e.a)  # Revealed type is "Any"  # Revealed type is "builtins.str"

@contextmanager
def cm() -> Iterator[None]:
    yield

playground

KotlinIsland avatar Jan 16 '23 03:01 KotlinIsland