python / python/mypy

Callable type anntoation written apart of assignment causes false positive typecheck result.

Open
#8,521 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug needs discussion priority-2-low topic-type-narrowing
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

A bugreport, mypy version is 0.761, python version is 3.7.3

This is a case where expected incapatible types, because i declared that foo's input type is float, and attempting to call it with str, but typechecking passed successful.
from typing import Callable

def bar(x):  return 5

foo : Callable[[float],int]
foo = bar

foo("test")

mypy this_file.py
Mypy's output:

Success: no issues found in 1 source file

A case with type annotation written with assignment wont give false positive result:
from typing import Callable

def bar(x):  return 5

foo : Callable[[float],int] = bar

foo("test")

mypy this_file.py
Mypy's output:

this_file.py:7: error: Argument 1 has incompatible type "str"; expected "float"
Found 1 error in 1 file (checked 1 source file)

I wasnt found other cases excluding cases with Callable type.

For example with Tuples type anntoation written apart of assignment all working fine:

from typing import Tuple

foo : Tuple[str,int]
foo = (1,3)

mypy this_file.py
Mypy's output:

this_file.py:4: error: Incompatible types in assignment (expression has type "Tuple[int, int]", variable has type "Tuple[str, int]")
Found 1 error in 1 file (checked 1 source file)

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

Reproduce the two Callable examples with mypy 0.761 and Python 3.7.3, then compare the separate declaration and assignment path with the combined form. Trace the Callable handling in the type checker and add a regression test that expects an incompatible argument-type error for the separate declaration case.

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
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.