Skip to content

Commit

Permalink
Merge branch 'main' into jinja-template
Browse files Browse the repository at this point in the history
  • Loading branch information
redeboer committed Oct 10, 2024
2 parents c5c7d91 + 08274b8 commit 93beba6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 37 deletions.
10 changes: 3 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,16 @@ classifiers = [
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python",
"Topic :: Documentation",
"Topic :: Utilities",
"Typing :: Typed",
]
dependencies = [
"Sphinx>=4.4",
"colorama",
"docutils",
'importlib-metadata; python_version <"3.8.0"',
'typing-extensions; python_version <"3.8.0"',
"sphinx",
]
description = "Relink type hints in your Sphinx API"
dynamic = ["version"]
Expand All @@ -42,13 +38,13 @@ keywords = [
license = {text = "BSD 3-Clause License"}
maintainers = [{email = "[email protected]"}]
name = "sphinx-api-relink"
requires-python = ">=3.7"
requires-python = ">=3.9"

[project.optional-dependencies]
dev = ["sphinx-api-relink[sty]"]
sty = [
"mypy",
"pre-commit >=1.4.0",
"pre-commit",
"ruff",
"sphinx-api-relink[types]",
]
Expand Down
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
40 changes: 20 additions & 20 deletions src/sphinx_api_relink/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@
import os
import re
import sys
from functools import lru_cache
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 Expand Up @@ -141,7 +141,7 @@ def set_intersphinx_version_remapping(
__VERSION_REMAPPING.update(version_remapping)


@lru_cache(maxsize=None)
@cache
def print_once(message: str, *, color: str = Fore.RED) -> None:
colored_text = f"{color}{message}{Style.RESET_ALL}"
print(colored_text) # noqa: T201
8 changes: 4 additions & 4 deletions src/sphinx_api_relink/linkcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import inspect
import subprocess
import sys
from functools import lru_cache
from functools import cache, lru_cache
from os.path import dirname, relpath
from typing import TYPE_CHECKING, Any, Callable, TypedDict
from urllib.parse import quote
Expand Down Expand Up @@ -66,13 +66,13 @@ def _get_path(domain: str, info: LinkcodeInfo, debug: bool) -> str | None:
return f"{path}#{linenumbers}"


@lru_cache(maxsize=None)
@cache
def _get_package(module_name: str) -> ModuleType:
package_name = module_name.split(".")[0]
return __get_module(package_name)


@lru_cache(maxsize=None)
@cache
def __get_module(module_name: str) -> ModuleType:
module = sys.modules.get(module_name)
if module is None:
Expand Down Expand Up @@ -158,7 +158,7 @@ def _get_latest_tag() -> str | None:
return None


@lru_cache(maxsize=None)
@cache
def _url_exists(url: str) -> bool:
try:
response = requests.head(url, timeout=5)
Expand Down

0 comments on commit 93beba6

Please sign in to comment.