python / python/mypy

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

Open
#17,364 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the provided lambda example and compare it with the one-expression variant that does not report an error. Trace how mypy checks tuple expressions inside lambdas; done means the valid multi-expression callback is handled consistently without losing the relevant return-value diagnostic.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.