Skip to content

Commit 19349ab

Browse files
authored
Fixed the bug where pdl.__version__ was not set (#882)
It wasn't set previously because importlib searches for the distribution name and not the module's top-level name. Fallbacks are in place to revert to searching for 'pdl', and if that fails, it fallsback to the hardcoded version in _version.py Signed-off-by: Abi Ullattil <[email protected]>
1 parent d62cad5 commit 19349ab

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

src/pdl/__init__.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
11
from importlib.metadata import PackageNotFoundError, version
22

3-
try:
4-
__version__ = version("pdl")
5-
except PackageNotFoundError:
6-
pass
3+
from ._version import __version__ as hardcoded_version
4+
5+
6+
def _get_distribution_version(distribution_name: str) -> str:
7+
"""
8+
This function attempts to retrieve the version of PDL package using
9+
importlib.metadata.
10+
11+
When the package is not installed, importlib will raise a PackageNotFoundError.
12+
In this case, we fallback to the hardcoded version.
13+
14+
When the package is installed, but the distribution name does not match,
15+
importlib will return the version of the package that is installed.
16+
"""
17+
try:
18+
return version(distribution_name)
19+
except PackageNotFoundError:
20+
# This is a fallback for when the package is not recognized by importlib.metadata.
21+
# This can happen when the package is not installed.
22+
return (
23+
hardcoded_version
24+
if distribution_name == "pdl"
25+
else _get_distribution_version(
26+
"pdl" # This is a fallback to maintain the previous behavior.
27+
)
28+
)
29+
30+
31+
__version__ = _get_distribution_version("prompt-declaration-language")

tests/test_lib_version.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import pytest
2+
3+
import pdl
4+
5+
6+
def test_version():
7+
# Make sure the __version__ attribute is available.
8+
assert pdl.__version__ is not None
9+
10+
# Since pdl is not installed, the version returned will be the dev version.
11+
# NOTE: For some reason, the version is not the same as the hardcoded version.
12+
assert pdl.__version__.startswith("0.1.dev")
13+
14+
15+
if __name__ == "__main__":
16+
pytest.main(["-v", "--tb=short", __file__])

0 commit comments

Comments
 (0)