DEV Community 👩‍💻👨‍💻

Yao-Ching Huang
Yao-Ching Huang

Posted on • Updated on

Python :: 重新認識compatible release operator

熟知的Semantic versioning建議讓軟體版本符合Major.Minor.Patch風格。

The “Major.Minor.Patch” (described in this PEP as “major.minor.micro”) aspects of semantic versioning (clauses 1-8 in the 2.0.0 specification) are fully compatible with the version scheme defined in this PEP, and abiding by these aspects is encouraged.

通常Patch版本代表該版本只修正bug,並不影響軟體的使用方式。所以我經常會用compatible release operator(~=)固定Major.Minor並自動下載最新的Patch version。

殊不知卻我誤用了很長一段時間...

以numpy為例,如果我想要下載numpy-1.20.*套件,pip install 'numpy~=1.20'pip install 'numpy~=1.20.0'的意義完全不同!

numpy~=1.20的版本要求等同於numpy >= 1.20, == 1.*,所以當下我會下載到numpy-1.23.3的版本套件。

而正確的語法應該是numpy~=1.20.0(numpy >= 1.20.0, == 1.20.*)。

僅缺少Patch版本,看似些微的差異,卻會對版本管理有很大的隱憂。除非事後特地用pip list仔細確認,否則平常也不會特地比較版本是否符合預期。

平常沒事多翻文件,不同階段回顧文件總會有不同程度的體會!

參考資料: PEP 440 https://peps.python.org/pep-0440/#compatible-release

Top comments (0)

Take a look at this:

Settings

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. 🛠