Skip to content

Commit dbeaaab

Browse files
committed
pylock: don't import typing_extensions at runtime
1 parent 84cb477 commit dbeaaab

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

src/pip/_internal/models/pylock.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import dataclasses
22
import logging
33
import re
4+
import sys
45
from dataclasses import dataclass
56
from pathlib import Path
67
from typing import (
8+
TYPE_CHECKING,
79
Any,
810
Dict,
911
Iterable,
@@ -20,20 +22,25 @@
2022
from pip._vendor.packaging.markers import Marker
2123
from pip._vendor.packaging.specifiers import SpecifierSet
2224
from pip._vendor.packaging.version import Version
23-
from pip._vendor.typing_extensions import Self
2425

2526
from pip._internal.models.direct_url import ArchiveInfo, DirInfo, VcsInfo
2627
from pip._internal.models.link import Link
2728
from pip._internal.req.req_install import InstallRequirement
2829
from pip._internal.utils.urls import url_to_path
2930

31+
if TYPE_CHECKING:
32+
if sys.version_info >= (3, 11):
33+
from typing import Self
34+
else:
35+
from pip._vendor.typing_extensions import Self
36+
3037
T = TypeVar("T")
3138
T2 = TypeVar("T2")
3239

3340

3441
class FromDictProtocol(Protocol):
3542
@classmethod
36-
def from_dict(cls, d: Dict[str, Any]) -> Self:
43+
def from_dict(cls, d: Dict[str, Any]) -> "Self":
3744
pass
3845

3946

@@ -218,7 +225,7 @@ def __post_init__(self) -> None:
218225
raise PylockValidationError("No path nor url set for vcs package")
219226

220227
@classmethod
221-
def from_dict(cls, d: Dict[str, Any]) -> Self:
228+
def from_dict(cls, d: Dict[str, Any]) -> "Self":
222229
return cls(
223230
type=_get_required(d, str, "type"),
224231
url=_get(d, str, "url"),
@@ -236,7 +243,7 @@ class PackageDirectory:
236243
subdirectory: Optional[str]
237244

238245
@classmethod
239-
def from_dict(cls, d: Dict[str, Any]) -> Self:
246+
def from_dict(cls, d: Dict[str, Any]) -> "Self":
240247
return cls(
241248
path=_get_required(d, str, "path"),
242249
editable=_get(d, bool, "editable"),
@@ -258,7 +265,7 @@ def __post_init__(self) -> None:
258265
raise PylockValidationError("No path nor url set for archive package")
259266

260267
@classmethod
261-
def from_dict(cls, d: Dict[str, Any]) -> Self:
268+
def from_dict(cls, d: Dict[str, Any]) -> "Self":
262269
return cls(
263270
url=_get(d, str, "url"),
264271
path=_get(d, str, "path"),
@@ -282,7 +289,7 @@ def __post_init__(self) -> None:
282289
raise PylockValidationError("No path nor url set for sdist package")
283290

284291
@classmethod
285-
def from_dict(cls, d: Dict[str, Any]) -> Self:
292+
def from_dict(cls, d: Dict[str, Any]) -> "Self":
286293
return cls(
287294
name=_get_required(d, str, "name"),
288295
url=_get(d, str, "url"),
@@ -306,7 +313,7 @@ def __post_init__(self) -> None:
306313
raise PylockValidationError("No path nor url set for wheel package")
307314

308315
@classmethod
309-
def from_dict(cls, d: Dict[str, Any]) -> Self:
316+
def from_dict(cls, d: Dict[str, Any]) -> "Self":
310317
wheel = cls(
311318
name=_get_required(d, str, "name"),
312319
url=_get(d, str, "url"),
@@ -349,7 +356,7 @@ def __post_init__(self) -> None:
349356
)
350357

351358
@classmethod
352-
def from_dict(cls, d: Dict[str, Any]) -> Self:
359+
def from_dict(cls, d: Dict[str, Any]) -> "Self":
353360
package = cls(
354361
name=_get_required(d, str, "name"),
355362
version=_get_as(d, str, Version, "version"),
@@ -364,7 +371,9 @@ def from_dict(cls, d: Dict[str, Any]) -> Self:
364371
return package
365372

366373
@classmethod
367-
def from_install_requirement(cls, ireq: InstallRequirement, base_dir: Path) -> Self:
374+
def from_install_requirement(
375+
cls, ireq: InstallRequirement, base_dir: Path
376+
) -> "Self":
368377
base_dir = base_dir.resolve()
369378
dist = ireq.get_dist()
370379
download_info = ireq.download_info
@@ -484,7 +493,7 @@ def to_dict(self) -> Dict[str, Any]:
484493
return dataclasses.asdict(self, dict_factory=_toml_dict_factory)
485494

486495
@classmethod
487-
def from_dict(cls, d: Dict[str, Any]) -> Self:
496+
def from_dict(cls, d: Dict[str, Any]) -> "Self":
488497
return cls(
489498
lock_version=_get_required_as(d, str, Version, "lock-version"),
490499
environments=_get_list_as(d, str, Marker, "environments"),
@@ -496,7 +505,7 @@ def from_dict(cls, d: Dict[str, Any]) -> Self:
496505
@classmethod
497506
def from_install_requirements(
498507
cls, install_requirements: Iterable[InstallRequirement], base_dir: Path
499-
) -> Self:
508+
) -> "Self":
500509
return cls(
501510
lock_version=Version("1.0"),
502511
environments=None, # not supported

0 commit comments

Comments
 (0)