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

Unexpected "need type type annotation" with contextmanager #11704

Closed
JukkaL opened this issue Dec 10, 2021 · 0 comments · Fixed by #11725
Closed

Unexpected "need type type annotation" with contextmanager #11704

JukkaL opened this issue Dec 10, 2021 · 0 comments · Fixed by #11725
Assignees
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-0-high

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Dec 10, 2021

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.

@JukkaL JukkaL added bug mypy got something wrong priority-0-high false-positive mypy gave an error on correct code labels Dec 10, 2021
@JukkaL JukkaL self-assigned this Dec 13, 2021
JukkaL added a commit that referenced this issue Dec 13, 2021
We didn't infer constraints from the return type.

Fixes #11704.
JukkaL added a commit that referenced this issue Dec 15, 2021
We didn't infer constraints from the return type.

Fixes #11704.
tushar-deepsource pushed a commit to DeepSourceCorp/mypy that referenced this issue Jan 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-0-high
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant