python
type_error
ai_generated
true
TypeError:'>' 不支持 'Version' 和 'str' 实例之间
TypeError: '>' not supported between instances of 'Version' and 'str'
ID: python/packaging-version-comparison-error
80%修复率
84%置信度
0证据数
2024-12-03首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.8 | active | — | — | — |
| 3.9 | active | — | — | — |
| 3.10 | active | — | — | — |
| 3.11 | active | — | — | — |
| 3.12 | active | — | — | — |
根因分析
直接将 packaging.version.Version 对象与字符串进行比较而未进行转换。
English
Comparing a packaging.version.Version object directly with a string without conversion.
解决方案
-
95% 成功率
from packaging.version import Version; v1 = Version('1.0'); v2 = Version('2.0'); print(v1 < v2) -
90% 成功率
from packaging.version import parse; print(parse('1.0') > parse('2.0'))
无效尝试
常见但无效的做法:
-
80% 失败
String comparison of versions (e.g., '1.10' > '1.9') is lexicographic and incorrect.
-
50% 失败
Equality may work but other comparisons still fail.