Skip to content

Unexpected "need type type annotation" with contextmanager  #11704

Closed

Description

@JukkaL

This generates a false positive in g:

from typing import Generator, Callable, Iterator, TypeVar, ContextManager, Any
from typing_extensions import ParamSpec

from nonexistent import deco  # type: ignore

_T = TypeVar("_T")
_P = ParamSpec("_P")
_T_co = TypeVar("_T_co", covariant=True)
_F = TypeVar("_F", bound=Callable[..., Any])

class _GeneratorContextManager(ContextManager[_T_co]):
    def __call__(self, func: _F) -> _F: ...

def contextmanager(
    func: Callable[_P, Iterator[_T]]) -> Callable[_P, _GeneratorContextManager[_T]]: ...

@contextmanager
@deco
def f() -> Generator[str, None, None]:
    pass

def g() -> Generator[str, None, None]:
    with f() as x:  # <<<--------- Need type annotation for "x"
        yield x

This is a regression so it's high priority. It was introduced by me in #11594.

Activity

  1. self-assigned this
    on Dec 13, 2021
  2. added a commit that references this issue on Dec 13, 2021
    8f64758
  3. added a commit that references this issue on Dec 15, 2021
    22ab84a
  4. added a commit that references this issue on Jan 20, 2022
    ea5e8b9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions