Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type annotations for pip._internal.models #6065

Merged
merged 1 commit into from
Dec 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/pip/_internal/models/candidate.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
from pip._vendor.packaging.version import parse as parse_version

from pip._internal.utils.models import KeyBasedCompareMixin
from pip._internal.utils.typing import MYPY_CHECK_RUNNING

if MYPY_CHECK_RUNNING:
from pip._vendor.packaging.version import _BaseVersion # noqa: F401
from pip._internal.models.link import Link # noqa: F401
from typing import Any, Union # noqa: F401


class InstallationCandidate(KeyBasedCompareMixin):
"""Represents a potential "candidate" for installation.
"""

def __init__(self, project, version, location):
# type: (Any, str, Link) -> None
self.project = project
self.version = parse_version(version)
self.version = parse_version(version) # type: _BaseVersion
self.location = location

super(InstallationCandidate, self).__init__(
Expand All @@ -18,6 +25,7 @@ def __init__(self, project, version, location):
)

def __repr__(self):
# type: () -> str
return "<InstallationCandidate({!r}, {!r}, {!r})>".format(
self.project, self.version, self.location,
)
2 changes: 2 additions & 0 deletions src/pip/_internal/models/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class PackageIndex(object):
"""

def __init__(self, url, file_storage_domain):
# type: (str, str) -> None
super(PackageIndex, self).__init__()
self.url = url
self.netloc = urllib_parse.urlsplit(url).netloc
Expand All @@ -18,6 +19,7 @@ def __init__(self, url, file_storage_domain):
self.file_storage_domain = file_storage_domain

def _url_for_path(self, path):
# type: (str) -> str
return urllib_parse.urljoin(self.url, path)


Expand Down
20 changes: 20 additions & 0 deletions src/pip/_internal/models/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@
WHEEL_EXTENSION, redact_password_from_url, splitext,
)
from pip._internal.utils.models import KeyBasedCompareMixin
from pip._internal.utils.typing import MYPY_CHECK_RUNNING

if MYPY_CHECK_RUNNING:
from typing import Optional, Tuple, Union, Text # noqa: F401
from pip._internal.index import HTMLPage # noqa: F401


class Link(KeyBasedCompareMixin):
"""Represents a parsed link from a Package Index's simple URL
"""

def __init__(self, url, comes_from=None, requires_python=None):
# type: (str, Optional[Union[str, HTMLPage]], Optional[str]) -> None
"""
url:
url of the resource pointed to (href of the link)
Expand Down Expand Up @@ -55,6 +61,7 @@ def __repr__(self):

@property
def filename(self):
# type: () -> str
_, netloc, path, _, _ = urllib_parse.urlsplit(self.url)
name = posixpath.basename(path.rstrip('/')) or netloc
name = urllib_parse.unquote(name)
Expand All @@ -63,32 +70,39 @@ def filename(self):

@property
def scheme(self):
# type: () -> str
return urllib_parse.urlsplit(self.url)[0]

@property
def netloc(self):
# type: () -> str
return urllib_parse.urlsplit(self.url)[1]

@property
def path(self):
# type: () -> str
return urllib_parse.unquote(urllib_parse.urlsplit(self.url)[2])

def splitext(self):
# type: () -> Tuple[str, str]
return splitext(posixpath.basename(self.path.rstrip('/')))

@property
def ext(self):
# type: () -> str
return self.splitext()[1]

@property
def url_without_fragment(self):
# type: () -> str
scheme, netloc, path, query, fragment = urllib_parse.urlsplit(self.url)
return urllib_parse.urlunsplit((scheme, netloc, path, query, None))

_egg_fragment_re = re.compile(r'[#&]egg=([^&]*)')

@property
def egg_fragment(self):
# type: () -> Optional[str]
match = self._egg_fragment_re.search(self.url)
if not match:
return None
Expand All @@ -98,6 +112,7 @@ def egg_fragment(self):

@property
def subdirectory_fragment(self):
# type: () -> Optional[str]
match = self._subdirectory_fragment_re.search(self.url)
if not match:
return None
Expand All @@ -109,28 +124,33 @@ def subdirectory_fragment(self):

@property
def hash(self):
# type: () -> Optional[str]
match = self._hash_re.search(self.url)
if match:
return match.group(2)
return None

@property
def hash_name(self):
# type: () -> Optional[str]
match = self._hash_re.search(self.url)
if match:
return match.group(1)
return None

@property
def show_url(self):
# type: () -> Optional[str]
return posixpath.basename(self.url.split('#', 1)[0].split('?', 1)[0])

@property
def is_wheel(self):
# type: () -> bool
return self.ext == WHEEL_EXTENSION

@property
def is_artifact(self):
# type: () -> bool
"""
Determines if this points to an actual artifact (e.g. a tarball) or if
it points to an "abstract" thing like a path or a VCS location.
Expand Down