TL; DR
>>> from packaging.specifiers import SpecifierSet
>>> '0.18.1.0' in SpecifierSet('== 0.18.1')
True
pip
の version specifier
pip
には、npm
など他のパッケージマネージャ同様、バージョンを柔軟に指定するための仕組みがあります。
もっともよく使われるのは ==
でしょうか。
pip install flake8-import-order==0.18
この他にも、下記の6種類の比較演算子が使えます。
~=
: Compatible release clause==
: Version matching clause!=
: Version exclusion clause<=
,>=
: Inclusive ordered comparison clause<
,>
: Exclusive ordered comparison clause===
: Arbitrary equality clause.
この version はこの specifier にマッチするか問題
さて、問題です。== 0.18.1
は 0.18.1.0
にマッチするでしょうか? === 0.18.1
だったら? ~= 0.18.1
だったら?
詳細な仕様は上記ドキュメントに記載されていますが、細かい挙動がわりと複雑で、意外とすんなりとは理解しづらいものです(本記事の目的から外れるので解説はしません)。
そんなときは、pip
が使っている pypa/packaging をそのまま使いましょう!
>>> from packaging.specifiers import SpecifierSet
>>> '0.18.1.0' in SpecifierSet('== 0.18.1')
True
>>> for spec in ['== 0.18.1', '== 0.18.*', '~= 0.18.1', '=== 0.18.1']:
... for version in ['0.18.0', '0.18.1', '0.18.1.0', '0.18.2', '0.19']:
... match = version in SpecifierSet(spec)
... print('{:10s}{:10s} ... {}'.format(version, spec, match))
... print('-' * 30)
...
0.18.0 == 0.18.1 ... False
0.18.1 == 0.18.1 ... True
0.18.1.0 == 0.18.1 ... True
0.18.2 == 0.18.1 ... False
0.19 == 0.18.1 ... False
------------------------------
0.18.0 == 0.18.* ... True
0.18.1 == 0.18.* ... True
0.18.1.0 == 0.18.* ... True
0.18.2 == 0.18.* ... True
0.19 == 0.18.* ... False
------------------------------
0.18.0 ~= 0.18.1 ... False
0.18.1 ~= 0.18.1 ... True
0.18.1.0 ~= 0.18.1 ... True
0.18.2 ~= 0.18.1 ... True
0.19 ~= 0.18.1 ... False
------------------------------
0.18.0 === 0.18.1 ... False
0.18.1 === 0.18.1 ... True
0.18.1.0 === 0.18.1 ... False
0.18.2 === 0.18.1 ... False
0.19 === 0.18.1 ... False
------------------------------
公式へのリンク
-
ドキュメント https://packaging.pypa.io/en/latest/specifiers/
-
ソースコード https://github.com/pypa/packaging/blob/master/packaging/specifiers.py
~=
を使おう
書きたいことはここまでですが、最後に補足を一つ。
前述の6種類の中では ==
が使われがちではありますが、公式には推奨されていません。
The use of == (without at least the wildcard suffix) when defining dependencies for published distributions is strongly discouraged as it greatly complicates the deployment of security fixes.
拙訳
== を(少なくともワイルドカードなしで)公開パッケージに対して使うと、セキュリティフィックスのデプロイが極めて煩雑になります。そのため、できる限り使用しないことをおすすめします。
かわりに、~=
(compatible release operator) を使うようにしましょう。
これにより
- セキュリティフィックス・バグフィックスに追随しやすい
- 仕様変更に振り回されにくい
の2点を両立できます。