Skip to content

Incorrect 'Implicit return in function which does not return' when using 'contextlib.contextmanager' #8401

Closed
@tomwatson1024

Description

@tomwatson1024

Consider the following snippet.

import contextlib
import sys
from typing import Iterator, NoReturn

def foo() -> NoReturn:
    with bar():
        pass
    sys.exit(0)


@contextlib.contextmanager
def bar() -> Iterator[None]:
    yield None

Running mypy produces

error: Implicit return in function which does not return  [misc]
    def foo() -> NoReturn:
    ^

Hand-writing the context manager (as follows) does not produce the same error.

class Bar:
    def __enter__(self) -> None:
        return None

    def __exit__(self, *args: Any) -> None:
        pass

I think the error in the contextlib case is a false positive.

mypy version 0.761 and 0.770+dev.a07dbd00638c35729e30dfc4ae441a8985183563 (current master)
Python version 3.6.9
flags --ignore-missing-imports --check-untyped-defs --disallow-untyped-defs --no-implicit-optional --warn-unused-ignores

May be related to #8129.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions