Skip to content

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

Closed

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.

Activity

  1. ilevkivskyi commented on Feb 17, 2020

    @ilevkivskyi
    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

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