Skip to content

Commit 2a5c23b

Browse files
committed
Simplify the handling of "typing.cast"
1 parent 80b82b5 commit 2a5c23b

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

src/pip/_internal/utils/misc.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,18 @@
5353
if MYPY_CHECK_RUNNING:
5454
from typing import (
5555
Any, AnyStr, Container, Iterable, List, Mapping, Match, Optional, Text,
56-
Union,
56+
Tuple, Union, cast,
5757
)
5858
from pip._vendor.pkg_resources import Distribution
5959
from pip._internal.models.link import Link
6060
from pip._internal.utils.ui import SpinnerInterface
6161

62-
try:
63-
from typing import cast, Tuple
6462
VersionInfo = Tuple[int, int, int]
65-
except ImportError:
66-
# typing's cast() isn't supported in code comments, so we need to
67-
# define a dummy, no-op version.
68-
def cast(typ, val): # type: ignore
69-
return val
70-
VersionInfo = None # type: ignore
63+
else:
64+
# typing's cast() is needed at runtime, but we don't want to import typing.
65+
# Thus, we use a dummy no-op version, which we tell mypy to ignore.
66+
def cast(type_, value): # type: ignore
67+
return value
7168

7269

7370
__all__ = ['rmtree', 'display_path', 'backup_dir',
@@ -140,7 +137,7 @@ def normalize_version_info(py_version_info):
140137
elif len(py_version_info) > 3:
141138
py_version_info = py_version_info[:3]
142139

143-
return cast(VersionInfo, py_version_info)
140+
return cast('VersionInfo', py_version_info)
144141

145142

146143
def ensure_dir(path):

0 commit comments

Comments
 (0)