python / python/mypy

Redefinition without reassignment doesn't check existing value

Aperta
#12,866 3 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

With --allow-redefinition, mypy allows a variable to be redefined, without checking that the existing value is the correct type.

a: str = "foo"
print(a)  # value needs to be used before redefinition
a: int  # but it is really still str
if a > 1:
    ...

https://mypy-play.net/?mypy=latest&python=3.10&flags=allow-redefinition&gist=79d1e9219d66da441050ba730a7c2842

Expected Behaviour

In this example, mypy should detect that a is still a str, and not allow this redefinition.

It could treat the redefinition as though it is a new variable, being assigned the value of the existing one. That would result in an error like:
error: Incompatible types in assignment (expression has type "str", variable has type "int")

Or it could be a new error, like:
error: Redefinition incompatible with existing type (existing type "str", new type "int")

Actual Behaviour

mypy:

Success: no issues found in 1 source file

python:

TypeError: '>' not supported between instances of 'str' and 'int'

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 l'esempio nell'esecuzione collegata di mypy-play.net con --allow-redefinition, quindi traccia la gestione da parte di mypy delle ridefinizioni annotate. Il lavoro è terminato quando il valore str esistente viene verificato rispetto all'annotazione int successiva e mypy segnala un errore appropriato invece di avere successo.

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.