Join of actual Class with Type[Class] results in `object`

Aperta
#3,976 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

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

Direzione di ricerca

Start with the provided Python reproducer and trace mypy's handling of the conditional-expression type join involving the actual Foo class and Type[Foo]. Run the example to confirm the revealed type and call error; done means the expression is inferred as Type[Foo], Foo can be called, and the existing reversed-order and cls or Foo cases remain covered.

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

Descrizione

bug priority-1-normal

This file:

from typing import Type, Optional

class Foo:
    pass

def make_foo(cls: Optional[Type[Foo]] = None) -> Foo:
    x = Foo if cls is None else cls
    reveal_type(x)
    return x()

gives the following:

typeofclass.py:10: error: Revealed type is 'builtins.object' 
typeofclass.py:11: error: "object" not callable 

This example is superficially related to #3487, in that it uses an if/else expression to make mypy perform a type join, but the distinct bug here is that joining Foo (the actual class object) with Type[Foo] should result in Type[Foo], but instead results in object.

This is related to the fact that class objects are typed as callables instead of as Type[Class].

Oddly reversing Foo if cls is None else cls to cls if cls is not None else Foo results in builtin.type instead of object, which is still wrong, but differently wrong.

Using cls or Foo results in Type[Foo].

Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Merge medio
1g 18h
PR unite (30g)
54

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.

Altre issue di python/mypy

Tutte le issue di python/mypy

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.