Skip to content

Commit f77eba0

Browse files
committed
Bring __version__ retrieval up to date. NFCI
Because `importlib.metadata.PackageNotFoundError` inherits from `ImportError`, the code did not previously work in the way that was stated in the comment. We should probably deprecate `__version__` entirely at some point.
1 parent e228e26 commit f77eba0

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

amaranth_soc/__init__.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1+
# Extract version for this package from the environment package metadata. This used to be a lot
2+
# more difficult in earlier Python versions, and the `__version__` field is a legacy of that time.
3+
import importlib.metadata
14
try:
2-
try:
3-
from importlib import metadata as importlib_metadata # py3.8+ stdlib
4-
except ImportError:
5-
import importlib_metadata # py3.7- shim
6-
__version__ = importlib_metadata.version(__package__)
7-
except ImportError:
8-
# No importlib_metadata. This shouldn't normally happen, but some people prefer not installing
9-
# packages via pip at all, instead using PYTHONPATH directly or copying the package files into
10-
# `lib/pythonX.Y/site-packages`. Although not a recommended way, we still try to support it.
5+
__version__ = importlib.metadata.version(__package__)
6+
except importlib.metadata.PackageNotFoundError:
7+
# No importlib metadata for this package. This shouldn't normally happen, but some people
8+
# prefer not installing packages via pip at all. Although not recommended we still support it.
119
__version__ = "unknown" # :nocov:
10+
del importlib

0 commit comments

Comments
 (0)