Skip to content

Commit 20e8bbd

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 012f123 commit 20e8bbd

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

amaranth/__init__.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
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-
from importlib import metadata as importlib_metadata
3-
__version__ = importlib_metadata.version(__package__)
4-
del importlib_metadata
5-
except ImportError:
6-
# No importlib_metadata. This shouldn't normally happen, but some people prefer not installing
7-
# packages via pip at all, instead using PYTHONPATH directly or copying the package files into
8-
# `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.
99
__version__ = "unknown" # :nocov:
10+
del importlib
1011

1112

1213
from .hdl import *

0 commit comments

Comments
 (0)