should report ambiguous type variables in self types

Abierto
#4,402 4 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
35/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
python
Área
tooling

Línea de trabajo

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.

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

Descripción

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.

Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Merge medio
1 d 18 h
PR fusionados (30 d)
54

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.

Más de python/mypy

Todos los issues de python/mypy

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.