Skip to content

Commit

Permalink
Merge pull request #2108 from chrysle/add-compat-modules
Browse files Browse the repository at this point in the history
Add compat modules for `tomllib` and `importlib.metadata`
  • Loading branch information
webknjaz authored Dec 16, 2024
2 parents d7b0e8b + 485caa0 commit 2a6db55
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 29 deletions.
2 changes: 2 additions & 0 deletions piptools/_compat/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from .importlib_metadata import PackageMetadata
from .pip_compat import (
Distribution,
create_wheel_cache,
Expand All @@ -12,4 +13,5 @@
"parse_requirements",
"create_wheel_cache",
"get_dev_pkgs",
"PackageMetadata",
]
17 changes: 17 additions & 0 deletions piptools/_compat/importlib_metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from __future__ import annotations

import sys

if sys.version_info >= (3, 10):
from importlib.metadata import PackageMetadata
else:
from typing import Any, Protocol, TypeVar, overload

_T = TypeVar("_T")

class PackageMetadata(Protocol):
@overload
def get_all(self, name: str, failobj: None = None) -> list[Any] | None: ...

@overload
def get_all(self, name: str, failobj: _T) -> list[Any] | _T: ...
11 changes: 11 additions & 0 deletions piptools/_compat/tomllib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from __future__ import annotations

import sys

if sys.version_info >= (3, 11):
from tomllib import TOMLDecodeError, load, loads
else:
from tomli import TOMLDecodeError, load, loads


__all__ = ["loads", "load", "TOMLDecodeError"]
23 changes: 2 additions & 21 deletions piptools/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
import contextlib
import os
import pathlib
import sys
import tempfile
from dataclasses import dataclass
from importlib import metadata as importlib_metadata
from typing import Any, Iterator, Protocol, TypeVar, overload
from typing import Iterator

import build
import build.env
Expand All @@ -18,29 +17,11 @@
from pip._vendor.packaging.markers import Marker
from pip._vendor.packaging.requirements import Requirement

from ._compat import PackageMetadata, tomllib
from .utils import copy_install_requirement, install_req_from_line

if sys.version_info >= (3, 11):
import tomllib
else:
import tomli as tomllib

PYPROJECT_TOML = "pyproject.toml"

_T = TypeVar("_T")


if sys.version_info >= (3, 10):
from importlib.metadata import PackageMetadata
else:

class PackageMetadata(Protocol):
@overload
def get_all(self, name: str, failobj: None = None) -> list[Any] | None: ...

@overload
def get_all(self, name: str, failobj: _T) -> list[Any] | _T: ...


@dataclass
class StaticProjectMetadata:
Expand Down
11 changes: 3 additions & 8 deletions piptools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,12 @@
import os
import re
import shlex
import sys
from pathlib import Path
from typing import Any, Callable, Iterable, Iterator, TypeVar, cast

from click.core import ParameterSource

if sys.version_info >= (3, 11):
import tomllib
else:
import tomli as tomllib

import click
import pip
from click.core import ParameterSource
from click.utils import LazyFile
from pip._internal.req import InstallRequirement
from pip._internal.req.constructors import (
Expand All @@ -40,6 +33,8 @@
from piptools.locations import DEFAULT_CONFIG_FILE_NAMES
from piptools.subprocess_utils import run_python_snippet

from ._compat import tomllib

_KT = TypeVar("_KT")
_VT = TypeVar("_VT")
_T = TypeVar("_T")
Expand Down

0 comments on commit 2a6db55

Please sign in to comment.