Skip to content

Guard clause prevents match from seeing that all paths return #12534

Closed
@bkovitz

Description

@bkovitz

Bug Report

In a match statement in which all cases include a return statement, mypy 0.942 falsely reports "Missing return statement [return]" if at least one case includes a guard clause.

To Reproduce

def func(e) -> int:
    match e:
        case x if True:
            return x
        case _:
            return 0

Or even more simply:

def func(e) -> int:
    match e:
        case _ if True:
            return 0

Expected Behavior

This code should type-check.

Actual Behavior

$ mypy --python-version 3.10 --show-error-codes x5.py
x5.py:1: error: Missing return statement  [return]
Found 1 error in 1 file (checked 1 source file)

Notes

If we change the first case to something that does not include a guard clause, like this:

def func(e) -> int:
    match e:
        case (x, _):
            return x
        case _:
            return 0

then mypy returns no errors.

Environment

  • Mypy version used: 0.942
  • Mypy command-line flags: --python-version 3.10 --show-error-codes
  • Mypy configuration options from mypy.ini (and other config files): (none)
  • Python version used: 3.10.2
  • Operating system and version: Ubuntu 18.04

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions