python / python/mypy

TypeVar with Type-based constraints misbehaving when used to annotate *args

Offen
#9,569 0 Kommentare 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

It seems that the below combination of *args, and TypeVar with constraints related to Type, is misbehaving. I reached out on the Gitter and was instructed that this is probably a bug.

I tried replacing the Types with ints (observable in the linked mypy-playground) and that seems to work fine.

To Reproduce

from typing import TypeVar, List, Type


TypeArgT = TypeVar("TypeArgT", Type, List[Type])

def type_a(*args: TypeArgT):
    ...
    
    
def type_b(*args: List[Type]):
    ...
    
type_a(int, str, float)             # test t1
type_a([str, float], [int])         # test t2   <-- this does not work

type_b([bool, bool], [str, bool])   # test t3

https://mypy-play.net/?mypy=0.780&python=3.8&gist=847224369f6bad4d74a1ccfd672fb250

Actual Behavior

Test t2 in the above snippet throws the following mypy error:

main.py:14: error: Value of type variable "TypeArgT" of "type_a" cannot be "object"
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.790
  • Python version used: 3.8

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

Keine Projektdatei oder kein Test wird genannt. Beginne damit, die Reproduktion mit mypy 0.790 auszuführen, und verfolge dann die Inferenz für eingeschränkte TypeVar-Werte, die mit *args verwendet werden; abgeschlossen ist es, wenn t2 akzeptiert wird, ohne das bestehende Verhalten von t1 und t3 zu ändern.

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

Neue Issues direkt in Ihr Postfach

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