使用 `--pre` 选项时,预发布版本与 pip 不匹配

Pre-release versions are not matched by pip when using the `--pre` option

假设您发布了两个预发行版:

 package 0.0.1.dev0
 package 0.0.2.dev0

我在 setup.py 中的 install_requires 部分指出:

[
    'package>=0.0.2,<1.0.0'
]

现在,当我 运行 pip install . --upgrade --pre 我得到一个错误:

ERROR: Could not find a version that satisfies the requirement package<1.0.0,>=0.0.2 (from versions: 0.0.1.dev0, 0.0.2.dev0) ERROR: No matching distribution found for package<1.0.0,>=0.0.2

我做错了什么? --pre 标志不是应该告诉 pip 匹配预发布版本吗?

总结

pip --pre 选项指示 pip 包含潜在匹配 pre-release 和开发版本,但它不会改变版本匹配的语义。

由于pre-release0.0.2.dev0比稳定版本0.0.2旧,pip在搜索至少与稳定版本[=14=一样新的包时正确报告错误].

说明

混淆的关键点在于 pip --pre 选项,它被记录为:

--pre
Include pre-release and development versions. By default, pip only finds stable versions.

问题的前提是 --pre 选项应该改变 package-version-matching 语义,使得 pre-release 版本后缀在与稳定版本匹配时将被忽略。

为了进一步说明,请考虑兼容发布运算符 ~=。 PEP 440 部分 Compatible release,部分陈述:

For a given release identifier V.N, the compatible release clause is approximately equivalent to the pair of comparison clauses:

>= V.N, == V.*

...

If a pre-release, post-release or developmental release is named in a compatible release clause as V.N.suffix, then the suffix is ignored when determining the required prefix match:

~= 2.2.post3 = 2.2.post3, == 2.*

~= 1.4.5a4 = 1.4.5a4, == 1.4.*

这个例子清楚地表明 后缀 被忽略了。

以下要求不符合 0.0.2.dev0:

install_requires=['package~=0.0.2']  # ERROR: ResolutionImpossible

而此示例 确实 匹配稳定版本 0.0.2:

install_requires=['package~=0.0.2.dev0']  # OK - suffix ignored