Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ostrokach opened this issue Nov 8, 2017 · 4 comments

Comments

@ostrokach
Copy link

ostrokach commented Nov 8, 2017

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"
@elazarg
Copy link
Contributor

elazarg commented Nov 8, 2017

This code is fragile; if Color will change, it will silently break. The right way IMO is to add assert False at the end. This will also silence mypy.

To the point, I don't think it's worth the special casing inside mypy. If we'll ever have singleton types, it might be possible to make it work out of the box.

@ilevkivskyi
Copy link
Member

PEP 484 actually explicitly specifies that examples like this should be supported. I am not sure if we already have an issue for this, so I will keep this open.

@ilevkivskyi
Copy link
Member

Just for reference, the relevant section is https://www.python.org/dev/peps/pep-0484/#support-for-singleton-types-in-unions

jMyles added a commit to jMyles/nucypher that referenced this issue Nov 22, 2017
@ilevkivskyi
Copy link
Member

I think we can now track this in #6366, which is a more general issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants