Mypy can't correctly infer nested union types in some situations

Abierto
#3,935 4 comentarios 3 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
devtools

Línea de trabajo

Start by running the issue's good, bad, and good2 examples with mypy and compare the inferred types for the list literal and concatenation. Trace the nested union and list inference path, then add a regression test showing that bad() is accepted without the cast while existing type-checking behavior remains intact.

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

Descripción

topic-union-types

I'm not sure what the cause is here, but I documented some simple examples of this issue. I would expect all of these examples to pass the type checker, but one fails peculiarly. I wouldn't be surprised if this is the result of an existing issue, but I couldn't find it.

from typing import cast, List, Union

Scalar = Union[str, float]


def good() -> List[Scalar]:
    return ["foo", 1.0]


def bad(params: List[Scalar]) -> List[Scalar]:
    # error: Incompatible return value type (got List[object], expected
    # List[Union[str, float]])
    # error: Unsupported operand types for + (List[object] and
    # List[Union[str, float]])
    return ["foo", 1.0] + params


def good2(params: List[Scalar]) -> List[Scalar]:
    return cast(List[Scalar], ["foo", 1.0]) + params
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.