bloomberg / bloomberg/python-comdb2

Type Checking for Class Row Factory

Offen
#60 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Python
Sterne
29
Forks
28
Ø Merge
11 Std. 25 Min.
Gemergte PRs (30 T.)
2

Beschreibung

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)
```

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Überprüfe zuerst Issue #42 und PR #57, da die vorgeschlagene Lösung dort beschrieben wird. Untersuche dann comdb2.dataclasses.class_row_factory und vergleiche das Beispiel BaseRowFactoryClass mit dem genannten Problem der Typprüfung. Als erledigt gilt, zu bestätigen, ob der PR die Validierung für Dataclass-Attribute und Datenbankausgabe vollständig abdeckt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
databases
Issue-Typ
Feature
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
20/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.