Skip to content

POC: Type-safe name canonicalization #7984

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

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 2 additions & 1 deletion src/pip/_internal/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from pip._vendor.packaging.tags import Tag

from pip._internal.models.format_control import FormatControl
from pip._internal.utils.packaging import CanonicalName

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -122,7 +123,7 @@ def _get_cache_path_parts(self, link):
return parts

def _get_candidates(self, link, canonical_package_name):
# type: (Link, Optional[str]) -> List[Any]
# type: (Link, Optional[CanonicalName]) -> List[Any]
can_not_cache = (
not self.cache_dir or
not canonical_package_name or
Expand Down
14 changes: 8 additions & 6 deletions src/pip/_internal/models/format_control.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
# The following comment should be removed at some point in the future.
# mypy: strict-optional=False

from pip._vendor.packaging.utils import canonicalize_name

from pip._internal.exceptions import CommandError
from pip._internal.utils.packaging import canonicalize_name
from pip._internal.utils.typing import MYPY_CHECK_RUNNING

if MYPY_CHECK_RUNNING:
from typing import Optional, Set, FrozenSet
from typing import FrozenSet, Literal, Optional, Set, Union
from pip._internal.utils.packaging import CanonicalName

Value = Union[CanonicalName, Literal[":all:"]]


class FormatControl(object):
"""Helper for managing formats from which a package can be installed.
"""

def __init__(self, no_binary=None, only_binary=None):
# type: (Optional[Set[str]], Optional[Set[str]]) -> None
# type: (Optional[Set[Value]], Optional[Set[Value]]) -> None
if no_binary is None:
no_binary = set()
if only_binary is None:
Expand All @@ -42,7 +44,7 @@ def __repr__(self):

@staticmethod
def handle_mutual_excludes(value, target, other):
# type: (str, Optional[Set[str]], Optional[Set[str]]) -> None
# type: (str, Optional[Set[Value]], Optional[Set[Value]]) -> None
if value.startswith('-'):
raise CommandError(
"--no-binary / --only-binary option requires 1 argument."
Expand All @@ -65,7 +67,7 @@ def handle_mutual_excludes(value, target, other):
target.add(name)

def get_allowed_formats(self, canonical_name):
# type: (str) -> FrozenSet[str]
# type: (CanonicalName) -> FrozenSet[str]
result = {"binary", "source"}
if canonical_name in self.only_binary:
result.discard('source')
Expand Down
14 changes: 12 additions & 2 deletions src/pip/_internal/utils/packaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@
from email.parser import FeedParser

from pip._vendor import pkg_resources
from pip._vendor.packaging import specifiers, version
from pip._vendor.packaging import specifiers, utils, version

from pip._internal.exceptions import NoneMetadataError
from pip._internal.utils.misc import display_path
from pip._internal.utils.typing import MYPY_CHECK_RUNNING

if MYPY_CHECK_RUNNING:
from typing import Optional, Tuple
from typing import NewType, Optional, Tuple
from email.message import Message
from pip._vendor.pkg_resources import Distribution

CanonicalName = NewType("CanonicalName", str)


logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -92,3 +94,11 @@ def get_installer(dist):
if line.strip():
return line.strip()
return ''


if MYPY_CHECK_RUNNING:
def canonicalize_name(s):
# type: (str) -> CanonicalName
return CanonicalName(utils.canonicalize_name(s))
else:
canonicalize_name = utils.canonicalize_name