bloomberg / bloomberg/python-comdb2
Type Checking for Class Row Factory
- Lenguaje dominante
- Python
- Estrellas
- 29
- Forks
- 28
- Merge medio
- 11 h 25 min
- PR fusionados (30 d)
- 2
Descripción
As described in Issue #42 there was a proposed solution for having a Class Row Factory. Alongside the conversations happened there, there was a concern regarding type checking for the attributes of the class and output returned by the database. There doesn't seem to be a way to enforce type checking hence an extra `__post_init__` method would be required within the class to enforce type checking. This can be overlooked by a lot of developers and can lead to a lot of type mismatches.
## Solution
Define a Base class that our dataclasses can inherit. The Base class can have all the required methods for the validation etc.
The updated use of it can be described it here. The solution has been implemented on the PR #57
```
>>> from dataclasses import dataclass
>>> from comdb2.dataclasses.class_row_factory import BaseRowFactoryClass
...
>>> @dataclass
>>> class ABC(BaseRowFactoryClass):
>>> x: int
>>> y: int
...
>>> conn.row_factory = ClassRowFactory(ABC)
>>> row = conn.cursor().execute("select 1 as x, 2 as y").fetchone()
>>> print(row)
<>
>>> print(row.x)
```
Guía de contribución
Línea de trabajo
Revisa primero Issue #42 y PR #57, ya que la solución propuesta se describe allí. Después inspecciona comdb2.dataclasses.class_row_factory y compara el ejemplo BaseRowFactoryClass con la preocupación indicada sobre la comprobación de tipos. Se considera completado confirmar si el PR aborda completamente la validación de los atributos de dataclass y la salida de la base de datos.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- databases
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 20/100