Common type in container is determined using constructor args

Abierto
#11,659 1 comentario 1 reacción 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
25/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
python
Área
compilers

Línea de trabajo

Empieza reproduciendo los dos ejemplos de reveal_type y el caso de pytest.raises del issue. Traza cómo se infiere el tipo común del contenedor a partir de los constructores de las clases y, a continuación, verifica que las subclases de Exception indicadas produzcan los tipos inferidos esperados y ningún error de argumentos incompatibles.

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

Descripción

bug

Bug Report

To Reproduce

from typing import Type, List


class NonLiskovChild(Exception):
    """Does not match Exception.__init__() signature"""
    def __init__(self, _some_other_arg: object) -> None:
        super().__init__()


types1 = [TypeError, ValueError]
reveal_type(types1)
# Revealed type is 'builtins.list[def (*args: builtins.object) -> builtins.Exception]'

types2 = [TypeError, NonLiskovChild]
reveal_type(types2)
# Revealed type is 'builtins.list[builtins.type*]'

# When explicitly setting the type, mypy accepts NonLiskovChild as an Exception subtype:
types3: List[Type[Exception]] = [TypeError, NonLiskovChild]
reveal_type(types3)
# Revealed type is 'builtins.list[Type[builtins.Exception]]'

Expected Behavior

The lowest common type should be determined by inheritance, not with __init__() signature matching.

Otherwise stdlib classes like CalledProcessError do not work correctly, e.g. in this example:

import pytest
from subprocess import CalledProcessError

with pytest.raises((TypeError, CalledProcessError)) as exc_info:
    pass
# error: Need type annotation for "exc_info"
# error: Argument 1 to "raises" has incompatible type "Tuple[Type[TypeError], Type[CalledProcessError]]"; expected "Union[Type[<nothing>], Tuple[Type[<nothing>], ...]]"

Your Environment

  • Mypy version used: 0.910
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.8.10
  • Operating system and version: Windows 10
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.