27
27
if sys .version_info < (3 , 8 ): # noqa: UP036 # Check for unsupported versions
28
28
raise RuntimeError ("Python 3.8 or later is required" )
29
29
30
- import os
30
+ import _imp
31
+ import collections
32
+ import email .parser
33
+ import errno
34
+ import functools
35
+ import importlib
36
+ import importlib .abc
37
+ import importlib .machinery
38
+ import inspect
31
39
import io
32
- import time
40
+ import ntpath
41
+ import operator
42
+ import os
43
+ import pkgutil
44
+ import platform
45
+ import plistlib
46
+ import posixpath
33
47
import re
48
+ import stat
49
+ import tempfile
50
+ import textwrap
51
+ import time
34
52
import types
53
+ import warnings
54
+ import zipfile
55
+ import zipimport
56
+ from pkgutil import get_importer
35
57
from typing import (
58
+ TYPE_CHECKING ,
36
59
Any ,
37
60
BinaryIO ,
38
- Literal ,
61
+ Callable ,
39
62
Dict ,
63
+ Iterable ,
40
64
Iterator ,
65
+ Literal ,
41
66
Mapping ,
42
67
MutableSequence ,
43
68
NamedTuple ,
44
69
NoReturn ,
45
- Tuple ,
46
- Union ,
47
- TYPE_CHECKING ,
48
70
Protocol ,
49
- Callable ,
50
- Iterable ,
71
+ Tuple ,
51
72
TypeVar ,
73
+ Union ,
52
74
overload ,
53
75
)
54
- import zipfile
55
- import zipimport
56
- import warnings
57
- import stat
58
- import functools
59
- import pkgutil
60
- import operator
61
- import platform
62
- import collections
63
- import plistlib
64
- import email .parser
65
- import errno
66
- import tempfile
67
- import textwrap
68
- import inspect
69
- import ntpath
70
- import posixpath
71
- import importlib
72
- import importlib .abc
73
- import importlib .machinery
74
- from pkgutil import get_importer
75
-
76
- import _imp
77
76
78
77
sys .path .extend (((vendor_path := os .path .join (os .path .dirname (os .path .dirname (__file__ )), 'setuptools' , '_vendor' )) not in sys .path ) * [vendor_path ]) # fmt: skip
79
78
# workaround for #4476
80
79
sys .modules .pop ('backports' , None )
81
80
82
81
# capture these to bypass sandboxing
83
- from os import utime
84
- from os import open as os_open
85
- from os .path import isdir , split
82
+ from os import open as os_open , utime # isort: skip
83
+ from os .path import isdir , split # isort: skip
86
84
87
85
try :
88
86
from os import mkdir , rename , unlink
92
90
# no write support, probably under GAE
93
91
WRITE_SUPPORT = False
94
92
93
+ import packaging .markers
94
+ import packaging .requirements
95
95
import packaging .specifiers
96
- from jaraco .text import (
97
- yield_lines ,
98
- drop_comment ,
99
- join_continuation ,
100
- )
101
- from packaging import markers as _packaging_markers
102
- from packaging import requirements as _packaging_requirements
103
- from packaging import utils as _packaging_utils
104
- from packaging import version as _packaging_version
96
+ import packaging .utils
97
+ import packaging .version
98
+ from jaraco .text import drop_comment , join_continuation , yield_lines
105
99
from platformdirs import user_cache_dir as _user_cache_dir
106
100
107
101
if TYPE_CHECKING :
108
- from _typeshed import BytesPath , StrPath , StrOrBytesPath
109
- from typing_extensions import Self
102
+ from _typeshed import BytesPath , StrOrBytesPath , StrPath
103
+ from typing_extensions import Self , TypeAlias
110
104
111
105
warnings .warn (
112
106
"pkg_resources is deprecated as an API. "
118
112
_T = TypeVar ("_T" )
119
113
_DistributionT = TypeVar ("_DistributionT" , bound = "Distribution" )
120
114
# Type aliases
121
- _NestedStr = Union [str , Iterable [Union [str , Iterable ["_NestedStr" ]]]]
122
- _InstallerTypeT = Callable [["Requirement" ], "_DistributionT" ]
123
- _InstallerType = Callable [["Requirement" ], Union ["Distribution" , None ]]
124
- _PkgReqType = Union [str , "Requirement" ]
125
- _EPDistType = Union ["Distribution" , _PkgReqType ]
126
- _MetadataType = Union ["IResourceProvider" , None ]
127
- _ResolvedEntryPoint = Any # Can be any attribute in the module
128
- _ResourceStream = Any # TODO / Incomplete: A readable file-like object
115
+ _NestedStr : TypeAlias = Union [str , Iterable [Union [str , Iterable ["_NestedStr" ]]]]
116
+ _StrictInstallerType : TypeAlias = Callable [["Requirement" ], "_DistributionT" ]
117
+ _InstallerType : TypeAlias = Callable [["Requirement" ], Union ["Distribution" , None ]]
118
+ _PkgReqType : TypeAlias = Union [str , "Requirement" ]
119
+ _EPDistType : TypeAlias = Union ["Distribution" , _PkgReqType ]
120
+ _MetadataType : TypeAlias = Union ["IResourceProvider" , None ]
121
+ _ResolvedEntryPoint : TypeAlias = Any # Can be any attribute in the module
122
+ _ResourceStream : TypeAlias = Any # TODO / Incomplete: A readable file-like object
129
123
# Any object works, but let's indicate we expect something like a module (optionally has __loader__ or __file__)
130
- _ModuleLike = Union [object , types .ModuleType ]
124
+ _ModuleLike : TypeAlias = Union [object , types .ModuleType ]
131
125
# Any: Should be _ModuleLike but we end up with issues where _ModuleLike doesn't have _ZipLoaderModule's __loader__
132
- _ProviderFactoryType = Callable [[Any ], "IResourceProvider" ]
133
- _DistFinderType = Callable [[_T , str , bool ], Iterable ["Distribution" ]]
134
- _NSHandlerType = Callable [[_T , str , str , types .ModuleType ], Union [str , None ]]
126
+ _ProviderFactoryType : TypeAlias = Callable [[Any ], "IResourceProvider" ]
127
+ _DistFinderType : TypeAlias = Callable [[_T , str , bool ], Iterable ["Distribution" ]]
128
+ _NSHandlerType : TypeAlias = Callable [[_T , str , str , types .ModuleType ], Union [str , None ]]
135
129
_AdapterT = TypeVar (
136
130
"_AdapterT" , _DistFinderType [Any ], _ProviderFactoryType , _NSHandlerType [Any ]
137
131
)
@@ -156,7 +150,7 @@ class PEP440Warning(RuntimeWarning):
156
150
"""
157
151
158
152
159
- parse_version = _packaging_version .Version
153
+ parse_version = packaging . version .Version
160
154
161
155
_state_vars : dict [str , str ] = {}
162
156
@@ -801,7 +795,7 @@ def add(
801
795
return
802
796
803
797
self .by_key [dist .key ] = dist
804
- normalized_name = _packaging_utils .canonicalize_name (dist .key )
798
+ normalized_name = packaging . utils .canonicalize_name (dist .key )
805
799
self .normalized_to_canonical_keys [normalized_name ] = dist .key
806
800
if dist .key not in keys :
807
801
keys .append (dist .key )
@@ -814,7 +808,7 @@ def resolve(
814
808
self ,
815
809
requirements : Iterable [Requirement ],
816
810
env : Environment | None ,
817
- installer : _InstallerTypeT [_DistributionT ],
811
+ installer : _StrictInstallerType [_DistributionT ],
818
812
replace_conflicting : bool = False ,
819
813
extras : tuple [str , ...] | None = None ,
820
814
) -> list [_DistributionT ]: ...
@@ -824,7 +818,7 @@ def resolve(
824
818
requirements : Iterable [Requirement ],
825
819
env : Environment | None = None ,
826
820
* ,
827
- installer : _InstallerTypeT [_DistributionT ],
821
+ installer : _StrictInstallerType [_DistributionT ],
828
822
replace_conflicting : bool = False ,
829
823
extras : tuple [str , ...] | None = None ,
830
824
) -> list [_DistributionT ]: ...
@@ -841,7 +835,7 @@ def resolve(
841
835
self ,
842
836
requirements : Iterable [Requirement ],
843
837
env : Environment | None = None ,
844
- installer : _InstallerType | None | _InstallerTypeT [_DistributionT ] = None ,
838
+ installer : _InstallerType | None | _StrictInstallerType [_DistributionT ] = None ,
845
839
replace_conflicting : bool = False ,
846
840
extras : tuple [str , ...] | None = None ,
847
841
) -> list [Distribution ] | list [_DistributionT ]:
@@ -947,7 +941,7 @@ def find_plugins(
947
941
self ,
948
942
plugin_env : Environment ,
949
943
full_env : Environment | None ,
950
- installer : _InstallerTypeT [_DistributionT ],
944
+ installer : _StrictInstallerType [_DistributionT ],
951
945
fallback : bool = True ,
952
946
) -> tuple [list [_DistributionT ], dict [Distribution , Exception ]]: ...
953
947
@overload
@@ -956,7 +950,7 @@ def find_plugins(
956
950
plugin_env : Environment ,
957
951
full_env : Environment | None = None ,
958
952
* ,
959
- installer : _InstallerTypeT [_DistributionT ],
953
+ installer : _StrictInstallerType [_DistributionT ],
960
954
fallback : bool = True ,
961
955
) -> tuple [list [_DistributionT ], dict [Distribution , Exception ]]: ...
962
956
@overload
@@ -971,7 +965,7 @@ def find_plugins(
971
965
self ,
972
966
plugin_env : Environment ,
973
967
full_env : Environment | None = None ,
974
- installer : _InstallerType | None | _InstallerTypeT [_DistributionT ] = None ,
968
+ installer : _InstallerType | None | _StrictInstallerType [_DistributionT ] = None ,
975
969
fallback : bool = True ,
976
970
) -> tuple [
977
971
list [Distribution ] | list [_DistributionT ],
@@ -1217,7 +1211,7 @@ def best_match(
1217
1211
self ,
1218
1212
req : Requirement ,
1219
1213
working_set : WorkingSet ,
1220
- installer : _InstallerTypeT [_DistributionT ],
1214
+ installer : _StrictInstallerType [_DistributionT ],
1221
1215
replace_conflicting : bool = False ,
1222
1216
) -> _DistributionT : ...
1223
1217
@overload
@@ -1232,7 +1226,7 @@ def best_match(
1232
1226
self ,
1233
1227
req : Requirement ,
1234
1228
working_set : WorkingSet ,
1235
- installer : _InstallerType | None | _InstallerTypeT [_DistributionT ] = None ,
1229
+ installer : _InstallerType | None | _StrictInstallerType [_DistributionT ] = None ,
1236
1230
replace_conflicting : bool = False ,
1237
1231
) -> Distribution | None :
1238
1232
"""Find distribution best matching `req` and usable on `working_set`
@@ -1265,7 +1259,7 @@ def best_match(
1265
1259
def obtain (
1266
1260
self ,
1267
1261
requirement : Requirement ,
1268
- installer : _InstallerTypeT [_DistributionT ],
1262
+ installer : _StrictInstallerType [_DistributionT ],
1269
1263
) -> _DistributionT : ...
1270
1264
@overload
1271
1265
def obtain (
@@ -1285,7 +1279,7 @@ def obtain(
1285
1279
installer : Callable [[Requirement ], None ]
1286
1280
| _InstallerType
1287
1281
| None
1288
- | _InstallerTypeT [_DistributionT ] = None ,
1282
+ | _StrictInstallerType [_DistributionT ] = None ,
1289
1283
) -> Distribution | None :
1290
1284
"""Obtain a distribution matching `requirement` (e.g. via download)
1291
1285
@@ -1561,8 +1555,8 @@ def safe_version(version: str) -> str:
1561
1555
"""
1562
1556
try :
1563
1557
# normalize the version
1564
- return str (_packaging_version .Version (version ))
1565
- except _packaging_version .InvalidVersion :
1558
+ return str (packaging . version .Version (version ))
1559
+ except packaging . version .InvalidVersion :
1566
1560
version = version .replace (' ' , '.' )
1567
1561
return re .sub ('[^A-Za-z0-9.]+' , '-' , version )
1568
1562
@@ -1639,9 +1633,9 @@ def evaluate_marker(text: str, extra: str | None = None) -> bool:
1639
1633
This implementation uses the 'pyparsing' module.
1640
1634
"""
1641
1635
try :
1642
- marker = _packaging_markers .Marker (text )
1636
+ marker = packaging . markers .Marker (text )
1643
1637
return marker .evaluate ()
1644
- except _packaging_markers .InvalidMarker as e :
1638
+ except packaging . markers .InvalidMarker as e :
1645
1639
raise SyntaxError (e ) from e
1646
1640
1647
1641
@@ -3001,20 +2995,20 @@ def parsed_version(self):
3001
2995
if not hasattr (self , "_parsed_version" ):
3002
2996
try :
3003
2997
self ._parsed_version = parse_version (self .version )
3004
- except _packaging_version .InvalidVersion as ex :
2998
+ except packaging . version .InvalidVersion as ex :
3005
2999
info = f"(package: { self .project_name } )"
3006
3000
if hasattr (ex , "add_note" ):
3007
3001
ex .add_note (info ) # PEP 678
3008
3002
raise
3009
- raise _packaging_version .InvalidVersion (f"{ str (ex )} { info } " ) from None
3003
+ raise packaging . version .InvalidVersion (f"{ str (ex )} { info } " ) from None
3010
3004
3011
3005
return self ._parsed_version
3012
3006
3013
3007
@property
3014
3008
def _forgiving_parsed_version (self ):
3015
3009
try :
3016
3010
return self .parsed_version
3017
- except _packaging_version .InvalidVersion as ex :
3011
+ except packaging . version .InvalidVersion as ex :
3018
3012
self ._parsed_version = parse_version (_forgiving_version (self .version ))
3019
3013
3020
3014
notes = "\n " .join (getattr (ex , "__notes__" , [])) # PEP 678
@@ -3194,7 +3188,7 @@ def from_filename(
3194
3188
3195
3189
def as_requirement (self ):
3196
3190
"""Return a ``Requirement`` that matches this distribution exactly"""
3197
- if isinstance (self .parsed_version , _packaging_version .Version ):
3191
+ if isinstance (self .parsed_version , packaging . version .Version ):
3198
3192
spec = "%s==%s" % (self .project_name , self .parsed_version )
3199
3193
else :
3200
3194
spec = "%s===%s" % (self .project_name , self .parsed_version )
@@ -3452,11 +3446,11 @@ def parse_requirements(strs: _NestedStr) -> map[Requirement]:
3452
3446
return map (Requirement , join_continuation (map (drop_comment , yield_lines (strs ))))
3453
3447
3454
3448
3455
- class RequirementParseError (_packaging_requirements .InvalidRequirement ):
3449
+ class RequirementParseError (packaging . requirements .InvalidRequirement ):
3456
3450
"Compatibility wrapper for InvalidRequirement"
3457
3451
3458
3452
3459
- class Requirement (_packaging_requirements .Requirement ):
3453
+ class Requirement (packaging . requirements .Requirement ):
3460
3454
# prefer variable length tuple to set (as found in
3461
3455
# packaging.requirements.Requirement)
3462
3456
extras : tuple [str , ...] # type: ignore[assignment]
0 commit comments