Skip to content

Commit 1b27726

Browse files
committed
Update packaging to 20.4. Closes #2310.
1 parent 9d7b246 commit 1b27726

24 files changed

+1776
-384
lines changed

pkg_resources/_vendor/packaging/__about__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
__summary__ = "Core utilities for Python packages"
1919
__uri__ = "https://github.com/pypa/packaging"
2020

21-
__version__ = "19.2"
21+
__version__ = "20.4"
2222

2323
__author__ = "Donald Stufft and individual contributors"
2424
__email__ = "[email protected]"
2525

26-
__license__ = "BSD or Apache License, Version 2.0"
26+
__license__ = "BSD-2-Clause or Apache-2.0"
2727
__copyright__ = "Copyright 2014-2019 %s" % __author__

pkg_resources/_vendor/packaging/_compat.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55

66
import sys
77

8+
from ._typing import TYPE_CHECKING
9+
10+
if TYPE_CHECKING: # pragma: no cover
11+
from typing import Any, Dict, Tuple, Type
12+
813

914
PY2 = sys.version_info[0] == 2
1015
PY3 = sys.version_info[0] == 3
@@ -18,14 +23,16 @@
1823

1924

2025
def with_metaclass(meta, *bases):
26+
# type: (Type[Any], Tuple[Type[Any], ...]) -> Any
2127
"""
2228
Create a base class with a metaclass.
2329
"""
2430
# This requires a bit of explanation: the basic idea is to make a dummy
2531
# metaclass for one level of class instantiation that replaces itself with
2632
# the actual metaclass.
27-
class metaclass(meta):
33+
class metaclass(meta): # type: ignore
2834
def __new__(cls, name, this_bases, d):
35+
# type: (Type[Any], str, Tuple[Any], Dict[Any, Any]) -> Any
2936
return meta(name, bases, d)
3037

3138
return type.__new__(metaclass, "temporary_class", (), {})

pkg_resources/_vendor/packaging/_structures.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,83 @@
44
from __future__ import absolute_import, division, print_function
55

66

7-
class Infinity(object):
7+
class InfinityType(object):
88
def __repr__(self):
9+
# type: () -> str
910
return "Infinity"
1011

1112
def __hash__(self):
13+
# type: () -> int
1214
return hash(repr(self))
1315

1416
def __lt__(self, other):
17+
# type: (object) -> bool
1518
return False
1619

1720
def __le__(self, other):
21+
# type: (object) -> bool
1822
return False
1923

2024
def __eq__(self, other):
25+
# type: (object) -> bool
2126
return isinstance(other, self.__class__)
2227

2328
def __ne__(self, other):
29+
# type: (object) -> bool
2430
return not isinstance(other, self.__class__)
2531

2632
def __gt__(self, other):
33+
# type: (object) -> bool
2734
return True
2835

2936
def __ge__(self, other):
37+
# type: (object) -> bool
3038
return True
3139

3240
def __neg__(self):
41+
# type: (object) -> NegativeInfinityType
3342
return NegativeInfinity
3443

3544

36-
Infinity = Infinity()
45+
Infinity = InfinityType()
3746

3847

39-
class NegativeInfinity(object):
48+
class NegativeInfinityType(object):
4049
def __repr__(self):
50+
# type: () -> str
4151
return "-Infinity"
4252

4353
def __hash__(self):
54+
# type: () -> int
4455
return hash(repr(self))
4556

4657
def __lt__(self, other):
58+
# type: (object) -> bool
4759
return True
4860

4961
def __le__(self, other):
62+
# type: (object) -> bool
5063
return True
5164

5265
def __eq__(self, other):
66+
# type: (object) -> bool
5367
return isinstance(other, self.__class__)
5468

5569
def __ne__(self, other):
70+
# type: (object) -> bool
5671
return not isinstance(other, self.__class__)
5772

5873
def __gt__(self, other):
74+
# type: (object) -> bool
5975
return False
6076

6177
def __ge__(self, other):
78+
# type: (object) -> bool
6279
return False
6380

6481
def __neg__(self):
82+
# type: (object) -> InfinityType
6583
return Infinity
6684

6785

68-
NegativeInfinity = NegativeInfinity()
86+
NegativeInfinity = NegativeInfinityType()
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""For neatly implementing static typing in packaging.
2+
3+
`mypy` - the static type analysis tool we use - uses the `typing` module, which
4+
provides core functionality fundamental to mypy's functioning.
5+
6+
Generally, `typing` would be imported at runtime and used in that fashion -
7+
it acts as a no-op at runtime and does not have any run-time overhead by
8+
design.
9+
10+
As it turns out, `typing` is not vendorable - it uses separate sources for
11+
Python 2/Python 3. Thus, this codebase can not expect it to be present.
12+
To work around this, mypy allows the typing import to be behind a False-y
13+
optional to prevent it from running at runtime and type-comments can be used
14+
to remove the need for the types to be accessible directly during runtime.
15+
16+
This module provides the False-y guard in a nicely named fashion so that a
17+
curious maintainer can reach here to read this.
18+
19+
In packaging, all static-typing related imports should be guarded as follows:
20+
21+
from packaging._typing import TYPE_CHECKING
22+
23+
if TYPE_CHECKING:
24+
from typing import ...
25+
26+
Ref: https://github.com/python/mypy/issues/3216
27+
"""
28+
29+
__all__ = ["TYPE_CHECKING", "cast"]
30+
31+
# The TYPE_CHECKING constant defined by the typing module is False at runtime
32+
# but True while type checking.
33+
if False: # pragma: no cover
34+
from typing import TYPE_CHECKING
35+
else:
36+
TYPE_CHECKING = False
37+
38+
# typing's cast syntax requires calling typing.cast at runtime, but we don't
39+
# want to import typing at runtime. Here, we inform the type checkers that
40+
# we're importing `typing.cast` as `cast` and re-implement typing.cast's
41+
# runtime behavior in a block that is ignored by type checkers.
42+
if TYPE_CHECKING: # pragma: no cover
43+
# not executed at runtime
44+
from typing import cast
45+
else:
46+
# executed at runtime
47+
def cast(type_, value): # noqa
48+
return value

pkg_resources/_vendor/packaging/markers.py

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,14 @@
1313
from pkg_resources.extern.pyparsing import Literal as L # noqa
1414

1515
from ._compat import string_types
16+
from ._typing import TYPE_CHECKING
1617
from .specifiers import Specifier, InvalidSpecifier
1718

19+
if TYPE_CHECKING: # pragma: no cover
20+
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
21+
22+
Operator = Callable[[str, str], bool]
23+
1824

1925
__all__ = [
2026
"InvalidMarker",
@@ -46,30 +52,37 @@ class UndefinedEnvironmentName(ValueError):
4652

4753
class Node(object):
4854
def __init__(self, value):
55+
# type: (Any) -> None
4956
self.value = value
5057

5158
def __str__(self):
59+
# type: () -> str
5260
return str(self.value)
5361

5462
def __repr__(self):
63+
# type: () -> str
5564
return "<{0}({1!r})>".format(self.__class__.__name__, str(self))
5665

5766
def serialize(self):
67+
# type: () -> str
5868
raise NotImplementedError
5969

6070

6171
class Variable(Node):
6272
def serialize(self):
73+
# type: () -> str
6374
return str(self)
6475

6576

6677
class Value(Node):
6778
def serialize(self):
79+
# type: () -> str
6880
return '"{0}"'.format(self)
6981

7082

7183
class Op(Node):
7284
def serialize(self):
85+
# type: () -> str
7386
return str(self)
7487

7588

@@ -85,13 +98,13 @@ def serialize(self):
8598
| L("python_version")
8699
| L("sys_platform")
87100
| L("os_name")
88-
| L("os.name")
101+
| L("os.name") # PEP-345
89102
| L("sys.platform") # PEP-345
90103
| L("platform.version") # PEP-345
91104
| L("platform.machine") # PEP-345
92105
| L("platform.python_implementation") # PEP-345
93-
| L("python_implementation") # PEP-345
94-
| L("extra") # undocumented setuptools legacy
106+
| L("python_implementation") # undocumented setuptools legacy
107+
| L("extra") # PEP-508
95108
)
96109
ALIASES = {
97110
"os.name": "os_name",
@@ -131,13 +144,16 @@ def serialize(self):
131144

132145

133146
def _coerce_parse_result(results):
147+
# type: (Union[ParseResults, List[Any]]) -> List[Any]
134148
if isinstance(results, ParseResults):
135149
return [_coerce_parse_result(i) for i in results]
136150
else:
137151
return results
138152

139153

140154
def _format_marker(marker, first=True):
155+
# type: (Union[List[str], Tuple[Node, ...], str], Optional[bool]) -> str
156+
141157
assert isinstance(marker, (list, tuple, string_types))
142158

143159
# Sometimes we have a structure like [[...]] which is a single item list
@@ -172,18 +188,19 @@ def _format_marker(marker, first=True):
172188
"!=": operator.ne,
173189
">=": operator.ge,
174190
">": operator.gt,
175-
}
191+
} # type: Dict[str, Operator]
176192

177193

178194
def _eval_op(lhs, op, rhs):
195+
# type: (str, Op, str) -> bool
179196
try:
180197
spec = Specifier("".join([op.serialize(), rhs]))
181198
except InvalidSpecifier:
182199
pass
183200
else:
184201
return spec.contains(lhs)
185202

186-
oper = _operators.get(op.serialize())
203+
oper = _operators.get(op.serialize()) # type: Optional[Operator]
187204
if oper is None:
188205
raise UndefinedComparison(
189206
"Undefined {0!r} on {1!r} and {2!r}.".format(op, lhs, rhs)
@@ -192,13 +209,18 @@ def _eval_op(lhs, op, rhs):
192209
return oper(lhs, rhs)
193210

194211

195-
_undefined = object()
212+
class Undefined(object):
213+
pass
214+
215+
216+
_undefined = Undefined()
196217

197218

198219
def _get_env(environment, name):
199-
value = environment.get(name, _undefined)
220+
# type: (Dict[str, str], str) -> str
221+
value = environment.get(name, _undefined) # type: Union[str, Undefined]
200222

201-
if value is _undefined:
223+
if isinstance(value, Undefined):
202224
raise UndefinedEnvironmentName(
203225
"{0!r} does not exist in evaluation environment.".format(name)
204226
)
@@ -207,7 +229,8 @@ def _get_env(environment, name):
207229

208230

209231
def _evaluate_markers(markers, environment):
210-
groups = [[]]
232+
# type: (List[Any], Dict[str, str]) -> bool
233+
groups = [[]] # type: List[List[bool]]
211234

212235
for marker in markers:
213236
assert isinstance(marker, (list, tuple, string_types))
@@ -234,6 +257,7 @@ def _evaluate_markers(markers, environment):
234257

235258

236259
def format_full_version(info):
260+
# type: (sys._version_info) -> str
237261
version = "{0.major}.{0.minor}.{0.micro}".format(info)
238262
kind = info.releaselevel
239263
if kind != "final":
@@ -242,9 +266,13 @@ def format_full_version(info):
242266

243267

244268
def default_environment():
269+
# type: () -> Dict[str, str]
245270
if hasattr(sys, "implementation"):
246-
iver = format_full_version(sys.implementation.version)
247-
implementation_name = sys.implementation.name
271+
# Ignoring the `sys.implementation` reference for type checking due to
272+
# mypy not liking that the attribute doesn't exist in Python 2.7 when
273+
# run with the `--py27` flag.
274+
iver = format_full_version(sys.implementation.version) # type: ignore
275+
implementation_name = sys.implementation.name # type: ignore
248276
else:
249277
iver = "0"
250278
implementation_name = ""
@@ -266,6 +294,7 @@ def default_environment():
266294

267295
class Marker(object):
268296
def __init__(self, marker):
297+
# type: (str) -> None
269298
try:
270299
self._markers = _coerce_parse_result(MARKER.parseString(marker))
271300
except ParseException as e:
@@ -275,12 +304,15 @@ def __init__(self, marker):
275304
raise InvalidMarker(err_str)
276305

277306
def __str__(self):
307+
# type: () -> str
278308
return _format_marker(self._markers)
279309

280310
def __repr__(self):
311+
# type: () -> str
281312
return "<Marker({0!r})>".format(str(self))
282313

283314
def evaluate(self, environment=None):
315+
# type: (Optional[Dict[str, str]]) -> bool
284316
"""Evaluate a marker.
285317
286318
Return the boolean from evaluating the given marker against the

pkg_resources/_vendor/packaging/py.typed

Whitespace-only changes.

0 commit comments

Comments
 (0)