Skip to content

Commit 898d105

Browse files
authored
Clean up distutils_scheme types (#7300)
2 parents d3129f2 + d709966 commit 898d105

File tree

4 files changed

+29
-27
lines changed

4 files changed

+29
-27
lines changed

src/pip/_internal/locations.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@
1414
import sysconfig
1515
from distutils import sysconfig as distutils_sysconfig
1616
from distutils.command.install import SCHEME_KEYS # type: ignore
17+
from distutils.command.install import install as distutils_install_command
1718

1819
from pip._internal.utils import appdirs
1920
from pip._internal.utils.compat import WINDOWS
20-
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
21+
from pip._internal.utils.typing import MYPY_CHECK_RUNNING, cast
2122
from pip._internal.utils.virtualenv import running_under_virtualenv
2223

2324
if MYPY_CHECK_RUNNING:
24-
from typing import Any, Union, Dict, List, Optional
25+
from typing import Dict, List, Optional, Union
26+
27+
from distutils.cmd import Command as DistutilsCommand
2528

2629

2730
# Application Directories
@@ -88,29 +91,25 @@ def get_src_prefix():
8891
bin_py = '/usr/local/bin'
8992

9093

91-
def distutils_scheme(dist_name, user=False, home=None, root=None,
92-
isolated=False, prefix=None):
94+
def distutils_scheme(
95+
dist_name, user=False, home=None, root=None, isolated=False, prefix=None
96+
):
9397
# type:(str, bool, str, str, bool, str) -> dict
9498
"""
9599
Return a distutils install scheme
96100
"""
97101
from distutils.dist import Distribution
98102

99-
scheme = {}
100-
101-
if isolated:
102-
extra_dist_args = {"script_args": ["--no-user-cfg"]}
103-
else:
104-
extra_dist_args = {}
105103
dist_args = {'name': dist_name} # type: Dict[str, Union[str, List[str]]]
106-
dist_args.update(extra_dist_args)
104+
if isolated:
105+
dist_args["script_args"] = ["--no-user-cfg"]
107106

108107
d = Distribution(dist_args)
109-
# Ignoring, typeshed issue reported python/typeshed/issues/2567
110108
d.parse_config_files()
111-
# NOTE: Ignoring type since mypy can't find attributes on 'Command'
112-
i = d.get_command_obj('install', create=True) # type: Any
113-
assert i is not None
109+
obj = None # type: Optional[DistutilsCommand]
110+
obj = d.get_command_obj('install', create=True)
111+
assert obj is not None
112+
i = cast(distutils_install_command, obj)
114113
# NOTE: setting user or home has the side-effect of creating the home dir
115114
# or user base for installations during finalize_options()
116115
# ideally, we'd prefer a scheme class that has no side-effects.
@@ -123,6 +122,8 @@ def distutils_scheme(dist_name, user=False, home=None, root=None,
123122
i.home = home or i.home
124123
i.root = root or i.root
125124
i.finalize_options()
125+
126+
scheme = {}
126127
for key in SCHEME_KEYS:
127128
scheme[key] = getattr(i, 'install_' + key)
128129

@@ -131,9 +132,7 @@ def distutils_scheme(dist_name, user=False, home=None, root=None,
131132
# platlib). Note, i.install_lib is *always* set after
132133
# finalize_options(); we only want to override here if the user
133134
# has explicitly requested it hence going back to the config
134-
135-
# Ignoring, typeshed issue reported python/typeshed/issues/2567
136-
if 'install_lib' in d.get_option_dict('install'): # type: ignore
135+
if 'install_lib' in d.get_option_dict('install'):
137136
scheme.update(dict(purelib=i.install_lib, platlib=i.install_lib))
138137

139138
if running_under_virtualenv():

src/pip/_internal/utils/filesystem.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
from pip._vendor.six import PY2
1414

1515
from pip._internal.utils.compat import get_path_uid
16-
from pip._internal.utils.misc import cast
17-
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
16+
from pip._internal.utils.typing import MYPY_CHECK_RUNNING, cast
1817

1918
if MYPY_CHECK_RUNNING:
2019
from typing import BinaryIO, Iterator

src/pip/_internal/utils/misc.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
str_to_display,
4141
)
4242
from pip._internal.utils.marker_files import write_delete_marker_file
43-
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
43+
from pip._internal.utils.typing import MYPY_CHECK_RUNNING, cast
4444
from pip._internal.utils.virtualenv import (
4545
running_under_virtualenv,
4646
virtualenv_no_global,
@@ -54,16 +54,11 @@
5454
if MYPY_CHECK_RUNNING:
5555
from typing import (
5656
Any, AnyStr, Container, Iterable, List, Optional, Text,
57-
Tuple, Union, cast,
57+
Tuple, Union,
5858
)
5959
from pip._vendor.pkg_resources import Distribution
6060

6161
VersionInfo = Tuple[int, int, int]
62-
else:
63-
# typing's cast() is needed at runtime, but we don't want to import typing.
64-
# Thus, we use a dummy no-op version, which we tell mypy to ignore.
65-
def cast(type_, value): # type: ignore
66-
return value
6762

6863

6964
__all__ = ['rmtree', 'display_path', 'backup_dir',

src/pip/_internal/utils/typing.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,12 @@
2727
"""
2828

2929
MYPY_CHECK_RUNNING = False
30+
31+
32+
if MYPY_CHECK_RUNNING:
33+
from typing import cast
34+
else:
35+
# typing's cast() is needed at runtime, but we don't want to import typing.
36+
# Thus, we use a dummy no-op version, which we tell mypy to ignore.
37+
def cast(type_, value): # type: ignore
38+
return value

0 commit comments

Comments
 (0)