Skip to content

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

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

Closed
tomwatson1024 opened this issue Feb 14, 2020 · 1 comment

Comments

@tomwatson1024
Copy link

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.

@ilevkivskyi
Copy link
Member

Yes, moving the definition of bar() before definition of foo() fixes the error, which means this is a duplicate of #8129

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants