python / python/mypy

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

Abierto
#14,602 0 comentarios 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

bug
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Merge medio
1 d 18 h
PR fusionados (30 d)
54

Descripción

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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Empieza ejecutando el ejemplo de Python proporcionado con mypy y compara el tipo inferido de foo_func1 con el tipo esperado Foo[Callable[[int], str]]. Sigue el diagnóstico de compatibilidad del argumento callable y confirma que el trabajo está terminado cuando la llamada final a ap ya no informe de un error arg-type para tipos Callable equivalentes.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
compilers
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.