python / python/mypy

Inherited, overridden attribute type reverted to the top parent class type

Aperta
#7,394 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug false-positive priority-1-normal topic-inheritance
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

The issue type is probably quite bad, but example should quickly clear things up:

class A:
    attr = None


class B(A):
    attr: str  # type: ignore


class C1(B):
    attr = "test"


class C2(B):
    pass


class D(C2):
    attr = "test"

mypy test.py outputs:

test.py:18: error: Incompatible types in assignment (expression has type "str", base class "A" defined the type as "None")

where line 18 is the last line of above example

As to why I do attr: str # type: ignore - in a nutshell, in my case A class comes from external source and they botched type hints there so I try to fix them in B. Not sure if it should have worked for C1, but if it did I see it breaking in D as a bug due mypy not being consistent, but then again maybe there is a good reason for it?.

Tested with mypy 0.720

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 eseguendo l’esempio di ereditarietà fornito con mypy 0.720 e confronta le diagnosi per C1 e D. Traccia la risoluzione dei tipi per gli attributi ereditati e sovrascritti, quindi conferma che il comportamento risultante sia coerente con la semantica prevista descritta nell’issue.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.