Redefinition of function arguments can give unclear error message
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the supplied Python reproduction and run it with mypy 1.10.0 to observe the current return-value error. Trace the existing [no-redef] diagnostic for ordinary redefinitions and determine where function-parameter redefinitions are handled. Done means the sample reports the parameter name and its declaration line with a clear diagnostic.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100