python / python/mypy

"Overloaded function implementation does not accept all arguments" error when using TypeVar with restricted types.

Aperta
#17,614 1 commento 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

I believe this may be similar to

https://github.com/python/mypy/issues/11004: mypy thinks **kwargs: Union[None, int, str] overload isn't accepted by **kwargs: Any
https://github.com/python/mypy/issues/9420: mypy thinks Type[T1] isn't accepted by Union[Type[T1], Type[T2]]
https://github.com/python/mypy/issues/9023: same bug with two different type variables
https://github.com/python/mypy/issues/11391: Overloaded function implementation does not accept all possible arguments

When using a TypeVar with restricted values in an overload for a function signature which does not contain generics, mypy seems to indicate an error where there shouldn't be one.

To Reproduce

from typing import TypeVar, Sequence, overload

T = TypeVar("T", int, str)

@overload
def foo(a: Sequence[int]):
    ...

@overload
def foo(a: Sequence[T]):
    ...

def foo(a: Sequence[int] | Sequence[str]):
    ...

Expected Behavior

Because T is restricted to be either an int or a str (or some subtype of either of them, but not of int | str), the upper-bounds for Sequence[T] in overload signature 2 should be Sequence[int] or Sequence[str]. The implementation accepts either a Sequence[int] or Sequence[str] for parameter a, so either of the overload's upper-bounds would satisfy the acceptable input types to the implementation.

Someone please tell me I'm just being a smoothbrain here, I've spent the last hour narrowing down my actual code to this small reproducible example and it feels like I'm just doing something dumb or I don't understand TypeVars even after reading all the documentation on them, but I can't see it.

Actual Behavior

mypycheck.py:13: error: Overloaded function implementation does not accept all possible arguments of signature 2  [misc]

Your Environment

  • Mypy version used: 1.6.1 (compiled: yes) AND 1.11.1. Tested both.
  • Mypy command-line flags: --config=mypy.ini
  • Mypy configuration options from mypy.ini (and other config files):
[mypy]
python_version = 3.12
cache_dir = .mypy_cache

allow_redefinition = true
check_untyped_defs = true
disallow_any_generics = true
implicit_reexport = false
no_implicit_optional = true
scripts_are_modules = true
strict_equality = true
warn_redundant_casts = true
warn_return_any = true
warn_unused_configs = true
warn_unused_ignores = true
warn_unreachable = true

show_error_context = true
show_column_numbers = true
show_error_codes = true
pretty = true
  • Python version used: 3.12.4

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 l'esempio minimo in mypycheck.py con mypy 1.11.1 e la configurazione Python 3.12 indicata. Traccia il controllo di compatibilità dell'implementazione di overload per i TypeVars vincolati. Il lavoro è completato quando l'esempio non segnala più l'errore e un test di regressione copre questo caso.

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

Valutazione

Stack tecnologico
python
Ambito
compilers, 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.