はじめに
七尾百合子さん、お誕生日 148日目 おめでとうございます! nikkieです。
Today I Learnedです。
目次
- はじめに
- 目次
- Pythonパッケージ名は正規化される
- ではextraは正規化される?
- PEP 685関係のドキュメントメモ
- 終わりに
- おまけ:Core metadata specifications 2.4への更新
Pythonパッケージ名は正規化される
「Python Packaging User Guide」で知っていました1。
「Name normalization(Names and normalization)」
The name should be lowercased with all runs of the characters ., -, or _ replaced with a single - character.
正規化の実装は re.sub(r"[-_.]+", "-", name).lower() です。
PEP 503が関連します。
挙がっている例より、「friendly-bard」と「FrIeNdLy-._.-bArD」は正規化すると同じです。
身近なところでは、pip installするときにawesome-package(ハイフン区切り)でもawesome_package(アンダースコア区切り)でも同じパッケージがインストールされます2
ではextraは正規化される?
extraとはpip install fabulous[extra]のように、[]で指定する追加の依存パッケージ群のことです。
結論から言うと、正規化されるようになりました。
以下のPEP 685(2022年)のおかげです
PEP 685により、PEP 503と同様の正規化がextraにも適用されます3。
re.sub(r"[-_.]+", "-", name).lower()
実は上で引いた「Names and normalization」にも
This specification defines the format that names for packages and extras are required to follow. It also describes how to normalize them, (略)
とextraも同様に正規化されることが示されています
PEP 685関係のドキュメントメモ
なんと以前はextraのハイフンとアンダースコアが区別されていたようです。
2021年の投稿 https://discuss.python.org/t/what-extras-names-are-treated-as-equal-and-why/7614
(これがPEP 685の契機となりました)
it(※pip) does not treat
webscrapbook[adhoc_ssl]andwebscrapbook[adhoc-ssl]equally 🤯
Core metadata specificationsも見ます。
Historyを見ると、PEP 685を機に2.3に上がっています
March 2022: Core metadata 2.3 was approved through PEP 685.
メタデータのうち Provides-Extra より
When writing data for older metadata versions, names MUST be normalized following the same rules used for the
Name:field when performing comparisons.
Nameのnormalizeでは、「Names and normalization」にリンクしています
終わりに
Pythonのパッケージ名はPEP 503で正規化され、ハイフンとアンダースコアは区別されません。
2022年のPEP 685で、extraも同様に正規化され、ハイフンとアンダースコアは区別されないようになりました。
この記事のきっかけは SQLAlchemy です。
PyPIのProvides-Extraでは「postgresql-psycopgbinary」とハイフン区切り、他方私が追っていたコード4ではsetup.cfgに「postgresql_psycopgbinary」とアンダースコア区切りで書かれていました。
これがパッケージ名なら正規化されて同じですが、extraの場合も同じなのかと調べて、PEP 685を知りました。
おまけ:Core metadata specifications 2.4への更新
https://packaging.python.org/en/latest/specifications/core-metadata/#history
August 2024: Core metadata 2.4 was approved through PEP 639.
こちらのツイートにつながりました!
pyproject.tomlにlicense = "Apache-2.0"ってあるのにclassifierで"License :: OSI Approved :: Apache Software License"って書いたらsetuptoolsに怒られた
— nikkie(にっきー) / にっP (@ftnext) 2025年4月12日
Apache-2.0がSPDX license expression syntax(PEP 639提案)で、同時にDeprecate license classifiersなんですねhttps://t.co/zvnwqrUp3v
- 日本語訳もあります。https://packaging.python.org/ja/latest/specifications/name-normalization/↩
- なお、importする際はアンダースコアに限ります。詳しくは、identifier↩
- https://peps.python.org/pep-0685/#specification↩
- こちらの記事参照 ↩