Skip to content

Commit

Permalink
ENH: get pin() version from importlib if no constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
redeboer committed Oct 10, 2024
1 parent b329ae1 commit aeddf70
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/sphinx_api_relink/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import re
import sys
from functools import cache
from importlib.metadata import version
from importlib.metadata import PackageNotFoundError, version

from colorama import Fore, Style

Expand Down Expand Up @@ -55,14 +55,24 @@ def pin(
) -> str:
if version_remapping is None:
version_remapping = __VERSION_REMAPPING
package_name = package_name.lower()
installed_version = _get_version_from_constraints(package_name)
if installed_version is None:
try:
installed_version = version(package_name)
except PackageNotFoundError:
return "stable"
remapped_versions = version_remapping.get(package_name)
if remapped_versions is None:
return installed_version
return remapped_versions.get(installed_version, installed_version)


def _get_version_from_constraints(package_name: str) -> str | None:
python_version = f"{sys.version_info.major}.{sys.version_info.minor}"
constraints_path = f"../.constraints/py{python_version}.txt"
if not os.path.exists(constraints_path):
msg = (
f"Could not find {constraints_path}. Did you pin your constraints with"
" https://github.com/ComPWA/update-pip-constraints?"
)
raise FileNotFoundError(msg)
return None
with open(constraints_path) as stream:
constraints = stream.read()
package_name = package_name.lower()
Expand All @@ -78,14 +88,8 @@ def pin(
if len(line_segments) != 2: # noqa: PLR2004
continue
_, installed_version, *_ = line_segments
installed_version = installed_version.strip()
remapped_versions = version_remapping.get(package_name)
if remapped_versions is not None:
existing_version = remapped_versions.get(installed_version)
if existing_version is not None:
return existing_version
return installed_version
return "stable"
return installed_version.strip()
return None


def pin_minor(package_name: str) -> str:
Expand Down

0 comments on commit aeddf70

Please sign in to comment.