python / python/mypy

False positive passing default valued arguments to lambda

Open
#14,932 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-type-context
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report
Using default valued arguments is a common trick to ensure that a lambda references the current loop value (https://stackoverflow.com/a/25314665/145289)

def f() -> None:
    for i in range(0, 10):
        print(list(filter(lambda m, x=i: m == x, [4, 3])))

Expected Behavior

Mypy should not report any error

Actual Behavior

But mypy reports a false positive
kk.py:3: error: Argument 1 to "filter" has incompatible type "Callable[[Any, Any], Any]"; expected "Callable[[int], TypeGuard[Any]]" [arg-type]

Your Environment

  • Mypy version used: 1.1.1
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.11.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

Reproduce the provided lambda and filter example with mypy 1.1.1 and Python 3.11. Trace how the callable is inferred when one lambda parameter has a default value, then add regression coverage for this case. Done means mypy accepts the example without the reported arg-type error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.