mypy doesn't understand function signature compatibility
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
mypy doesn't understand the signature compatibility between functions when determining valid uses in lists. This means mypy can miss type errors.
To Reproduce
The following code type checks despite containing a type error that will lead to runtime failure. The expectation was that appending to the list will show a type error or warning for the incompatible signatures.
funclist = [lambda x: print(1)]
funclist += [lambda k: print(2)] # incompatible signature, but type not widened and no error
reveal_type(funclist) # builtins.list[def (x: Any)]
funclist[1](x=4) # this will error at runtime because of a typing mistake
Expected Behavior
Type error. I compared to pyright and pyright does show a type error for the incompatible signatures. Other possible behaviors would be to widen the type of funclist on line 1 or 2 to no longer think the first argument has keyword variable named x, but this is probably a bit odd/inconsistent. In this case mypy would be able to throw an error on line 4 with the keyword argument usage. Normally you would need to annotate funclist when it's defined if you wanted the wider behavior.
Actual Behavior
no type error
Your Environment
- Mypy version used: 0.920
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.10
- Operating system and version: Gentoo/Linux stable
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 by running the provided Python snippet with mypy 0.920 and comparing its result with pyright. Trace how list expressions and augmented assignments infer callable signatures, then verify that the incompatible keyword argument is rejected or the list type is widened as described.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100