python / python/mypy

Get arg-type incompatible type error for arguments of a Callable type

Open
#14,602 0 comments 1 reaction 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

For the sample code below, mypy is able to infer all the type information. mypy shows that the type of foo_func1 is Foo[(n: int) -> str]. However, on the last line of the sample code, mypy gives the following type error:

error: Argument 1 to "ap" has incompatible type "Foo[Callable[[Arg(int, 'n')], str]]"; expected "Foo[Callable[[int], str]]" [arg-type]

As the error message shows, the caller type (i.e. Foo[Callable[[Arg(int, 'n')], str]] ) and callee type (i.e. Foo[Callable[[int], str]] ) are in essence equal. Is there a way to make the type checking pass?

To Reproduce

from dataclasses import dataclass
from typing import Callable, Generic, TypeVar

A = TypeVar('A')
C = TypeVar('C')
E = TypeVar('E')

@dataclass
class Foo(Generic[A]):
    value: A

def ap(f: Foo[Callable[[C], E]], other: Foo[C]) -> Foo[E]:
        return Foo(f.value(other.value))

def func1(n: int) -> str:
    return 'h' * n

foo_func1 = Foo(func1)  
foo_5 = Foo(5)
foo_result = ap(foo_func1, foo_5)

Expected Behavior

Something like foo_func1's type should match the type Foo[Callable[[int], str]].

Actual Behavior

mypy gives the following type error:

error: Argument 1 to "ap" has incompatible type "Foo[Callable[[Arg(int, 'n')], str]]"; expected "Foo[Callable[[int], str]]" [arg-type]

for the last line of the sample code above.

Your Environment

  • Mypy version used: latest source from this git repo
  • Mypy configuration options from mypy.ini (and other config files):

allow_redefinition = true
check_untyped_defs = true
ignore_errors = false
ignore_missing_imports = true
implicit_reexport = false
local_partial_types = true
no_implicit_optional = true
strict_equality = false
strict_optional = true
warn_no_return = true
warn_redundant_casts = true
warn_unreachable = true
warn_unused_configs = true
warn_unused_ignores = true

  • Python version used: 3.11.1

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 by running the provided Python sample with mypy and compare the inferred type of foo_func1 with the expected Foo[Callable[[int], str]] type. Trace the callable argument compatibility diagnostic and confirm the work is done when the final ap call no longer reports an arg-type error for equivalent callable types.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.