python / python/mypy

Bad self type inference with Generics and inheritance?

Abierto
#9,322 2 comentarios 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

bug priority-2-low
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

  • Are you reporting a bug, or opening a feature request?

Reporting a bug

  • Please insert below the code you are checking with mypy
from typing import TypeVar, Generic

T = TypeVar("T")

class Foo(Generic[T]):
    def __init__(self): ...

class Bar(Generic[T]): ...

class FooChild(Foo[Bar[T]]):
    def __init__(self, blah: T):
        Foo.__init__(self)
  • What is the actual behavior/output?
$ mypy foo.py
foo.py:18: error: Argument 1 to "__init__" has incompatible type "FooChild[T]"; expected "Foo[T]"
Found 1 error in 1 file (checked 1 source file)
  • What is the behavior/output you expect?
Success: no issues found in 1 source file
  • What are the versions of mypy and Python you are using?
mypy 0.750
Python 3.7.3
  • Do you see the same issue after installing mypy from Git master?

Yes, successfully reproduced with mypy 0.790+dev.df6894b5a9bb1a913e66df87138cd7a240c09fc1

  • What are the mypy flags you are using? (For example --strict-optional)

No flags.

  • If mypy crashed with a traceback, please paste
    the full traceback below.

No crash

  • Additional information:

The following versions typecheck successfully:

from typing import TypeVar, Generic

T = TypeVar("T")

class Foo(Generic[T]):
    def __init__(self): ...

class Bar(Generic[T]): ...

class FooChild(Foo[T]): # Removed one level of nesting here
    def __init__(self, blah: T):
        Foo.__init__(self)
from typing import TypeVar, Generic

T = TypeVar("T")

class Foo(Generic[T]):
    def __init__(self): ...

class Bar(Generic[T]): ...

class FooChild(Foo[Bar[T]]):
    def __init__(self): # Removed the argument here
        Foo.__init__(self)

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.

Línea de trabajo

Comienza con el reproducible foo.py proporcionado y ejecútalo con las versiones de mypy indicadas, confirmando el tipo self incorrecto para FooChild[T]. Sigue la herencia genérica y la llamada al constructor implicadas en la inferencia de tipos de mypy, y añade cobertura que demuestre que el caso anidado Bar[T] tiene éxito sin errores.

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

Evaluación

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

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.