python / python/mypy

(🐞) Locally narrowed value used in a lambda parameter from an overloaded higher order function is analyzed twice with different types

Open
#12,727 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

from typing import overload, Callable

@overload
def f() -> None: ...
@overload
def f(fn: Callable[[], object]) -> None: ...

def f(fn: object=1) -> None: ...

x: object
assert isinstance(x, str)
y = x
f(lambda: reveal_type(x))  # Revealed type is "str", Revealed type is "object"
f(lambda: reveal_type(y))  # Revealed type is "str"

The lambda is analyzed twice, once with the local type of the variable, and once with the global type of the variable, duplicating all notes and error messages.

The second usage with y is only analyzed once because it does not have a narrowed type.

  • Related #11953

playground

Credit to @septatrix for unearthing this.

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 provided overload and lambda reproduction, either locally or in the linked playground, and compare the diagnostics for x and y. Trace how the overloaded higher-order call analyzes the lambda and verify that the narrowed x case produces one set of revealed types and messages, while the y case remains unchanged.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.