mypy
mypy copied to clipboard
Redefinition of function arguments can give unclear error message
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.