python / python/mypy

Cannot infer type of lambda regression with mypy 1.9

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

Nobody has claimed this yet.

bug topic-pep-646
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

https://github.com/python/mypy/issues/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

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 running the supplied reproducer with mypy 1.8.0 and 1.9.0 using --strict, then compare the behavior in the linked mypy playground. Investigate the lambda type-inference regression shown in the example; done means the current version no longer reports Cannot infer type of lambda for this code.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.