mypy
mypy copied to clipboard
Mypy error "__ does not return a value" when using `lambda` with multiple expression
Bug Report
(A clear and concise description of what the bug is.)
To Reproduce
from typing import Callable, cast
import os
import re
import sys
import time
import click
import inquirer # type: ignore
import halo # type: ignore
################################################
@click.command()
def main():
options = [
inquirer.List("mainOption", message="What would you like to do?",
choices=[
("1. Option 1.", pick1),
("2. Option 2.", pick2),
("3. Quit the program...", lambda: (click.echo(click.style("Bye bye!", fg="magenta")), sys.exit(0)))
],
)
]
answer = inquirer.prompt(options)
selected_opt = cast(Callable[[], None], answer.get("mainOption"))
if selected_opt is not None:
selected_opt()
########################
def pick1() -> None:
click.echo("Select opt 1")
def pick2() -> None:
click.echo("Select opt 2")
################################################
if __name__ == "__main__":
main()
Error on line:
# "echo" does not return a value
("3. Quit the program...", lambda: (click.echo(click.style("Bye bye!", fg="magenta")), sys.exit(0)))
If I drop an expression for lambda, everything is fine:
# No error
("3. Quit the program...", lambda: (click.echo(click.style("Bye bye!", fg="magenta"))))
Your Environment
- Mypy version used: Mypy Type Checker v2023.6.0 "A Visual Studio Code extension with support for the Mypy type checker. This extension ships with mypy=1.6.1."
- Mypy command-line flags:
- Mypy configuration options from
mypy.ini(and other config files): - Python version used: 3.12.0
Evidently this is intentional behavior: https://github.com/python/mypy/issues/6549#issuecomment-538531013
I would also point out that lambda: (click.echo(click.style("Bye bye!", fg="magenta")), sys.exit(0)) is inconsistent with the signature Callable[[], None]. The lambda really returns tuple[None, Any]. That's why mypy stops complaining when you remove sys.exit(0).