Skip to content

False positive unreachable statement after exception raised within context block #8766

Open
@PeterJCLaw

Description

@PeterJCLaw
import contextlib
from typing import Iterator

@contextlib.contextmanager
def context_func() -> Iterator[None]:
    try:
        yield
    except Exception:
        pass

def foo() -> None:
    with context_func():
        raise Exception

    print("xxx")  # mypy: Statement is unreachable

Since context managers can silence exceptions, I don't think mypy should consider statements after the context to be unreachable due to exceptions which might be raised within the context.

Flags needed: --warn-unreachable.


Python 3.6, mypy 0.770, also master (at 2a3de7b)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-reachabilityDetecting unreachable code

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions