bloomberg / bloomberg/python-comdb2
Type Checking for Class Row Factory
- 主要語言
- 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 #42 和 PR #57,因為其中描述了提議的解決方案。接著檢查 comdb2.dataclasses.class_row_factory,並將 BaseRowFactoryClass 範例與所述的型別檢查疑慮進行比較。完成的標準是確認 PR 是否完整處理 dataclass 屬性和資料庫輸出的驗證問題。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- databases
- Issue 類型
- 功能
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 20/100