10000 'Missing return statement' error occurring in the match statement in Python 3.10. · Issue #14704 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

'Missing return statement' error occurring in the match statement in Python 3.10. #14704

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

Closed
Supertonian opened this issue Feb 15, 2023 · 5 comments
Labels
bug mypy got something wrong topic-match-statement Python 3.10's match statement topic-reachability Detecting unreachable code

Comments

@Supertonian
Copy link

Bug Report

(A clear and concise description of what the bug is.)

To Reproduce

# Ideally, a small sample program that demonstrates the problem.
# Or even better, a reproducible playground link https://mypy-play.net/ (use the "Gist" button)

def to_bytes(value: Any) -> bytes:
    match value:
        case s if isinstance(s, str):
            return s.encode("utf-8")
        case b if isinstance(b, bytes):
            return b
        case _:
            raise ValueError('value cannot be expressed in bytes')

Expected Behavior

It should not raise error 'Missing return statement'

Actual Behavior

Raises error 'Missing return statement'.

Even changing raise inside case_ to return b"" still produces 'Missing return' error.

Your Environment

  • Mypy version used: 1.0.0
  • Mypy command-line flags: mypy
  • Mypy configuration options from mypy.ini (and other config files):
[tool.mypy]
disallow_any_generics = true
disallow_untyped_defs = true
exclude = ['pb2\.py$', 'pb2_grpc\.py$']
files = ["src", "tests"]
follow_imports = "silent"
ignore_missing_imports = true
python_version = "3.10"
  • Python version used: 3.10.9
@Supertonian Supertonian added the bug mypy got something wrong label Feb 15, 2023
@hauntsaninja hauntsaninja added topic-match-statement Python 3.10's match statement topic-reachability Detecting unreachable code labels Feb 15, 2023
@henryiii
Copy link
Contributor

It's unhappy with the guard:

def f(x: object) -> bool:
    match x:
        case {"a": a} if a:
            return True
        case _:
            return False
$ mypy --strict tmp.py
tmp.py:1: error: Missing return statement  [return]
Found 1 error in 1 file (checked 1 source file)

Mypy works if you remove the if a, but claims there is a missing return statement when that if a is present. Guards just allow fall through, so the final case does always trigger if the one above doesn't, there's no missing return statement.

@Supertonian
Copy link
Author

Is there a way to perform type checking using the match statement, without resorting to using the isinstance function inside the case statement? @henryiii

@henryiii
Copy link
Contributor

Not sure what that has to do with the missing return statement bug, but yes - match MyClass() (notice the parentheses) replaces isinstance. That was the motivating reason to introduce pattern matching, as it could replace most isinstance uses. (I guess knowing this does help avoid this bug if you are using isinstance in a guard, which shouldn't be necessary?)

@ilmari
Copy link
ilmari commented Jul 11, 2023

This looks like a duplicate of #12534

@JelleZijlstra JelleZijlstra closed this as not planned Won't fix, can't repro, duplicate, stale Jul 11, 2023
@henryiii
Copy link
Contributor

For completeness, here is a working version (and better version, match statements are supposed to avoid isinstance) of the code at the top that avoids #12534:

def to_bytes(value: Any) -> bytes:
    match value:
        case str(s):
            return s.encode("utf-8")
        case bytes(b):
            return b
        case _:
            raise ValueError('value cannot be expressed in bytes')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-match-statement Python 3.10's match statement topic-reachability Detecting unreachable code
Projects
None yet
Development

No branches or pull requests

5 participants
0