Files
ipam/PYTHON_3_10_COMPATIBILITY.md
T
Your Name f66e4d41c9 fix(deps): 修复 pyasn1 版本冲突问题
问题原因:
- pyasn1>=0.5.0 移除了 pyasn1.compat.octets 模块
- python-jose==3.5.0 强制要求 pyasn1>=0.5.0
- 形成版本冲突,导致 pysnmp 无法导入

解决方案:
- pyasn1==0.4.8 (保留 compat.octets 的版本)
- pyasn1-modules==0.2.8 (配套版本)
- python-jose[cryptography]==3.3.0 (兼容 pyasn1 0.4.8)

验证:
- Python 3.10 & 3.11 均可正常导入
- FastAPI 应用可正常加载 (95 routes)
2026-07-23 14:10:59 +08:00

75 lines
2.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Python 3.10 兼容性说明
## 概述
本分支 (python3.10) 专门针对 Python 3.10 环境优化,确保所有代码和依赖都能在 Python 3.10 下正常运行。
## 兼容性调整
### 已验证的兼容特性
✅ 所有依赖包版本均支持 Python 3.10
✅ 无 Python 3.11 特有语法(如 `tomllib``asyncio.TaskGroup``typing.Self` 等)
✅ 类型注解完全兼容 Python 3.10
`match` 关键字未用作 Python 3.10 的模式匹配语句(仅用作 `re.match()` 的变量名)
### 重要依赖说明
#### pysnmp 兼容问题解决
**问题**`pysnmp==7.1.15` 存在以下兼容性问题:
- 使用已废弃的 `asyncio.coroutine` 装饰器(Python 3.11 中已移除)
- 与 Python 3.10+ 的 asyncio 不兼容
**解决方案**
使用社区维护的分支 `pysnmp-lextudio==5.0.31`
- 完全向后兼容原有 pysnmp API
- 支持 Python 3.6+
- 积极维护,修复了 asyncio 兼容性问题
- 导入方式完全不变
```python
# 无需修改代码,导入方式保持一致
from pysnmp.hlapi.asyncio import bulkCmd, getCmd, nextCmd
```
#### pyasn1 版本冲突解决
**问题**`pyasn1>=0.5.0` 移除了 `pyasn1.compat.octets` 模块,导致 pysnmp 导入失败。
同时 `python-jose==3.5.0` 强制要求 `pyasn1>=0.5.0`,形成版本冲突。
**解决方案**
- `pyasn1==0.4.8` - 锁定到包含 compat.octets 的版本
- `pyasn1-modules==0.2.8` - 配套版本
- `python-jose[cryptography]==3.3.0` - 兼容 pyasn1 0.4.8 的版本
### 依赖版本要求
- `pysnmp-lextudio==5.0.31` - SNMP 协议兼容替代
- `typing-extensions>=4.5.0` - 提供额外的类型支持
- 所有核心依赖(FastAPI、SQLAlchemy、Pydantic 等)均已验证兼容
## 已知的 Python 3.11+ 特性未使用
项目中未使用以下 Python 3.11+ 特有的功能:
- `tomllib` 标准库(如有需要可使用 `tomli` 第三方库)
- `asyncio.TaskGroup`
- `typing.Self`
- `ExceptionGroup` / `except*`
- `StrEnum` / `IntEnum` 的新特性
## 安装说明
```bash
cd backend
pip install -r requirements.txt
```
## 升级提示
如果未来需要升级到 Python 3.11+,可以:
1. 使用 `tomllib` 替代可能的 TOML 解析需求
2. 考虑使用 `asyncio.TaskGroup` 改进异步代码结构
3. 使用 `typing.Self` 简化类型注解
## 验证
可以使用以下命令验证 Python 版本兼容性:
```bash
python --version # 应为 3.10.x 或 3.11.x
python -c "import sys; assert sys.version_info >= (3, 10), 'Python 3.10+ required'"
# 验证 pysnmp 导入
python -c "from pysnmp.hlapi.asyncio import bulkCmd, getCmd, nextCmd; print('pysnmp OK')"
```