python / python/mypy

(🐞) `type.__call__` not checked properly

Abierto
#17,316 4 comentarios 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

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

Descripción

class A:
    def __init__(self, x: int):
        print(x)

A.__call__("a")  # no error
A("a")  # Argument 1 to "A" has incompatible type "str"; expected "int"  [arg-type]

The typing spec says:

At runtime, a call to a class’ constructor typically results in the invocation of three methods in the following order:

  1. The __call__ method of the metaclass (which is typically supplied by the type class but can be overridden by a custom metaclass and which is responsible for calling the next two methods)

So mypy is checking that usages of the call syntax (()) are conforming to the constructor type, but the functionally identical call attribute __call__. This is against the spec, which specifies that type checkers should mirror the behavior that __call__ will invoke __new__ and __init__

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

Empieza por el tratamiento que hace mypy de las llamadas al constructor y de las llamadas mediante atributo a type.__call__, usando como reproducción el ejemplo del issue. Sigue cómo se comprueba A("a") frente a A.__call__("a"), y añade cobertura que demuestre que ambas formas hacen cumplir el tipo del argumento del constructor.

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

Evaluación

Stack tecnológico
python
Área
devtools
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.