python / python/mypy

confusing type error when using overloads

Aperta
#14,976 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia eseguendo la riproduzione collegata di mypy-play e confronta la chiamata a overload alla riga 65 di main.py con parse_other alla riga 68. Traccia il modo in cui la risoluzione di overload seleziona le diagnosi; il lavoro è completo quando l’argomento errato viene segnalato con il messaggio diretto di tipo incompatibile, preservando al contempo gli errori di overload utili per le discrepanze effettive.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.