should report ambiguous type variables in self types

Aperta
#4,402 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

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

Direzione di ricerca

Reproduce the supplied Python example with mypy, focusing on the merge_method self type and its type-variable diagnostics. Trace the type-checking path that produces the shown Callable error; done means the example reports the ambiguity clearly and no longer suggests Tuple[int, int] as the expected callback type.

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

Descrizione

bug priority-1-normal topic-self-types topic-usability

Example code:

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

T = TypeVar('T', covariant=True)
S = TypeVar('S')


class _Base(Generic[T]):
    @classmethod
    def split(self, left, right):
        # type: (_Base[S], _Base[T]) -> _Base[Tuple[S, T]]
        return Split(left, right)

    @classmethod
    def merge_classmethod(cls, base, f):
        # type: (_Base[Iterable[T]], Callable[[Iterable[T]], T]) -> Merge[T]
        return Merge(base, f)

   # the type should use "S", not "T".
    def merge_method(self, f):
        # type: (_Base[Iterable[T]], Callable[[Iterable[T]], T]) -> Merge[T]
        return Merge(self, f)


class Split(_Base[Tuple[S, T]]):
    def __init__(self, left, right):
        # type: (_Base[S], _Base[T]) -> None
        pass


class Merge(_Base[T]):
    def __init__(self, base, f):
        # type: (_Base[Iterable[T]], Callable[[Iterable[T]], T]) -> None
        pass


def _sum(xs):
    # type: (Iterable[int]) -> int
    return sum(xs)


x = _Base()  # type: _Base[int]

# this typechecks
_Base.merge_classmethod(_Base.split(x, x), _sum)


# this doesn't:
# Argument 1 to "merge_method" of "_Base" has incompatible type "Callable[[Iterable[int]], int]"; expected "Callable[[Iterable[Tuple[int, int]]], Tuple[int, int]]"
_Base.split(x, x).merge_method(_sum)

the merge_method type annotation should be using a variable other than T—a more informative error message should go here.

Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Merge medio
1g 18h
PR unite (30g)
54

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.

Altre issue di python/mypy

Tutte le issue di python/mypy

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.