Should operator `|` on `TypedDict` allow for creating intersection-like dicts?
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 1.8k
- Forks
- 302
- Merge medio
- 23 h
- PR fusionados (30 d)
- 8
Descripción
I'm wondering how is the relation between the following PEPs:
- PEP 584 which introduces operator
|for dicts, but doesn't mention how it should behave forTypedDict. - PEP 589 which introduces
TypedDict, but doesn't mention the operator|.
According to PEP 589, multiple inheritance can be used to create a combined (or "intersection like") dict, and operator | provides the corresponding behavior at runtime. Thus it would be nice if the type system could handle the combination properly (similar to how it is possible in TypeScript):
from typing import TypedDict
class HasFoo(TypedDict):
foo: int
class HasBar(TypedDict):
bar: int
class HasFooAndBar(HasFoo, HasBar):
...
def f(a: HasFoo, b: HasBar) -> HasFooAndBar:
return a | b
From a runtime and type-checking perspective this code looks valid, but currently mypy does not accept it (playground):
main.py:15: error: Incompatible return value type (got "HasFoo", expected "HasFooAndBar") [return-value]
main.py:15: error: Unsupported operand types for | ("HasFoo" and "HasBar") [operator]
Pyright seems to have the same behavior.
Apparently operator | can only be used for two instances of the same typed dict, which as far as I can see has limited use cases, because using the operator | on two dicts that already have the same fields is kind of pointless (perhaps it mostly makes sense if the type used total=False).
Possible related discussions and issues I've found:
- https://github.com/python/typing/issues/213 (since this feature for
TypedDictprobably falls into the "intersection type" category as well?) - https://github.com/microsoft/pylance-release/issues/2300
- https://github.com/microsoft/pyright/issues/2951
Guía de contribución
No hay ninguna guía de contribución indexada para este repositorio
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza comparando PEP 584 y PEP 589 y, después, reproduce el ejemplo de TypedDict en el mypy playground enlazado. Revisa las discusiones relacionadas sobre typing, Pylance y Pyright para determinar si existe consenso sobre el comportamiento propuesto similar a una intersección. Se considerará completado cuando haya una semántica acordada y un camino documentado para que los type checkers lo admitan.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- tooling
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 30/100