Join of actual Class with Type[Class] results in `object`

Abierto
#3,976 2 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
45/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
python
Área
compilers

Línea de trabajo

Start with the provided Python reproducer and trace mypy's handling of the conditional-expression type join involving the actual Foo class and Type[Foo]. Run the example to confirm the revealed type and call error; done means the expression is inferred as Type[Foo], Foo can be called, and the existing reversed-order and cls or Foo cases remain covered.

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

Descripción

bug priority-1-normal

This file:

from typing import Type, Optional

class Foo:
    pass

def make_foo(cls: Optional[Type[Foo]] = None) -> Foo:
    x = Foo if cls is None else cls
    reveal_type(x)
    return x()

gives the following:

typeofclass.py:10: error: Revealed type is 'builtins.object' 
typeofclass.py:11: error: "object" not callable 

This example is superficially related to #3487, in that it uses an if/else expression to make mypy perform a type join, but the distinct bug here is that joining Foo (the actual class object) with Type[Foo] should result in Type[Foo], but instead results in object.

This is related to the fact that class objects are typed as callables instead of as Type[Class].

Oddly reversing Foo if cls is None else cls to cls if cls is not None else Foo results in builtin.type instead of object, which is still wrong, but differently wrong.

Using cls or Foo results in Type[Foo].

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.