EnAccess / EnAccess/OpenPAYGO-python

Provide explicit type hints and pydantic validation for robust usage

未关闭
#34 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Python
星标
15
派生
15
PR 合并指标
30 天内没有已合并 PR

描述

**Description:**
Hi team! Thanks for building such an awesome library for the OpenPAYGO ecosystem.

**Expected Behavior:**
When using the OpenPAYGO library in modern Python backend applications (like FastAPI servers or typing-heavy codebases), developers expect:

* Clear type definitions showing what arguments standard functions (like `decode_token` or `MetricsRequestHandler`) accept and return.
* When processing JSON or dictionaries from devices, malformed data should raise explicit parsing/validation errors before reaching deep token logic.

**Current Behavior:**
Currently, functions like `decode_token` and `MetricsRequestHandler` rely on loose dictionaries and typeless signatures. Passing incorrect types or malformed historical data can trigger cryptic `KeyError` or `ValueError`, making debugging difficult.

**Proposed Solution:**
Add explicit Python typing (`typing.Dict`, `typing.Optional`, etc.) to method signatures across:

* `openpaygo/token_encode.py`
* `openpaygo/token_decode.py`
* `metrics_request.py` / `metrics_response.py`

Additionally, leverage **Pydantic models** to automatically validate and parse input data, stripping invalid or unexpected fields.

**Implementation Notes:**

* Created `openpaygo/models.py` with Pydantic models:

* `MetricsDataFormat`
* `MetricsRequestData`
* `MetricsHistoricalDataStep`
* Functions in `metrics_request.py` and `metrics_response.py` now cast dictionaries into Pydantic models.
* Core files (`token_shared.py`, `token_encode.py`, `token_decode.py`) updated with type hints.
* Integration tests run successfully with all 80 tests passing.

**Benefits:**

* Improved type safety and clearer function signatures.
* Automatic input/output validation reduces runtime errors.
* Easier debugging and maintenance for developers.

贡献指南

打开贡献指南

调研方向

首先检查 openpaygo/models.py,以及 openpaygo/token_encode.py、openpaygo/token_decode.py、metrics_request.py、metrics_response.py 和 token_shared.py 中现有的实现。检查集成测试,目前这些测试报告有 80 个测试通过。完成的标准是:列出的函数具有显式类型提示,使用指定的 Pydantic 模型验证格式错误的输入,并且测试套件仍然通过。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
api, backend
Issue 类型
功能
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

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