bloomberg / bloomberg/python-comdb2

Type Checking for Class Row Factory

未关闭
#60 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Python
星标
29
派生
28
平均合并
11 小时 25 分钟
30 天内合并 PR
2

描述

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

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。