python / python/mypy

mypy failed to return correct overloaded func type when decorate func accepts more than one parameter

Aperta
#18,167 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug Report

mypy failed to return the correct overloaded func type when decorate func accepts more than one parameter.

To Reproduce

from functools import wraps
from typing import overload, Any, TypeVar, Callable, Type

from typing_extensions import ParamSpec, reveal_type


@overload
def foo(a: str, /) -> str:
    pass


@overload
def foo(a: int, /) -> int:
    pass


def foo(a: Any) -> Any:
    return a


P = ParamSpec("P")
T = TypeVar("T")
R = TypeVar("R")


def forbid_return_type(
    func: Callable[P, T],
    tp: Type[Any],
    /,
) -> Callable[P, T]:
    @wraps(func)
    def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
        result = func(*args, **kwargs)

        if isinstance(result, tp):
            raise TypeError(f"Return type {tp} is forbidden")

        return result

    return wrapper


foo_as_int = forbid_return_type(foo, str)

reveal_type(foo_as_int(1))
reveal_type(foo_as_int("1"))

Expected Behavior

note: Revealed type is "builtins.int"
note: Revealed type is "builtins.str"

Actual Behavior

note: Revealed type is "builtins.str"
error: Argument 1 has incompatible type "int"; expected "str"  [arg-type]
note: Revealed type is "builtins.str"

Your Environment

  • Mypy version used: mypy 1.14.0+dev
  • Mypy command-line flags: -
  • Mypy configuration options from mypy.ini (and other config files): -
  • Python version used: 3.8.19

Both codes bellow work as expected:

from functools import wraps
from typing import overload, Any, TypeVar, Callable

from typing_extensions import ParamSpec, reveal_type


@overload
def foo(a: str, /) -> str:
    pass


@overload
def foo(a: int, /) -> int:
    pass


def foo(a: Any) -> Any:
    return a


P = ParamSpec("P")
T = TypeVar("T")
R = TypeVar("R")


def forbid_return_type(
    func: Callable[P, T],
) -> Callable[P, T]:
    @wraps(func)
    def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
        result = func(*args, **kwargs)
        return result

    return wrapper


foo_as_int = forbid_return_type(foo)

reveal_type(foo_as_int(1))
reveal_type(foo_as_int("1"))
from functools import wraps
from typing import Any, TypeVar, Callable, Type, Union

from typing_extensions import ParamSpec, reveal_type


def foo(a: Union[int, str]) -> Union[int, str]:
    return a


P = ParamSpec("P")
T = TypeVar("T")
R = TypeVar("R")


def forbid_return_type(
    func: Callable[P, T],
    tp: Type[Any],
    /,
) -> Callable[P, T]:
    @wraps(func)
    def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
        result = func(*args, **kwargs)

        if isinstance(result, tp):
            raise TypeError(f"Return type {tp} is forbidden")

        return result

    return wrapper


foo_as_int = forbid_return_type(foo, str)

reveal_type(foo_as_int(1))
reveal_type(foo_as_int("1"))

So it's not working only when the function accepts an overloaded function and another argument.

I checked with pyright and it correctly handles code.

  /Users/yuriikarabas/projects/mypy/temp.py:45:13 - information: Type of "foo_as_int(1)" is "int"
  /Users/yuriikarabas/projects/mypy/temp.py:46:13 - information: Type of "foo_as_int("1")" is "str"
0 errors, 0 warnings, 2 informations 

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 il riproduttore minimo con mypy e confronta i tipi rivelati con l’output previsto. Traccia l’inferenza degli overload e di ParamSpec per la funzione decorata quando il decoratore riceve il suo secondo argomento. Il lavoro è completo quando il riproduttore rivela int per foo_as_int(1) e str per foo_as_int("1") senza un errore di tipo dell’argomento.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.