Skip to content
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

Cannot infer type of lambda regression with mypy 1.9 #17018

Open
rbuffat opened this issue Mar 12, 2024 · 1 comment
Open

Cannot infer type of lambda regression with mypy 1.9 #17018

rbuffat opened this issue Mar 12, 2024 · 1 comment
Labels
bug mypy got something wrong topic-pep-646 PEP 646 (TypeVarTuple, Unpack)

Comments

@rbuffat
Copy link

rbuffat commented Mar 12, 2024

Bug Report

The lambda in the following code snippet reports error: Cannot infer type of lambda starting from mypy 1.9.0 (It did not report an error with mypy 1.8.0).

#15459 seems related, but it looks as mypy 1.8.0 also reported an error there.

To Reproduce

import asyncio
import signal


async def shutdown(
    signal: signal.Signals,
) -> None:
    print(signal.name)


async def main() -> None:
    loop = asyncio.get_event_loop()
    signals = (signal.SIGHUP, signal.SIGTERM, signal.SIGINT)
    for s in signals:
        loop.add_signal_handler(
            s,
            lambda s=s: asyncio.create_task(  #  <-- error: Cannot infer type of lambda  [misc]
                shutdown(
                    signal=s,
                ),
                name="shutdown",
            ),
        )

mypy playground

Expected Behavior

No error shown regarding the lambda.

Actual Behavior

mypy reports Cannot infer type of lambda [misc]

Your Environment

  • Mypy version used: 1.9.0
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files): -
  • Python version used: 3.11.8
@rbuffat rbuffat added the bug mypy got something wrong label Mar 12, 2024
@AlexWaygood AlexWaygood added the topic-pep-646 PEP 646 (TypeVarTuple, Unpack) label Mar 12, 2024
@AlexWaygood
Copy link
Member

Here's a mimimal repro of the underlying issue that doesn't involve typeshed's stubs for asyncio:

from typing import TypeVarTuple, Callable

x = 42
Ts = TypeVarTuple("Ts")

def foo(cb: Callable[[*Ts], object], *args: *Ts) -> None: ...
foo(lambda x=x: None)  # error: Cannot infer type of lambda  [misc]

https://mypy-play.net/?mypy=latest&python=3.12&gist=ce433643f1bff8c03f6f8149f7829793

(The immediate cause of the new false positive was python/typeshed@a08d2d5, but the underlying issue is mypy struggling to solve a TypeVarTuple when you're passing in a lambda.)

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-pep-646 PEP 646 (TypeVarTuple, Unpack)
Projects
None yet
Development

No branches or pull requests

2 participants