python / python/mypy

"Invalid base class" when inheriting from tuple of classes

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

Nessuno ha ancora preso questa issue.

feature
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

I appreciate there are some complexities around handling inheritance from dynamic sources, so it's possible that this is a duplicate of one of those (#4284 seems most likely related), though I think this is a slightly different case.

I would like the following to pass type checking, yet I get an "Invalid base class" error even after adding an explicit type annotation.

Because in this case the tuple is of a fixed size (and the members of the tuple are all valid types) it feels like it should be possible to determine that it's valid to inherit from an expansion of that tuple.

# foo.py
from typing import Tuple, Type

class A: pass
class B: pass

PlainBases = (A, B)
reveal_type(PlainBases)
# ^ Revealed type is 'Tuple[def () -> foo.A, def () -> foo.B]'

class PlainC(*PlainBases):
# ^ Invalid base class
    pass

AnotatedBases = (A, B)  # type: Tuple[Type[A], Type[B]]
reveal_type(AnotatedBases)
# ^ Revealed type is 'Tuple[Type[foo.A], Type[foo.B]]'

class AnotatedC(*AnotatedBases):
# ^ Invalid base class
    pass

I'm using Python 3.5 and mypy 0.620. Having just updated to 0.641 I see that there are now also errors that "Can use starred expression only as assignment target" in the above, which goes some way to explaining why this is currently unsupported, though leads to another one (apologies if this is documented somewhere):
Why is it that mypy is unable to expand the types of this starred expression?

Is this a result of the issue relating to multiple passes which is mentioned in https://github.com/python/mypy/issues/4284#issuecomment-346908509 or something else?

Is this something which is possible/likely to be supported?

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 riproducendo gli esempi di basi con asterisco dell’issue usando le versioni di mypy indicate, quindi leggi la discussione nell’issue #4284 sui passaggi multipli. Il lavoro è completo solo quando la gestione prevista delle tuple fisse nelle basi delle classi è stata decisa e implementata, oppure quando il comportamento non supportato è chiaramente documentato con i controlli appropriati.

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

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
32/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.