Reusing variable name in different scopes
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
I saw that there is a similar bug around redefining a variable, but I believe this one is slightly different problem, here is the code:
#! /usr/bin/env python
from typing import Dict, List, Union
def main(arg):
# type: (bool) -> Union[Dict[str, str], List[str]]
if arg:
var = list()
var.append('something')
else:
var = dict()
var['key'] = 'something'
return var
if __name__ == '__main__':
main(True)
The variable var while it uses the same name, it is technically a different variable, but mypy 0.630 is complaining about it:
$ ../py37/bin/mypy --python-executable ../py27/bin/python test.py
test.py:12: error: Incompatible types in assignment (expression has type "Dict[<nothing>, <nothing>]", variable has type "List[str]")
test.py:13: error: No overload variant of "__setitem__" of "list" matches argument types "str", "str"
test.py:13: note: Possible overload variants:
test.py:13: note: def __setitem__(self, int, str) -> None
test.py:13: note: def __setitem__(self, slice, Iterable[str]) -> None
Ideally it should have no problem with it and at the point where there is return statement it should just do an union so the type would be Union[Dict[str, str], List[str]]
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con il reproducer test.py e la funzione main mostrata nell’issue, quindi segui come mypy deduce i tipi di var attraverso i rami if ed else. Il lavoro è completato quando l’esempio non produce errori incompatible-assignment o setitem e il valore restituito viene trattato come Union[Dict[str, str], List[str]].
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à
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100