python / python/mypy

Type issues with Callables

Offen
#8,718 5 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

false-positive feature priority-1-normal
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

  • 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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Führen Sie zunächst den mitgelieferten detect.py-Reproducer mit den gemeldeten mypy-Versionen aus und untersuchen Sie, wie die Callable- und TypeVar-Inferenz mit den Argumenten lambda und is_even umgeht. Verfolgen Sie das relevante Verhalten der Typprüfung und fügen Sie gegebenenfalls eine Regressionstestabdeckung hinzu; als erledigt gilt die Aufgabe, wenn das Beispiel ohne die beiden gemeldeten Fehler typgeprüft wird.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.