Skip to content

typing.overload and contextlib.contextmanager don't work together #11373

Closed
@bentheiii

Description

@bentheiii

Bug Report

The following example:

from contextlib import contextmanager
from typing import *

T = TypeVar('T')

@overload
def foo(a:int)->ContextManager[int]:...

@overload
def foo(a:str)->ContextManager[str]:...


@contextmanager
def foo(a: T)->Iterator[T]:
    yield a

f: ContextManager[int] = foo(1)

produces the following typing errors:

main.py:10: error: Overloaded function implementation cannot produce return type of signature 1
main.py:10: error: Overloaded function implementation cannot produce return type of signature 2
Found 2 errors in 1 file (checked 1 source file)

without the overloadings, mypy recognizes that foo(1) returns a ContextManager[int]

To Reproduce

  1. copy the above snipped
  2. run mypy, note the error
  3. comment out the overloads and their functions
  4. run mypy again, and note no errors

Expected Behavior

since the overload is correct for the implementation, mypy should pass in both cases.

Actual Behavior

mypy fails for the first case but passes for the second.

Your Environment

reproduced on current mypy-playground

  • Mypy version used: 0.910
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.10
  • Operating system and version: mypy-playground

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions