python / python/mypy

(🐞) Overload matching fails when sub expression contains an unrelated error

Open
#16,186 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

from dataclasses import dataclass
from typing import Generic, _T as T, overload

@dataclass
class A(Generic[T]):
    it: T

@overload
def foo(x: A[int]) -> int: ...
@overload
def foo(x: A[str]) -> str: ...

reveal_type(foo(A(str(1 + ""))))  # int; expected str

I think that the solution would be to change the current logic in infer_overload_return_type to something like:

  • infer the types of the argument expressions in the call site without the callabletype typecontext
  • check each overload against the inferred types
  • If the types of the expressions match the types of the arguments, then the overload is matched.
  • check the call against the matching overload as normal to generate error messages

links ⛓

  • super issue of #16025
  • Related #16101

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 reading infer_overload_return_type and reproduce the provided example to observe how the unrelated expression error affects overload selection. Trace how argument expressions are inferred and how each overload is checked. Done means the example reveals str while preserving normal error reporting for genuinely invalid calls.

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.