python / python/mypy

confusing type error when using overloads

Open
#14,976 0 comments 0 reactions 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

Bug Report

when using overloads, the type errors for mistakes are confusingly hidden

https://mypy-play.net/?mypy=latest&python=3.11&gist=bd18c94e9aa3e0eb5bbd58d670a1f166

from __future__ import annotations
from typing import overload, Callable, TypeVar
import subprocess
import logging
log = logging.getLogger(__name__)


PARSE_RESULT= TypeVar("PARSE_RESULT")
T = TypeVar("T")



@overload
def parse_success(
    res: subprocess.CompletedProcess[str],
    *,
    parse: Callable[[str], PARSE_RESULT],
    default: None = None,
    error_msg: str | None = None,
) -> PARSE_RESULT | None:
    ...


@overload
def parse_success(
    res: subprocess.CompletedProcess[str],
    *,
    parse: Callable[[str], PARSE_RESULT],
    default: T,
    error_msg: str | None = None,
) -> PARSE_RESULT | T:
    ...




def parse_success(
    res: subprocess.CompletedProcess[str],
    *,
    parse: Callable[[str], PARSE_RESULT],
    default: T | None = None,
    error_msg: str | None = None,
) -> PARSE_RESULT | T | None:
    if res.returncode:
        if error_msg:
            log.warning("%s %s", error_msg, res)
        return default
    else:
        return parse(res.stdout)
        
        
def parse_other(
    res: subprocess.CompletedProcess[str]) -> str:
    return res.stdout
        
        
example = subprocess.CompletedProcess("example", 0, "wrong", "ignored")
    
def myparse(data: str) -> int|None:
    try:
        return int(data)
    except Exception: #diaper
       return None
       
parse_success((example,), parse=myparse, error_msg=":)")
        

parse_other((example,))


        

Expected Behavior

main.py:65: error: Argument 1 to "parse_success" has incompatible type "Tuple[CompletedProcess[str]]"; expected "CompletedProcess[str]"  [arg-type]
main.py:68: error: Argument 1 to "parse_other" has incompatible type "Tuple[CompletedProcess[str]]"; expected "CompletedProcess[str]"  [arg-type]
Found 2 errors in 1 file (checked 1 source file)

Actual Behavior

main.py:65: error: No overload variant of "parse_success" matches argument types "Tuple[CompletedProcess[str]]", "Callable[[str], Optional[int]]", "str"  [call-overload]
main.py:65: note: Possible overload variants:
main.py:65: note:     def [PARSE_RESULT] parse_success(res: CompletedProcess[str], *, parse: Callable[[str], PARSE_RESULT], default: None = ..., error_msg: Optional[str] = ...) -> Optional[PARSE_RESULT]
main.py:65: note:     def [PARSE_RESULT, T] parse_success(res: CompletedProcess[str], *, parse: Callable[[str], PARSE_RESULT], default: T, error_msg: Optional[str] = ...) -> Union[PARSE_RESULT, T]
main.py:68: error: Argument 1 to "parse_other" has incompatible type "Tuple[CompletedProcess[str]]"; expected "CompletedProcess[str]"  [arg-type]
Found 2 errors in 1 file (checked 1 source file)


Your Environment

replicated this in the playground on 1.1.1

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 linked mypy-play reproduction and compare the overload call at main.py line 65 with parse_other at line 68. Trace how overload resolution selects diagnostics; done means the incorrect argument is reported with the direct incompatible-type message while preserving useful overload errors for genuine mismatches.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.