Skip to content

MyPy gives error “Missing return statement” even when all cases are tested #4223

Closed
@ostrokach

Description

@ostrokach

I haven't received any answers to the following StackOverflow post, so I thought I'd also post it here. I'm not sure if it's an actual bug or if it's just considered good practice to always have something like else: raise Exception at the end of if / elif block.


I am getting a MyPy error "Missing return statement", even when I check for all possible cases inside a function.

For example, in the following code, MyPy is still giving me an error "9: error: Missing return statement", even though color can only be Color.RED, Color.GREEN, or Color.BLUE, and I test all those cases!

class Color(enum.IntEnum):
    RED: int = 1
    GREEN: int = 2
    BLUE: int = 3


def test_enum(color: Color) -> str:
    if color == Color.RED:
        return "red"
    elif color == Color.GREEN:
        return "green"
    elif color == Color.BLUE:
        return "blue"

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions