Use importlib.metadata on Python 3.10+

Favor importlib.metadata.packages_distributions() over
importlib_metadata.packages_distributions() where it is available, i.e.,
on Python 3.10 and later.

Depend on PyPI importlib_metadata only on Python 3.9 and older:
conditionalize the requirements.txt entry on Python version.

Change-Id: I624fc0059057de05bb2bacd9c1238cba71ebad2a
This commit is contained in:
Benjamin A. Beasley 2022-09-28 08:28:18 -04:00 committed by Takashi Kajinami
parent 3ee0725a09
commit 156e1eb0b9
2 changed files with 9 additions and 5 deletions

View File

@ -13,7 +13,13 @@
import abc
import inspect
import importlib_metadata
try:
# Python 3.10 and later
from importlib.metadata import packages_distributions
except ImportError:
# Python 3.9 and older
from importlib_metadata import packages_distributions
from stevedore import extension
from cliff import _argparse
@ -34,8 +40,7 @@ def _get_distributions_by_modules():
# There can be multiple distribution in the case of namespace packages
# so we'll just grab the first one
_dists_by_mods = {
k: v[0] for k, v in
importlib_metadata.packages_distributions().items()
k: v[0] for k, v in packages_distributions().items()
}
return _dists_by_mods

View File

@ -1,6 +1,5 @@
autopage>=0.4.0 # Apache 2.0
# TODO: Drop this when Python 3.10 is our minimum supported version
importlib_metadata>=4.4 # Apache-2.0
importlib_metadata>=4.4;python_version<"3.10" # Apache-2.0
cmd2>=1.0.0 # MIT
PrettyTable>=0.7.2 # BSD
stevedore>=2.0.1 # Apache-2.0