python / python/mypy

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

Offen
#17,614 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, das minimale Beispiel in mypycheck.py mit mypy 1.11.1 und der angegebenen Python-3.12-Konfiguration auszuführen. Verfolge die Kompatibilitätsprüfung der Overload-Implementierung für eingeschränkte TypeVars. Als abgeschlossen gilt die Aufgabe, wenn das Beispiel den Fehler nicht mehr meldet und ein Regressionstest diesen Fall abdeckt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers, devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.