Allow TypedDict.__required_keys__/__optional_keys__ to be used as Literal[...]
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Aptitud para principiantes
- 25/100
Línea de trabajo
Comienza revisando la interacción solicitada entre TypedDict.required_keys, TypedDict.optional_keys y Literal, junto con el ejemplo que usa mypy y pymongo. El issue no menciona archivos ni tests del repositorio; se consideraría completado con un diseño acordado y la validación de que los conjuntos de claves de TypedDict pueden proporcionar información literal de las claves para operaciones de almacenamiento tipadas.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
TypedDict instances work very well for NoSQL databases, like Mongo DB, but for any extended functionality a class is required, and marrying the two requires a few error-prone steps described here.
Consider a Mongo DB database document described with this structure, which works out well to the most part when being supplied into pymongo methods (with the exception of mypy failing to realize that it's TypedDict is a mutable mapping).
class X(TypedDict):
i: int
s: str
f: NotRequired[float]
Compared to Mapping, using a typed dictionary allows one to catch field references and value type mismatches quite quickly.
For more advanced functionality for this entity, however, a class may be maintained, like below. I will omit getters, etc, and just show relevant methods to set required and optional fields.
class Y:
def __init__(self, x: X) -> None:
self._storage = x
def set_required(self, key: Literal["i", "s"], value: Any) -> None:
self._storage[key] = value
def set_optional(self, key: Literal["f"], value: Any) -> None:
if value is None:
del self._storage[key]
else:
self._storage[key] = value
Herein lies the problem - I cannot use X.__required_keys__ to drive field references in the underlying storage because it's not a literal and the fact that it's frozenset doesn't help here, so I have to maintain copies of field names separately.
If X.__required_keys__ and X.__optional_keys__ would operate similarly to how constexpr behaves in C++ and would propagate literal keys they are created with to where they are used, like those methods above, it would make it very straightforward to integrate typed dictionaries with classes and use the former as storage with a well-defined schema that can be referenced in class methods to enforce proper field references.
I will also note that @dataclass is not good for this functionality because of a couple of reasons. First, just like any class, it has no concept of missing attributes and an attribute set to None is interpreted by the database driver as null. Second, @dataclass is implemented via auto-generated __init__, so it imposes unreasonable field order to satisfy optional arguments following required ones in the constructor. Lack of an alternative constructor doesn't help either.
EDIT: Replaced self.x_o with self._storage, which was a copy-and-paste error copying examples from a larger test case.
- Lenguaje dominante
- Python
- Estrellas
- 1.8k
- Forks
- 302
- Merge medio
- 23 h
- PR fusionados (30 d)
- 8
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.
Más de python/typing
-
topic: typing spec
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
-
topic: typing spec
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
-
topic: documentation
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
-
topic: documentation
Dificultad 2/5 1-3 horas Aptitud para principiantes 65/100
-
topic: conformance tests topic: typing spec
Dificultad 3/5 1-2 días Aptitud para principiantes 72/100
Todos los issues de python/typing
Issues similares
-
link-check link-check:sphinx-theme
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 65/100
qgis/QGIS-Documentation#11275 ·
-
bug priority:normal ready-for-dev
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100
OpenHands/extensions#626 · 1 comentario ·
-
Change observation tooltip text Abierto
Dificultad 1/5 Menos de una hora Aptitud para principiantes 90/100
CSCfi/sd-search-api#39 ·
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 90/100