Skip to content

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

Closed

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

Activity

  1. added 2 commits that reference this issue on Aug 16, 2023
    2409044
    8033909
  2. hauntsaninja commented on Aug 16, 2023

    @hauntsaninja
    Collaborator

    Opened a PR that should fix this. Are any of the people here encountering this in open source projects? I could use some more match statement coverage in https://github.com/hauntsaninja/mypy_primer

  3. added a commit that references this issue on Aug 19, 2023
    5af7671
  4. hauntsaninja commented on Aug 19, 2023

    @hauntsaninja
    Collaborator

    I've merged the fix, but if your project is open source, please let me know!

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions