8000 Mypy error "__ does not return a value" when using `lambda` with multiple expression · Issue #17364 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

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

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

Open
KiddoV opened this issue Jun 11, 2024 · 1 comment
Labels
bug mypy got something wrong

Comments

@KiddoV
Copy link
KiddoV commented Jun 11, 2024

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
@KiddoV KiddoV added the bug mypy got something wrong label Jun 11, 2024
@matteosantama
Copy link

Evidently this is intentional behavior: #6549 (comment)

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants
0