Skip to content

Commit

Permalink
ENH: get pin() version from importlib if no constraints (#29)
Browse files Browse the repository at this point in the history
* MAINT: remove `sys.version_info` import switch
  • Loading branch information
redeboer authored Oct 10, 2024
1 parent 2f948c5 commit 08274b8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
7 changes: 1 addition & 6 deletions src/sphinx_api_relink/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from __future__ import annotations

import shutil
import sys
from importlib.metadata import version
from pathlib import Path
from typing import TYPE_CHECKING, Any

Expand All @@ -13,11 +13,6 @@

from sphinx_api_relink.linkcode import get_linkcode_resolve

if sys.version_info < (3, 8):
from importlib_metadata import version
else:
from importlib.metadata import version

__SPHINX_VERSION = tuple(int(i) for i in version("sphinx").split(".") if i.isdigit())
if __SPHINX_VERSION < (7, 3):
from sphinx.domains.python import parse_reftarget # type:ignore[attr-defined]
Expand Down
36 changes: 18 additions & 18 deletions src/sphinx_api_relink/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@
import re
import sys
from functools import cache
from importlib.metadata import PackageNotFoundError, version

from colorama import Fore, Style

if sys.version_info < (3, 8):
from importlib_metadata import version
else:
from importlib.metadata import version

__DEFAULT_BRANCH = "main"
__VERSION_REMAPPING: dict[str, dict[str, str]] = {}

Expand Down Expand Up @@ -59,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 @@ -82,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 08274b8

Please sign in to comment.