python / python/mypy

Type issues with Callables

Aperta
#8,718 5 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

false-positive feature priority-1-normal
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

  • 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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia eseguendo il reproducer detect.py fornito con le versioni di mypy indicate e analizza come l’inferenza di Callable e TypeVar gestisce gli argomenti lambda e is_even. Traccia il comportamento rilevante del controllo dei tipi e aggiungi una copertura di regressione dove appropriato; il lavoro è completato quando l’esempio supera il controllo dei tipi senza i due errori segnalati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.