python / python/mypy

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

Aperta
#9,569 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

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

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

Non viene indicato alcun file di progetto né alcun test. Inizia eseguendo la riproduzione con mypy 0.790, quindi traccia l’inferenza dei valori di TypeVar vincolati usati con *args; il lavoro è completato quando t2 viene accettato senza modificare il comportamento esistente di t1 e t3.

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.