1
1
Fork 0
mirror of https://github.com/pypa/pip synced 2023-12-13 21:30:23 +01:00

Complete type annotations in pip/_internal/locations (#10127)

This commit is contained in:
Harutaka Kawamura 2021-07-12 12:42:16 +09:00 committed by GitHub
parent 72e1ff35c7
commit 23eb69fe21
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 51 additions and 67 deletions

1
news/10127.trivial.rst Normal file
View file

@ -0,0 +1 @@
Converted type commentaries into annotations in ``pip/_internal/locations``.

View file

@ -69,14 +69,13 @@ def _log_context(
def get_scheme(
dist_name, # type: str
user=False, # type: bool
home=None, # type: Optional[str]
root=None, # type: Optional[str]
isolated=False, # type: bool
prefix=None, # type: Optional[str]
):
# type: (...) -> Scheme
dist_name: str,
user: bool = False,
home: Optional[str] = None,
root: Optional[str] = None,
isolated: bool = False,
prefix: Optional[str] = None,
) -> Scheme:
old = _distutils.get_scheme(
dist_name,
user=user,
@ -124,8 +123,7 @@ def get_scheme(
return old
def get_bin_prefix():
# type: () -> str
def get_bin_prefix() -> str:
old = _distutils.get_bin_prefix()
new = _sysconfig.get_bin_prefix()
if _warn_if_mismatch(pathlib.Path(old), pathlib.Path(new), key="bin_prefix"):
@ -133,13 +131,11 @@ def get_bin_prefix():
return old
def get_bin_user():
# type: () -> str
def get_bin_user() -> str:
return _sysconfig.get_scheme("", user=True).scripts
def get_purelib():
# type: () -> str
def get_purelib() -> str:
"""Return the default pure-Python lib location."""
old = _distutils.get_purelib()
new = _sysconfig.get_purelib()
@ -148,8 +144,7 @@ def get_purelib():
return old
def get_platlib():
# type: () -> str
def get_platlib() -> str:
"""Return the default platform-shared lib location."""
old = _distutils.get_platlib()
new = _sysconfig.get_platlib()
@ -158,8 +153,7 @@ def get_platlib():
return old
def get_prefixed_libs(prefix):
# type: (str) -> List[str]
def get_prefixed_libs(prefix: str) -> List[str]:
"""Return the lib locations under ``prefix``."""
old_pure, old_plat = _distutils.get_prefixed_libs(prefix)
new_pure, new_plat = _sysconfig.get_prefixed_libs(prefix)

View file

@ -19,21 +19,25 @@ from .base import get_major_minor_version
def _distutils_scheme(
dist_name, user=False, home=None, root=None, isolated=False, prefix=None
):
# type:(str, bool, str, str, bool, str) -> Dict[str, str]
dist_name: str,
user: bool = False,
home: str = None,
root: str = None,
isolated: bool = False,
prefix: str = None,
) -> Dict[str, str]:
"""
Return a distutils install scheme
"""
from distutils.dist import Distribution
dist_args = {"name": dist_name} # type: Dict[str, Union[str, List[str]]]
dist_args: Dict[str, Union[str, List[str]]] = {"name": dist_name}
if isolated:
dist_args["script_args"] = ["--no-user-cfg"]
d = Distribution(dist_args)
d.parse_config_files()
obj = None # type: Optional[DistutilsCommand]
obj: Optional[DistutilsCommand] = None
obj = d.get_command_obj("install", create=True)
assert obj is not None
i = cast(distutils_install_command, obj)
@ -82,14 +86,13 @@ def _distutils_scheme(
def get_scheme(
dist_name, # type: str
user=False, # type: bool
home=None, # type: Optional[str]
root=None, # type: Optional[str]
isolated=False, # type: bool
prefix=None, # type: Optional[str]
):
# type: (...) -> Scheme
dist_name: str,
user: bool = False,
home: Optional[str] = None,
root: Optional[str] = None,
isolated: bool = False,
prefix: Optional[str] = None,
) -> Scheme:
"""
Get the "scheme" corresponding to the input parameters. The distutils
documentation provides the context for the available schemes:
@ -117,8 +120,7 @@ def get_scheme(
)
def get_bin_prefix():
# type: () -> str
def get_bin_prefix() -> str:
if WINDOWS:
bin_py = os.path.join(sys.prefix, "Scripts")
# buildout uses 'bin' on Windows too?
@ -132,18 +134,15 @@ def get_bin_prefix():
return os.path.join(sys.prefix, "bin")
def get_purelib():
# type: () -> str
def get_purelib() -> str:
return get_python_lib(plat_specific=False)
def get_platlib():
# type: () -> str
def get_platlib() -> str:
return get_python_lib(plat_specific=True)
def get_prefixed_libs(prefix):
# type: (str) -> Tuple[str, str]
def get_prefixed_libs(prefix: str) -> Tuple[str, str]:
return (
get_python_lib(plat_specific=False, prefix=prefix),
get_python_lib(plat_specific=True, prefix=prefix),

View file

@ -25,8 +25,7 @@ logger = logging.getLogger(__name__)
_AVAILABLE_SCHEMES = set(sysconfig.get_scheme_names())
def _infer_prefix():
# type: () -> str
def _infer_prefix() -> str:
"""Try to find a prefix scheme for the current platform.
This tries:
@ -51,8 +50,7 @@ def _infer_prefix():
return "posix_prefix"
def _infer_user():
# type: () -> str
def _infer_user() -> str:
"""Try to find a user scheme for the current platform."""
suffixed = f"{os.name}_user"
if suffixed in _AVAILABLE_SCHEMES:
@ -62,8 +60,7 @@ def _infer_user():
return "posix_user"
def _infer_home():
# type: () -> str
def _infer_home() -> str:
"""Try to find a home for the current platform."""
suffixed = f"{os.name}_home"
if suffixed in _AVAILABLE_SCHEMES:
@ -85,14 +82,13 @@ if sysconfig.get_config_var("userbase") is not None:
def get_scheme(
dist_name, # type: str
user=False, # type: bool
home=None, # type: typing.Optional[str]
root=None, # type: typing.Optional[str]
isolated=False, # type: bool
prefix=None, # type: typing.Optional[str]
):
# type: (...) -> Scheme
dist_name: str,
user: bool = False,
home: typing.Optional[str] = None,
root: typing.Optional[str] = None,
isolated: bool = False,
prefix: typing.Optional[str] = None,
) -> Scheme:
"""
Get the "scheme" corresponding to the input parameters.
@ -156,25 +152,21 @@ def get_scheme(
return scheme
def get_bin_prefix():
# type: () -> str
def get_bin_prefix() -> str:
# Forcing to use /usr/local/bin for standard macOS framework installs.
if sys.platform[:6] == "darwin" and sys.prefix[:16] == "/System/Library/":
return "/usr/local/bin"
return sysconfig.get_paths()["scripts"]
def get_purelib():
# type: () -> str
def get_purelib() -> str:
return sysconfig.get_paths()["purelib"]
def get_platlib():
# type: () -> str
def get_platlib() -> str:
return sysconfig.get_paths()["platlib"]
def get_prefixed_libs(prefix):
# type: (str) -> typing.Tuple[str, str]
def get_prefixed_libs(prefix: str) -> typing.Tuple[str, str]:
paths = sysconfig.get_paths(vars={"base": prefix, "platbase": prefix})
return (paths["purelib"], paths["platlib"])

View file

@ -11,11 +11,10 @@ from pip._internal.utils.virtualenv import running_under_virtualenv
USER_CACHE_DIR = appdirs.user_cache_dir("pip")
# FIXME doesn't account for venv linked to global site-packages
site_packages = sysconfig.get_path("purelib") # type: typing.Optional[str]
site_packages: typing.Optional[str] = sysconfig.get_path("purelib")
def get_major_minor_version():
# type: () -> str
def get_major_minor_version() -> str:
"""
Return the major-minor version of the current Python as a string, e.g.
"3.7" or "3.10".
@ -23,8 +22,7 @@ def get_major_minor_version():
return "{}.{}".format(*sys.version_info)
def get_src_prefix():
# type: () -> str
def get_src_prefix() -> str:
if running_under_virtualenv():
src_prefix = os.path.join(sys.prefix, "src")
else:
@ -43,6 +41,6 @@ def get_src_prefix():
try:
# Use getusersitepackages if this is present, as it ensures that the
# value is initialised properly.
user_site = site.getusersitepackages() # type: typing.Optional[str]
user_site: typing.Optional[str] = site.getusersitepackages()
except AttributeError:
user_site = site.USER_SITE