python / python/mypy

private (name mangled) attributes within inherited classes can not have different types

Aperta
#19,598 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-runtime-semantics
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

mypy does not recognise that a dunder class attribute does not clash with the inherited one

To Reproduce

Consider the following example:

class BaseSpecialValue:

    def __init__(self, special_value:int):
        self.__special_value = special_value

    @property
    def base_special_value(self) -> int:
        return self.__special_value

class SpecialValue(BaseSpecialValue):

    def __init__(self, base_special_value:int, special_value:str):
        super().__init__(special_value=base_special_value)
        self.__special_value = special_value

    @property
    def special_value(self) -> str:
        return self.__special_value

if __name__ == '__main__':

    foo = SpecialValue(special_value='one', base_special_value=1)
    print(f'special_value={foo.special_value}, base_special_value={foo.base_special_value}')

In this case both SpecialValue and BaseSpecialValue define __special_value, when run it is clear that python treats them separately as the result is:

special_value=one, base_special_value=1

Expected Behavior

No mypy error reported

Actual Behavior

However, when mypy is run the following result is returned by mypy:

mypy --config-file=default_mypy.ini mypy_dunder_test.py   
mypy_dunder_test.py:15: error: Incompatible types in assignment (expression has type "str", variable has type "int")  [assignment]
mypy_dunder_test.py:19: error: Incompatible return value type (got "int", expected "str")  [return-value]
Found 2 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.17.1
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): See below
  • Python version used: 3.12.0

default configuration used

# Global options:

[mypy]
warn_return_any = True
warn_unused_configs = True

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 1.17.1 e confermando i due errori segnalati. Traccia il modo in cui mypy gestisce i nomi degli attributi con doppio underscore tra le classi ereditate, quindi verifica che l’esempio non produca errori, preservando i diversi tipi inferiti e i tipi restituiti dalle proprietà.

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
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.