Skip to content

Mypy error "__ does not return a value" when using lambda with multiple expression #17364

@KiddoV

Description

@KiddoV

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"))))

Untitled

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions