Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions src/pdl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
from importlib.metadata import PackageNotFoundError, version

try:
__version__ = version("pdl")
except PackageNotFoundError:
pass
from ._version import __version__ as hardcoded_version


def _get_distribution_version(distribution_name: str) -> str:
"""
This function attempts to retrieve the version of PDL package using
importlib.metadata.

When the package is not installed, importlib will raise a PackageNotFoundError.
In this case, we fallback to the hardcoded version.

When the package is installed, but the distribution name does not match,
importlib will return the version of the package that is installed.
"""
try:
return version(distribution_name)
except PackageNotFoundError:
# This is a fallback for when the package is not recognized by importlib.metadata.
# This can happen when the package is not installed.
return (
hardcoded_version
if distribution_name == "pdl"
else _get_distribution_version(
"pdl" # This is a fallback to maintain the previous behavior.
)
)


__version__ = _get_distribution_version("prompt-declaration-language")
16 changes: 16 additions & 0 deletions tests/test_lib_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import pytest

import pdl


def test_version():
# Make sure the __version__ attribute is available.
assert pdl.__version__ is not None

# Since pdl is not installed, the version returned will be the dev version.
# NOTE: For some reason, the version is not the same as the hardcoded version.
assert pdl.__version__.startswith("0.1.dev")


if __name__ == "__main__":
pytest.main(["-v", "--tb=short", __file__])