Assignment with a callable: inconsistent kwarg handling
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug(?) Report
Two problems:
reveal_type(a)andreveal_type(b)yield identical answers while at the same time these 2 types behave differently when used in lines 14 and 15.- mypy gives no error for line 14 (
aa: F = a) while (to my understanding) it is within normal mypy capabilities to catch the kwarg mismatch. In other words, we would have a runtime error withaa(x=1)while mypy stays happy.
To Reproduce
https://mypy-play.net/?mypy=master&python=3.10&gist=090ff9d41c1b1e3012bf96720b0900d4
from typing import *
class F(Protocol):
def __call__(self, x: int) -> int: ...
a = lambda y: 0
def b(y: Any) -> int:
return 0
reveal_type(a)
reveal_type(b)
aa: F = a
bb: F = b
Expected Behavior
1, reveal_type should reveal enough details so that mypy behavior appears consistent.
2. For both the lines 14 and 15 mypy should discover the type incompatibility (due to argument name mismatch).
The output
/home/toton/tmp/f.py:11: note: Revealed type is "def (y: Any) -> builtins.int"
/home/toton/tmp/f.py:12: note: Revealed type is "def (y: Any) -> builtins.int"
/home/toton/tmp/f.py:15: error: Incompatible types in assignment (expression has type "Callable[[Any], int]", variable has type "F") [assignment]
/home/toton/tmp/f.py:15: note: "F.__call__" has type "Callable[[Arg(int, 'x')], int]"
Your Environment
$ venv/bin/python --version
Python 3.9.2
$ venv/bin/mypy --version
mypy 1.0.0+dev.a4da89e0543ee0d213b30216c9e15a5d00fcd578 (compiled: no)
$ venv/bin/mypy ~/tmp/f.py
.....
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 mypy-play reproduction and the callable assignment and reveal_type cases shown in the issue. Compare the behavior for the lambda and annotated function, then verify that both incompatible keyword-argument assignments are diagnosed and that revealed types explain the difference.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100