Redefinition of function arguments can give unclear error message
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
With
import typing
# simulate an untyped module function
def function() -> typing.Any:
return []
def foo(var: list|str) -> list:
# insert a lot of code so that it's totally unobvious that "var" is a function parameter
var = function()
return var
You get the output
$ mypy ./test2.py
test2.py:8: error: Incompatible return value type (got "list[Any] | str", expected "list[Any]") [return-value]
Found 1 error in 1 file (checked 1 source file)
$ mypy--version
mypy 1.10.0 (compiled: yes)
which is technically correct, as var was defined as list[Any] | str as a parameter. But if you don't realise that, say there is a ton of code between where that's defined and where the return value is, you are left looking at var = function(); return var and wondering why the heck function() has been inferred to return list[Any] | str.
For a "standard" redefinition a [no-redef] warning comes out making it fairly clear. It would be great if somehow it remembered function arguments too, and then put out for this case Name "var" already defined as function parameter on line 7 which would make this very clear.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit der bereitgestellten Python-Reproduktion und führe sie mit mypy 1.10.0 aus, um den aktuellen Fehler beim Rückgabewert zu beobachten. Verfolge die bestehende Diagnose [no-redef] für gewöhnliche Neudefinitionen und ermittle, wo Neudefinitionen von Funktionsparametern behandelt werden. Fertig ist die Aufgabe, wenn das Beispiel den Parameternamen und seine Deklarationszeile mit einer eindeutigen Diagnose meldet.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100