Convert type comments to type annotations

This commit is contained in:
Ethan Smith
2022-06-06 03:38:50 -07:00
parent c97ffc1ed4
commit 4b996c0fa8
9 changed files with 187 additions and 226 deletions
+23 -24
View File
@@ -1,3 +1,5 @@
from __future__ import annotations
import json as simplejson
import logging
import os
@@ -1358,17 +1360,16 @@ def do_init(
def get_pip_args(
project,
pre=False, # type: bool
verbose=False, # type: bool
upgrade=False, # type: bool
require_hashes=False, # type: bool
no_build_isolation=False, # type: bool
no_use_pep517=False, # type: bool
no_deps=False, # type: bool
selective_upgrade=False, # type: bool
src_dir=None, # type: Optional[str]
):
# type: (...) -> List[str]
pre: bool = False,
verbose: bool = False,
upgrade: bool = False,
require_hashes: bool = False,
no_build_isolation: bool = False,
no_use_pep517: bool = False,
no_deps: bool = False,
selective_upgrade: bool = False,
src_dir: Optional[str] = None,
) -> List[str]:
from .vendor.packaging.version import parse as parse_version
arg_map = {
@@ -1399,12 +1400,11 @@ def get_pip_args(
def get_requirement_line(
requirement, # type: Requirement
src_dir=None, # type: Optional[str]
include_hashes=True, # type: bool
format_for_file=False, # type: bool
):
# type: (...) -> Union[List[str], str]
requirement: Requirement,
src_dir: Optional[str] = None,
include_hashes: bool = True,
format_for_file: bool = False,
) -> Union[List[str], str]:
line = None
if requirement.vcs or requirement.is_file_or_url:
if src_dir and requirement.line_instance.wheel_kwargs:
@@ -1426,13 +1426,12 @@ def get_requirement_line(
def write_requirement_to_file(
project, # type: Project
requirement, # type: Requirement
requirements_dir=None, # type: Optional[str]
src_dir=None, # type: Optional[str]
include_hashes=True, # type: bool
):
# type: (...) -> str
project: Project,
requirement: Requirement,
requirements_dir: Optional[str] = None,
src_dir: Optional[str] = None,
include_hashes: bool = True,
) -> str:
if not requirements_dir:
requirements_dir = vistir.path.create_tracked_tempdir(
prefix="pipenv", suffix="requirements"
+52 -68
View File
@@ -1,3 +1,5 @@
from __future__ import annotations
import contextlib
import importlib
import itertools
@@ -37,13 +39,13 @@ BASE_WORKING_SET = pkg_resources.WorkingSet(sys.path)
class Environment:
def __init__(
self,
prefix=None, # type: Optional[str]
python=None, # type: Optional[str]
is_venv=False, # type: bool
base_working_set=None, # type: pkg_resources.WorkingSet
pipfile=None, # type: Optional[Union[tomlkit.toml_document.TOMLDocument, TPipfile]]
sources=None, # type: Optional[List[TSource]]
project=None, # type: Optional[Project]
prefix: Optional[str] = None,
python: Optional[str] = None,
is_venv: bool = False,
base_working_set: pkg_resources.WorkingSet = None,
pipfile: Optional[Union[tomlkit.toml_document.TOMLDocument, TPipfile]] = None,
sources: Optional[List[TSource]] = None,
project: Optional[Project] = None,
):
super().__init__()
self._modules = {"pkg_resources": pkg_resources, "pipenv": pipenv}
@@ -70,8 +72,7 @@ class Environment:
self._base_paths = self.get_paths()
self.sys_paths = get_paths()
def safe_import(self, name):
# type: (str) -> ModuleType
def safe_import(self, name: str) -> ModuleType:
"""Helper utility for reimporting previously imported modules while inside the env"""
module = None
if name not in self._modules:
@@ -88,8 +89,9 @@ class Environment:
return module
@classmethod
def resolve_dist(cls, dist, working_set):
# type: (pkg_resources.Distribution, pkg_resources.WorkingSet) -> Set[pkg_resources.Distribution]
def resolve_dist(
cls, dist: pkg_resources.Distribution, working_set: pkg_resources.WorkingSet
) -> Set[pkg_resources.Distribution]:
"""Given a local distribution and a working set, returns all dependencies from the set.
:param dist: A single distribution to find the dependencies of
@@ -117,34 +119,29 @@ class Environment:
deps |= cls.resolve_dist(dist, working_set)
return deps
def extend_dists(self, dist):
# type: (pkg_resources.Distribution) -> None
def extend_dists(self, dist: pkg_resources.Distribution) -> None:
extras = self.resolve_dist(dist, self.base_working_set)
self.extra_dists.append(dist)
if extras:
self.extra_dists.extend(extras)
def add_dist(self, dist_name):
# type: (str) -> None
def add_dist(self, dist_name: str) -> None:
dist = pkg_resources.get_distribution(pkg_resources.Requirement(dist_name))
self.extend_dists(dist)
@cached_property
def python_version(self):
# type: () -> str
def python_version(self) -> str:
with self.activated():
sysconfig = self.safe_import("sysconfig")
py_version = sysconfig.get_python_version()
return py_version
def find_libdir(self):
# type: () -> Optional[Path]
def find_libdir(self) -> Optional[Path]:
libdir = self.prefix / "lib"
return next(iter(list(libdir.iterdir())), None)
@property
def python_info(self):
# type: () -> Dict[str, str]
def python_info(self) -> Dict[str, str]:
include_dir = self.prefix / "include"
if not os.path.exists(include_dir):
include_dirs = self.get_include_path()
@@ -162,8 +159,7 @@ class Environment:
return {"py_version_short": py_version_short, "abiflags": abiflags}
return {}
def _replace_parent_version(self, path, replace_version):
# type: (str, str) -> str
def _replace_parent_version(self, path: str, replace_version: str) -> str:
if not os.path.exists(path):
base, leaf = os.path.split(path)
base, parent = os.path.split(base)
@@ -184,8 +180,7 @@ class Environment:
return "posix_prefix"
@cached_property
def base_paths(self):
# type: () -> Dict[str, str]
def base_paths(self) -> Dict[str, str]:
"""
Returns the context appropriate paths for the environment.
@@ -269,8 +264,7 @@ class Environment:
return paths
@cached_property
def script_basedir(self):
# type: () -> str
def script_basedir(self) -> str:
"""Path to the environment scripts dir"""
prefix = make_posix(self.prefix.as_posix())
paths = get_paths(
@@ -283,8 +277,7 @@ class Environment:
return paths["scripts"]
@property
def python(self):
# type: () -> str
def python(self) -> str:
"""Path to the environment python"""
if self._python is not None:
return self._python
@@ -298,8 +291,7 @@ class Environment:
return py
@cached_property
def sys_path(self):
# type: () -> List[str]
def sys_path(self) -> List[str]:
"""
The system path inside the environment
@@ -329,9 +321,12 @@ class Environment:
return path
def build_command(
self, python_lib=False, python_inc=False, scripts=False, py_version=False
):
# type: (bool, bool, bool, bool) -> str
self,
python_lib: bool = False,
python_inc: bool = False,
scripts: bool = False,
py_version: bool = False,
) -> str:
"""Build the text for running a command in the given environment
:param python_lib: Whether to include the python lib dir commands, defaults to False
@@ -382,8 +377,7 @@ class Environment:
py_command = py_command % lines_as_str
return py_command
def get_paths(self):
# type: () -> Optional[Dict[str, str]]
def get_paths(self) -> Optional[Dict[str, str]]:
"""
Get the paths for the environment by running a subcommand
@@ -420,8 +414,7 @@ class Environment:
click.secho(f"Output: {c.stdout}", fg="yellow")
return None
def get_lib_paths(self):
# type: () -> Dict[str, str]
def get_lib_paths(self) -> Dict[str, str]:
"""Get the include path for the environment
:return: The python include path for the environment
@@ -477,8 +470,7 @@ class Environment:
return paths
return {}
def get_include_path(self):
# type: () -> Optional[Dict[str, str]]
def get_include_path(self) -> Optional[Dict[str, str]]:
"""Get the include path for the environment
:return: The python include path for the environment
@@ -510,8 +502,7 @@ class Environment:
return None
@cached_property
def sys_prefix(self):
# type: () -> str
def sys_prefix(self) -> str:
"""
The prefix run inside the context of the environment
@@ -525,8 +516,7 @@ class Environment:
return sys_prefix
@cached_property
def paths(self):
# type: () -> Dict[str, str]
def paths(self) -> Dict[str, str]:
paths = {}
with vistir.contextmanagers.temp_environ(), vistir.contextmanagers.temp_path():
os.environ["PYTHONIOENCODING"] = "utf-8"
@@ -539,21 +529,18 @@ class Environment:
return paths
@property
def scripts_dir(self):
# type: () -> str
def scripts_dir(self) -> str:
return self.paths["scripts"]
@property
def libdir(self):
# type: () -> str
def libdir(self) -> str:
purelib = self.paths.get("purelib", None)
if purelib and os.path.exists(purelib):
return "purelib", purelib
return "platlib", self.paths["platlib"]
@property
def pip_version(self):
# type: () -> Version
def pip_version(self) -> Version:
"""
Get the pip version in the environment. Useful for knowing which args we can use
when installing.
@@ -567,8 +554,7 @@ class Environment:
return parse_version(pip.version)
return parse_version("20.2")
def expand_egg_links(self):
# type: () -> None
def expand_egg_links(self) -> None:
"""
Expand paths specified in egg-link files to prevent pip errors during
reinstall
@@ -590,8 +576,7 @@ class Environment:
]
pth.write_text("\n".join(contents))
def get_distributions(self):
# type: () -> Generator[pkg_resources.Distribution, None, None]
def get_distributions(self) -> Generator[pkg_resources.Distribution, None, None]:
"""
Retrives the distributions installed on the library path of the environment
@@ -608,8 +593,7 @@ class Environment:
dists = (pkg_resources.find_distributions(libdir) for libdir in libdirs)
yield from itertools.chain.from_iterable(dists)
def find_egg(self, egg_dist):
# type: (pkg_resources.Distribution) -> str
def find_egg(self, egg_dist: pkg_resources.Distribution) -> str:
"""Find an egg by name in the given environment"""
site_packages = self.libdir[1]
search_filename = f"{egg_dist.project_name}.egg-link"
@@ -623,8 +607,7 @@ class Environment:
if os.path.isfile(egg):
return egg
def locate_dist(self, dist):
# type: (pkg_resources.Distribution) -> str
def locate_dist(self, dist: pkg_resources.Distribution) -> str:
"""Given a distribution, try to find a corresponding egg link first.
If the egg - link doesn 't exist, return the supplied distribution."""
@@ -632,8 +615,7 @@ class Environment:
location = self.find_egg(dist)
return location or dist.location
def dist_is_in_project(self, dist):
# type: (pkg_resources.Distribution) -> bool
def dist_is_in_project(self, dist: pkg_resources.Distribution) -> bool:
"""Determine whether the supplied distribution is in the environment."""
from .environments import normalize_pipfile_path as _normalized
@@ -648,8 +630,7 @@ class Environment:
location = _normalized(make_posix(location))
return any(location.startswith(prefix) for prefix in prefixes)
def get_installed_packages(self):
# type: () -> List[pkg_resources.Distribution]
def get_installed_packages(self) -> List[pkg_resources.Distribution]:
"""Returns all of the installed packages in a given environment"""
workingset = self.get_working_set()
packages = [
@@ -660,8 +641,9 @@ class Environment:
return packages
@contextlib.contextmanager
def get_finder(self, pre=False):
# type: (bool) -> ContextManager[pip_shims.shims.PackageFinder]
def get_finder(
self, pre: bool = False
) -> ContextManager[pip_shims.shims.PackageFinder]:
from .vendor.pip_shims.shims import InstallCommand, get_package_finder
pip_command = InstallCommand()
@@ -675,8 +657,9 @@ class Environment:
)
yield finder
def get_package_info(self, pre=False):
# type: (bool) -> Generator[pkg_resources.Distribution, None, None]
def get_package_info(
self, pre: bool = False
) -> Generator[pkg_resources.Distribution, None, None]:
from .vendor.pip_shims.shims import parse_version, pip_version
dependency_links = []
@@ -722,8 +705,9 @@ class Environment:
dist.latest_filetype = typ
yield dist
def get_outdated_packages(self, pre=False):
# type: (bool) -> List[pkg_resources.Distribution]
def get_outdated_packages(
self, pre: bool = False
) -> List[pkg_resources.Distribution]:
return [
pkg
for pkg in self.get_package_info(pre=pre)
+1 -2
View File
@@ -403,8 +403,7 @@ class Setting:
return self.PIPENV_VERBOSITY <= threshold
def is_using_venv():
# type: () -> bool
def is_using_venv() -> bool:
"""Check for venv-based virtual environment which sets sys.base_prefix"""
if getattr(sys, "real_prefix", None) is not None:
# virtualenv venvs
+42 -76
View File
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import annotations
import base64
import fnmatch
import hashlib
@@ -145,8 +147,7 @@ class Project:
except (TypeError, AttributeError):
pass
def path_to(self, p):
# type: (str) -> str
def path_to(self, p: str) -> str:
"""Returns the absolute path to a given relative path."""
if os.path.isabs(p):
return p
@@ -158,20 +159,17 @@ class Project:
return self.parsed_pipfile.get(package_section, {})
@property
def name(self):
# type: () -> str
def name(self) -> str:
if self._name is None:
self._name = self.pipfile_location.split(os.sep)[-2]
return self._name
@property
def pipfile_exists(self):
# type: () -> bool
def pipfile_exists(self) -> bool:
return os.path.isfile(self.pipfile_location)
@property
def required_python_version(self):
# type: () -> str
def required_python_version(self) -> str:
if self.pipfile_exists:
required = self.parsed_pipfile.get("requires", {}).get("python_full_version")
if not required:
@@ -180,17 +178,14 @@ class Project:
return required
@property
def project_directory(self):
# type: () -> str
def project_directory(self) -> str:
return os.path.abspath(os.path.join(self.pipfile_location, os.pardir))
@property
def requirements_exists(self):
# type: () -> bool
def requirements_exists(self) -> bool:
return bool(self.requirements_location)
def is_venv_in_project(self):
# type: () -> bool
def is_venv_in_project(self) -> bool:
if self.s.PIPENV_VENV_IN_PROJECT is False:
return False
return self.s.PIPENV_VENV_IN_PROJECT or (
@@ -199,8 +194,7 @@ class Project:
)
@property
def virtualenv_exists(self):
# type: () -> bool
def virtualenv_exists(self) -> bool:
if os.path.exists(self.virtualenv_location):
if os.name == "nt":
extra = ["Scripts", "activate.bat"]
@@ -210,8 +204,7 @@ class Project:
return False
def get_location_for_virtualenv(self):
# type: () -> str
def get_location_for_virtualenv(self) -> str:
# If there's no project yet, set location based on config.
if not self.project_directory:
if self.is_venv_in_project():
@@ -246,8 +239,7 @@ class Project:
return str(get_workon_home().joinpath(name))
@property
def working_set(self):
# type: () -> pkg_resources.WorkingSet
def working_set(self) -> pkg_resources.WorkingSet:
from pipenv.utils.shell import load_path
sys_path = load_path(self.which("python"))
@@ -260,13 +252,11 @@ class Project:
return self.environment.get_installed_packages()
@property
def installed_package_names(self):
# type: () -> List[str]
def installed_package_names(self) -> List[str]:
return get_canonical_names([pkg.key for pkg in self.installed_packages])
@property
def lockfile_package_names(self):
# type: () -> Dict[str, Set[str]]
def lockfile_package_names(self) -> Dict[str, Set[str]]:
dev_keys = get_canonical_names(self.lockfile_content["develop"].keys())
default_keys = get_canonical_names(self.lockfile_content["default"].keys())
return {
@@ -276,8 +266,7 @@ class Project:
}
@property
def pipfile_package_names(self):
# type: () -> Dict[str, Set[str]]
def pipfile_package_names(self) -> Dict[str, Set[str]]:
dev_keys = get_canonical_names(self.dev_packages.keys())
default_keys = get_canonical_names(self.packages.keys())
return {
@@ -286,8 +275,7 @@ class Project:
"combined": dev_keys | default_keys,
}
def get_environment(self, allow_global=False):
# type: (bool) -> Environment
def get_environment(self, allow_global: bool = False) -> Environment:
is_venv = is_in_virtualenv()
if allow_global and not is_venv:
prefix = sys.prefix
@@ -312,20 +300,17 @@ class Project:
return environment
@property
def environment(self):
# type: () -> Environment
def environment(self) -> Environment:
if not self._environment:
allow_global = self.s.PIPENV_USE_SYSTEM
self._environment = self.get_environment(allow_global=allow_global)
return self._environment
def get_outdated_packages(self):
# type: () -> List[pkg_resources.Distribution]
def get_outdated_packages(self) -> List[pkg_resources.Distribution]:
return self.environment.get_outdated_packages(pre=self.pipfile.get("pre", False))
@classmethod
def _sanitize(cls, name):
# type: (str) -> Tuple[str, str]
def _sanitize(cls, name: str) -> Tuple[str, str]:
# Replace dangerous characters into '_'. The length of the sanitized
# project name is limited as 42 because of the limit of linux kernel
#
@@ -340,8 +325,7 @@ class Project:
# https://github.com/torvalds/linux/blob/2bfe01ef/include/uapi/linux/binfmts.h#L18
return re.sub(r'[ &$`!*@"()\[\]\\\r\n\t]', "_", name)[0:42]
def _get_virtualenv_hash(self, name):
# type: (str) -> str
def _get_virtualenv_hash(self, name: str) -> str:
"""Get the name of the virtualenv adjusted for windows if needed
Returns (name, encoded_hash)
@@ -383,8 +367,7 @@ class Project:
return clean_name, encoded_hash
@property
def virtualenv_name(self):
# type: () -> str
def virtualenv_name(self) -> str:
sanitized, encoded_hash = self._get_virtualenv_hash(self.name)
suffix = ""
if self.s.PIPENV_PYTHON:
@@ -398,8 +381,7 @@ class Project:
return sanitized + "-" + encoded_hash + suffix
@property
def virtualenv_location(self):
# type: () -> str
def virtualenv_location(self) -> str:
# if VIRTUAL_ENV is set, use that.
virtualenv_env = os.getenv("VIRTUAL_ENV")
if (
@@ -415,8 +397,7 @@ class Project:
return self._virtualenv_location
@property
def virtualenv_src_location(self):
# type: () -> str
def virtualenv_src_location(self) -> str:
if self.virtualenv_location:
loc = os.sep.join([self.virtualenv_location, "src"])
else:
@@ -425,8 +406,7 @@ class Project:
return loc
@property
def download_location(self):
# type: () -> str
def download_location(self) -> str:
if self._download_location is None:
loc = os.sep.join([self.virtualenv_location, "downloads"])
self._download_location = loc
@@ -435,8 +415,7 @@ class Project:
return self._download_location
@property
def proper_names_db_path(self):
# type: () -> str
def proper_names_db_path(self) -> str:
if self._proper_names_db_path is None:
self._proper_names_db_path = Path(
self.virtualenv_location, "pipenv-proper-names.txt"
@@ -445,20 +424,17 @@ class Project:
return self._proper_names_db_path
@property
def proper_names(self):
# type: () -> str
def proper_names(self) -> str:
with self.proper_names_db_path.open() as f:
return f.read().splitlines()
def register_proper_name(self, name):
# type: (str) -> None
def register_proper_name(self, name: str) -> None:
"""Registers a proper name to the database."""
with self.proper_names_db_path.open("a") as f:
f.write("{0}\n".format(name))
@property
def pipfile_location(self):
# type: () -> str
def pipfile_location(self) -> str:
if self.s.PIPENV_PIPFILE:
return self.s.PIPENV_PIPFILE
@@ -471,8 +447,7 @@ class Project:
return self._pipfile_location
@property
def requirements_location(self):
# type: () -> Optional[str]
def requirements_location(self) -> Optional[str]:
if self._requirements_location is None:
try:
loc = find_requirements(max_depth=self.s.PIPENV_MAX_DEPTH)
@@ -482,8 +457,7 @@ class Project:
return self._requirements_location
@property
def parsed_pipfile(self):
# type: () -> Union[tomlkit.toml_document.TOMLDocument, TPipfile]
def parsed_pipfile(self) -> Union[tomlkit.toml_document.TOMLDocument, TPipfile]:
"""Parse Pipfile into a TOMLFile and cache it
(call clear_pipfile_cache() afterwards if mutating)"""
@@ -495,8 +469,7 @@ class Project:
_pipfile_cache[cache_key] = parsed
return _pipfile_cache[cache_key]
def read_pipfile(self):
# type: () -> str
def read_pipfile(self) -> str:
# Open the pipfile, read it into memory.
if not self.pipfile_exists:
return ""
@@ -506,13 +479,13 @@ class Project:
return contents
def clear_pipfile_cache(self):
# type: () -> None
def clear_pipfile_cache(self) -> None:
"""Clear pipfile cache (e.g., so we can mutate parsed pipfile)"""
_pipfile_cache.clear()
def _parse_pipfile(self, contents):
# type: (str) -> Union[tomlkit.toml_document.TOMLDocument, TPipfile]
def _parse_pipfile(
self, contents: str
) -> Union[tomlkit.toml_document.TOMLDocument, TPipfile]:
try:
return tomlkit.parse(contents)
except Exception:
@@ -520,8 +493,7 @@ class Project:
# Fallback to toml parser, for large files.
return toml.loads(contents)
def _read_pyproject(self):
# type: () -> None
def _read_pyproject(self) -> None:
pyproject = self.path_to("pyproject.toml")
if os.path.exists(pyproject):
self._pyproject = toml.load(pyproject)
@@ -535,30 +507,25 @@ class Project:
self._build_system = build_system
@property
def build_requires(self):
# type: () -> List[str]
def build_requires(self) -> List[str]:
return self._build_system.get("requires", ["setuptools>=40.8.0", "wheel"])
@property
def build_backend(self):
# type: () -> str
def build_backend(self) -> str:
return self._build_system.get("build-backend", get_default_pyproject_backend())
@property
def settings(self):
# type: () -> Union[tomlkit.items.Table, Dict[str, Union[str, bool]]]
def settings(self) -> Union[tomlkit.items.Table, Dict[str, Union[str, bool]]]:
"""A dictionary of the settings added to the Pipfile."""
return self.parsed_pipfile.get("pipenv", {})
def has_script(self, name):
# type: (str) -> bool
def has_script(self, name: str) -> bool:
try:
return name in self.parsed_pipfile["scripts"]
except KeyError:
return False
def build_script(self, name, extra_args=None):
# type: (str, Optional[List[str]]) -> Script
def build_script(self, name: str, extra_args: Optional[List[str]] = None) -> Script:
try:
script = Script.parse(self.parsed_pipfile["scripts"][name])
except KeyError:
@@ -567,8 +534,7 @@ class Project:
script.extend(extra_args)
return script
def update_settings(self, d):
# type: (Dict[str, Union[str, bool]]) -> None
def update_settings(self, d: Dict[str, Union[str, bool]]) -> None:
settings = self.settings
changed = False
for new in d:
+2 -4
View File
@@ -415,13 +415,11 @@ class Entry:
return self.strip_version(version)
@property
def updated_specifier(self):
# type: () -> str
def updated_specifier(self) -> str:
return self.entry.specifiers
@property
def original_specifier(self):
# type: () -> str
def original_specifier(self) -> str:
return self.lockfile_entry.specifiers
@property
+14 -10
View File
@@ -1,3 +1,5 @@
from __future__ import annotations
import re
from collections.abc import Mapping
@@ -43,8 +45,11 @@ def prepare_pip_source_args(sources, pip_args=None):
return pip_args
def get_project_index(project, index=None, trusted_hosts=None):
# type: (Optional[Union[str, TSource]], Optional[List[str]], Optional[Project]) -> TSource
def get_project_index(
project: Optional[Union[str, TSource]],
index: Optional[List[str]] = None,
trusted_hosts: Optional[Project] = None,
) -> TSource:
from pipenv.project import SourceNotFound
if trusted_hosts is None:
@@ -62,14 +67,13 @@ def get_project_index(project, index=None, trusted_hosts=None):
def get_source_list(
project, # type: Project
index=None, # type: Optional[Union[str, TSource]]
extra_indexes=None, # type: Optional[List[str]]
trusted_hosts=None, # type: Optional[List[str]]
pypi_mirror=None, # type: Optional[str]
):
# type: (...) -> List[TSource]
sources = [] # type: List[TSource]
project: Project,
index: Optional[Union[str, TSource]] = None,
extra_indexes: Optional[List[str]] = None,
trusted_hosts: Optional[List[str]] = None,
pypi_mirror: Optional[str] = None,
) -> List[TSource]:
sources: List[TSource] = []
if index:
sources.append(get_project_index(project, index))
if extra_indexes:
+1 -2
View File
@@ -85,8 +85,7 @@ def get_url_name(url):
return urllib3_util.parse_url(url).host
def is_url_equal(url, other_url):
# type: (str, str) -> bool
def is_url_equal(url: str, other_url: str) -> bool:
"""
Compare two urls by scheme, host, and path, ignoring auth
+51 -38
View File
@@ -1,3 +1,5 @@
from __future__ import annotations
import contextlib
import hashlib
import os
@@ -139,18 +141,22 @@ class Resolver:
@classmethod
def get_metadata(
cls,
deps, # type: List[str]
index_lookup, # type: Dict[str, str]
markers_lookup, # type: Dict[str, str]
project, # type: Project
sources, # type: Dict[str, str]
req_dir=None, # type: Optional[str]
pre=False, # type: bool
clear=False, # type: bool
):
# type: (...) -> Tuple[Set[str], Dict[str, Dict[str, Union[str, bool, List[str]]]], Dict[str, str], Dict[str, str]]
constraints = set() # type: Set[str]
skipped = {} # type: Dict[str, Dict[str, Union[str, bool, List[str]]]]
deps: List[str],
index_lookup: Dict[str, str],
markers_lookup: Dict[str, str],
project: Project,
sources: Dict[str, str],
req_dir: Optional[str] = None,
pre: bool = False,
clear: bool = False,
) -> Tuple[
Set[str],
Dict[str, Dict[str, Union[str, bool, List[str]]]],
Dict[str, str],
Dict[str, str],
]:
constraints: Set[str] = set()
skipped: Dict[str, Dict[str, Union[str, bool, List[str]]]] = {}
if index_lookup is None:
index_lookup = {}
if markers_lookup is None:
@@ -209,12 +215,11 @@ class Resolver:
@classmethod
def parse_line(
cls,
line, # type: str
index_lookup=None, # type: Dict[str, str]
markers_lookup=None, # type: Dict[str, str]
project=None, # type: Optional[Project]
):
# type: (...) -> Tuple[Requirement, Dict[str, str], Dict[str, str]]
line: str,
index_lookup: Dict[str, str] = None,
markers_lookup: Dict[str, str] = None,
project: Optional[Project] = None,
) -> Tuple[Requirement, Dict[str, str], Dict[str, str]]:
if index_lookup is None:
index_lookup = {}
@@ -226,7 +231,7 @@ class Resolver:
project = Project()
index, extra_index, trust_host, remainder = parse_indexes(line)
line = " ".join(remainder)
req = None # type: Requirement
req: Requirement = None
try:
req = Requirement.from_line(line)
except ValueError:
@@ -262,8 +267,12 @@ class Resolver:
return req, index_lookup, markers_lookup
@classmethod
def get_deps_from_req(cls, req, resolver=None, resolve_vcs=True):
# type: (Requirement, Optional["Resolver"], bool) -> Tuple[Set[str], Dict[str, Dict[str, Union[str, bool, List[str]]]]]
def get_deps_from_req(
cls,
req: Requirement,
resolver: Optional["Resolver"] = None,
resolve_vcs: bool = True,
) -> Tuple[Set[str], Dict[str, Dict[str, Union[str, bool, List[str]]]]]:
from pipenv.vendor.requirementslib.models.requirements import Requirement
from pipenv.vendor.requirementslib.models.utils import (
_requirement_to_str_lowercase_name,
@@ -271,8 +280,8 @@ class Resolver:
from pipenv.vendor.requirementslib.utils import is_installable_dir
# TODO: this is way too complex, refactor this
constraints = set() # type: Set[str]
locked_deps = {} # type: Dict[str, Dict[str, Union[str, bool, List[str]]]]
constraints: Set[str] = set()
locked_deps: Dict[str, Dict[str, Union[str, bool, List[str]]]] = {}
if (req.is_file_or_url or req.is_vcs) and not req.is_wheel:
# for local packages with setup.py files and potential direct url deps:
if req.is_vcs:
@@ -281,7 +290,7 @@ class Resolver:
entry = lockfile[pep423_name(req.normalized_name)]
else:
_, entry = req.pipfile_entry
parsed_line = req.req.parsed_line # type: Line
parsed_line: Line = req.req.parsed_line
try:
name = req.normalized_name
except TypeError:
@@ -377,16 +386,15 @@ class Resolver:
@classmethod
def create(
cls,
deps, # type: List[str]
project, # type: Project
index_lookup=None, # type: Dict[str, str]
markers_lookup=None, # type: Dict[str, str]
sources=None, # type: List[str]
req_dir=None, # type: str
clear=False, # type: bool
pre=False, # type: bool
):
# type: (...) -> "Resolver"
deps: List[str],
project: Project,
index_lookup: Dict[str, str] = None,
markers_lookup: Dict[str, str] = None,
sources: List[str] = None,
req_dir: str = None,
clear: bool = False,
pre: bool = False,
) -> "Resolver":
if not req_dir:
req_dir = create_tracked_tempdir(suffix="-requirements", prefix="pipenv-")
@@ -419,8 +427,14 @@ class Resolver:
)
@classmethod
def from_pipfile(cls, project, pipfile=None, dev=False, pre=False, clear=False):
# type: (Optional[Project], Optional[Pipfile], bool, bool, bool) -> "Resolver"
def from_pipfile(
cls,
project: Optional[Project],
pipfile: Optional[Pipfile] = None,
dev: bool = False,
pre: bool = False,
clear: bool = False,
) -> "Resolver":
if not pipfile:
pipfile = project._pipfile
@@ -1109,8 +1123,7 @@ def resolve_deps(
@lru_cache()
def get_pipenv_sitedir():
# type: () -> Optional[str]
def get_pipenv_sitedir() -> Optional[str]:
import pkg_resources
site_dir = next(
+1 -2
View File
@@ -21,8 +21,7 @@ if environments.MYPY_RUNNING:
@lru_cache()
def make_posix(path):
# type: (str) -> str
def make_posix(path: str) -> str:
"""
Convert a path with possible windows-style separators to a posix-style path
(with **/** separators instead of **\\** separators).