python / python/mypy

Assignment with a callable: inconsistent kwarg handling

Open
#13,999 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug(?) Report

Two problems:

  1. reveal_type(a) and reveal_type(b) yield identical answers while at the same time these 2 types behave differently when used in lines 14 and 15.
  2. 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 with aa(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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.