python / python/mypy

mypy fails to report missing "private" __x attribute

Aperta
#8,267 9 commenti 4 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

feature priority-1-normal topic-inheritance topic-runtime-semantics
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Mypy doesn't seem to grok that self.__x in a subclass is different from self.__x in a superclass. It also doesn't seem to understand order of initialization.

  • Are you reporting a bug, or opening a feature request?

bug

  • Please insert below the code you are checking with mypy
class A:
    def __init__(self, x: int) -> None:
        self.__x = x  # replace this with pass and mypy reports the error


class B(A):
    def __init__(self) -> None:
        A.__init__(self, self.__x)


B()
  • What is the actual behavior/output?
$ mypy --strict tytest.py 
Success: no issues found in 1 source file
$ python tytest.py 
Traceback (most recent call last):
  File "tytest.py", line 11, in <module>
    B()
  File "tytest.py", line 8, in __init__
    A.__init__(self, self.__x)
AttributeError: 'B' object has no attribute '_B__x
  • What is the behavior/output you expect?

mypy should report:

tytest.py:8: error: "B" has no attribute "__x

  • What are the versions of mypy and Python you are using?

mypy 0.700

Do you see the same issue after installing mypy from Git master?

yes (mypy-0.770+dev.01ca4e062e38a9db8f462b9464fbbd5dd945a8cf)

  • What are the mypy flags you are using? (For example --strict-optional)
    --strict

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 tytest.py segnalato con mypy --strict e confronta il codice accettato con l’AttributeError di Python. Leggi i percorsi del controllo dei tipi per la gestione dei nomi con doppio underscore e l’inizializzazione delle istanze; poi aggiungi una copertura che distingua gli attributi privati della sottoclasse da quelli della superclasse e segnali l’attributo mancante prima dell’esecuzione del programma.

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

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Tranquilla
Chiarezza
Specificata chiaramente
Idoneità per principianti
55/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.