Skip to content

Commit df1d357

Browse files
chryslewebknjaz
authored andcommitted
Add compat modules for tomllib and importlib.metadata
1 parent d7b0e8b commit df1d357

File tree

5 files changed

+35
-29
lines changed

5 files changed

+35
-29
lines changed

piptools/_compat/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
from .importlib_metadata import PackageMetadata
34
from .pip_compat import (
45
Distribution,
56
create_wheel_cache,
@@ -12,4 +13,5 @@
1213
"parse_requirements",
1314
"create_wheel_cache",
1415
"get_dev_pkgs",
16+
"PackageMetadata",
1517
]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from __future__ import annotations
2+
3+
import sys
4+
5+
if sys.version_info >= (3, 10):
6+
from importlib.metadata import PackageMetadata
7+
else:
8+
from typing import Any, Protocol, TypeVar, overload
9+
10+
_T = TypeVar("_T")
11+
12+
class PackageMetadata(Protocol):
13+
@overload
14+
def get_all(self, name: str, failobj: None = None) -> list[Any] | None: ...
15+
16+
@overload
17+
def get_all(self, name: str, failobj: _T) -> list[Any] | _T: ...

piptools/_compat/tomllib.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from __future__ import annotations
2+
3+
import sys
4+
5+
if sys.version_info >= (3, 11):
6+
from tomllib import TOMLDecodeError, load, loads
7+
else:
8+
from tomli import TOMLDecodeError, load, loads
9+
10+
11+
__all__ = ["loads", "load", "TOMLDecodeError"]

piptools/build.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
import contextlib
55
import os
66
import pathlib
7-
import sys
87
import tempfile
98
from dataclasses import dataclass
109
from importlib import metadata as importlib_metadata
11-
from typing import Any, Iterator, Protocol, TypeVar, overload
10+
from typing import Iterator
1211

1312
import build
1413
import build.env
@@ -18,29 +17,11 @@
1817
from pip._vendor.packaging.markers import Marker
1918
from pip._vendor.packaging.requirements import Requirement
2019

20+
from ._compat import PackageMetadata, tomllib
2121
from .utils import copy_install_requirement, install_req_from_line
2222

23-
if sys.version_info >= (3, 11):
24-
import tomllib
25-
else:
26-
import tomli as tomllib
27-
2823
PYPROJECT_TOML = "pyproject.toml"
2924

30-
_T = TypeVar("_T")
31-
32-
33-
if sys.version_info >= (3, 10):
34-
from importlib.metadata import PackageMetadata
35-
else:
36-
37-
class PackageMetadata(Protocol):
38-
@overload
39-
def get_all(self, name: str, failobj: None = None) -> list[Any] | None: ...
40-
41-
@overload
42-
def get_all(self, name: str, failobj: _T) -> list[Any] | _T: ...
43-
4425

4526
@dataclass
4627
class StaticProjectMetadata:

piptools/utils.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,12 @@
88
import os
99
import re
1010
import shlex
11-
import sys
1211
from pathlib import Path
1312
from typing import Any, Callable, Iterable, Iterator, TypeVar, cast
1413

15-
from click.core import ParameterSource
16-
17-
if sys.version_info >= (3, 11):
18-
import tomllib
19-
else:
20-
import tomli as tomllib
21-
2214
import click
2315
import pip
16+
from click.core import ParameterSource
2417
from click.utils import LazyFile
2518
from pip._internal.req import InstallRequirement
2619
from pip._internal.req.constructors import (
@@ -40,6 +33,8 @@
4033
from piptools.locations import DEFAULT_CONFIG_FILE_NAMES
4134
from piptools.subprocess_utils import run_python_snippet
4235

36+
from ._compat import tomllib
37+
4338
_KT = TypeVar("_KT")
4439
_VT = TypeVar("_VT")
4540
_T = TypeVar("_T")

0 commit comments

Comments
 (0)