python / python/mypy

Changing just the return type annotation causes incompatible argument error

Aperta
#10,105 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

When a return type annotation changes from Iterable of Optional typevars into Iterable of Tuples of Optional typevars mypy starts to signal an error in function call arguments (invalid callback argument type).

To Reproduce

from typing import Callable, Iterable, Optional, Tuple, TypeVar

SourceType = TypeVar("SourceType")
TransformedType = TypeVar("TransformedType")
ProcessedType = TypeVar("ProcessedType")


def simple_transform_process(
    source: Iterable[SourceType],
    transform_callback: Callable[[SourceType], Optional[TransformedType]],
    process_callback: Callable[[TransformedType], ProcessedType],
) -> Iterable[Optional[ProcessedType]]:
    for item in source:
        transformed = transform_callback(item)
        if transformed is None:
            yield None
        else:
            yield process_callback(transformed)


def transform_process(
    source: Iterable[SourceType],
    transform_callback: Callable[[SourceType], Optional[TransformedType]],
    process_callback: Callable[[TransformedType], ProcessedType],
) -> Iterable[Tuple[Optional[TransformedType], Optional[ProcessedType]]]:
    for item in source:
        transformed = transform_callback(item)
        if transformed is None:
            yield None, None
        else:
            yield transformed, process_callback(transformed)


def transform(x: str) -> Optional[int]:
    if x:
        return int(x)
    else:
        return None


def process(x: int) -> float:
    return x / 2


def test_simple_transform_process() -> None:
    assert list(simple_transform_process(["", "2", "3"], transform, process)) == [
        None,
        1,
        1.5,
    ]


def test_transform_process() -> None:
    assert list(transform_process(["", "2", "3"], transform, process)) == [
        (None, None),
        (2, 1),
        (3, 1.5),
    ]

(Write your steps here:)

  1. Run mypy on the above code mypy test_mypy_problem.py

Expected Behavior

The code should have passed the type checker for both simple_transform_process call and transform_process call as both of those have the same exact signatures of input parameters.

Actual Behavior

mypy reports the following error on transform_process call:

test_mypy_problem.py:54: error: Argument 3 to "transform_process" has incompatible type "Callable[[int], float]"; expected "Callable[[Optional[int]], Optional[float]]"

The expected type of argument should be Callable[[int], float], just like for simple_transform_process and the code should pass the type checker.

Your Environment

  • Mypy version used: 0.790, 0.800, mypy 0.820+dev.dc4f0af163ed3748311115fd896494262c0dc644
  • Mypy command-line flags: mypy test_mypy_problem.py
  • Mypy configuration options from mypy.ini (and other config files): no config file used
  • Python version used: Python 3.8.5
  • Operating system and version: Ubuntu 20.04.2 LTS

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 con il riproduttore in test_mypy_problem.py ed esegui mypy test_mypy_problem.py usando le versioni indicate di Python e mypy. Traccia il modo in cui l'inferenza dei generics gestisce la callback transform_process rispetto a simple_transform_process, quindi aggiungi un test di regressione che dimostri che entrambe le chiamate superano correttamente il controllo dei tipi.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.