python / python/mypy

type without arguments becomes type[nothing] instead of type[Any]

Aperta
#16,001 0 commenti 2 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

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.11&gist=07773fc6948673f0c4279ecf835537cc

from typing import Any, TypeVar, assert_type

T = TypeVar("T")

def fn(x: type[T]) -> list[T]:
    return []

def test(x: type) -> None:
    assert_type(fn(x), list[Any])

Expected Behavior

I expect this to work. According to PEP 484: https://peps.python.org/pep-0484/#the-type-of-class-objects

Plain Type without brackets is equivalent to Type[Any] and this in turn is equivalent to type (the root of Python’s metaclass hierarchy).

Actual Behavior

main.py:9: error: Expression is of type "list[<nothing>]", not "list[Any]"  [assert-type]

The type parameter for type became nothing instead of Any. However, if I explicitly use type[Any] this works as expected:

def test(x: type[Any]) -> None:
    assert_type(fn(x), list[Any])

It also works if I use typing.Type without arguments instead of type.

Your Environment

  • Mypy version used: 1.5.1
  • Python version used: 3.11

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 con la riproduzione mypy-play collegata e analizza come viene gestito un tipo integrato non parametrizzato durante l'inferenza della variabile di tipo in fn. La correzione è completata quando la riproduzione accetta assert_type(fn(x), list[Any]) preservando il comportamento esistente per type[Any] esplicito e typing.Type.

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

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
52/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.