python / python/mypy

Type issues with Callables

Abierto
#8,718 5 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

false-positive feature priority-1-normal
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

  • Are you reporting a bug, or opening a feature request?
    bug

  • Please insert below the code you are checking with mypy

from typing import Optional, Iterable, TypeVar, Callable

T = TypeVar('T')


def is_even(n: int) -> bool:
    return n % 2 == 0


# equivalent to https://ruby-doc.org/core-2.5.0/Enumerable.html#method-i-detect
def detect(function: Callable[[T], bool],
           iterable: Iterable[T]) -> Optional[T]:
    for element in iterable:
        if function(element):
            return element
    return None


numbers = [1, 2, 3, 4, 5]

# using a lambda
print(detect(lambda n: n % 2 == 0, numbers))

# using a function
print(detect(is_even, numbers))

  • What is the actual behavior/output?
$ mypy detect.py 
detect.py:22: error: Unsupported operand types for % ("object" and "int")
detect.py:25: error: Argument 1 to "detect" has incompatible type "Callable[[int], bool]"; expected "Callable[[object], bool]"
Found 2 errors in 1 file (checked 1 source file)
  • What is the behavior/output you expect?
    I was expecting the types to be inferred and the above code to typecheck.

  • What are the versions of mypy and Python you are using?

$ mypy --version
mypy 0.770
$ python --version
Python 3.7.7
  • Do you see the same issue after installing mypy from Git master?
    yes
$ mypy detect.py
detect.py:22: error: Unsupported operand types for % ("object" and "int")
detect.py:25: error: Argument 1 to "detect" has incompatible type "Callable[[int], bool]"; expected "Callable[[object], bool]"
Found 2 errors in 1 file (checked 1 source file)
$ mypy --version
mypy 0.770+dev.22c67daac7d2b28f33d4e9c7bee4f1a06e61a3e5
  • What are the mypy flags you are using? (For example --strict-optional)
    None.

  • If mypy crashed with a traceback, please paste the full traceback below.
    It didn't crash

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 reproductor detect.py proporcionado con las versiones de mypy indicadas e inspecciona cómo la inferencia de Callable y TypeVar gestiona los argumentos lambda e is_even. Rastrea el comportamiento relevante de la comprobación de tipos y añade cobertura de regresión cuando corresponda; se considera terminado cuando el ejemplo pasa la comprobación de tipos sin los dos errores indicados.

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

Evaluación

Stack tecnológico
python
Área
devtools
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.