Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pip/_internal/utils/packaging.py: 63%
19 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:48 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:48 +0000
1import functools
2import logging
3import re
4from typing import NewType, Optional, Tuple, cast
6from pip._vendor.packaging import specifiers, version
7from pip._vendor.packaging.requirements import Requirement
9NormalizedExtra = NewType("NormalizedExtra", str)
11logger = logging.getLogger(__name__)
14def check_requires_python(
15 requires_python: Optional[str], version_info: Tuple[int, ...]
16) -> bool:
17 """
18 Check if the given Python version matches a "Requires-Python" specifier.
20 :param version_info: A 3-tuple of ints representing a Python
21 major-minor-micro version to check (e.g. `sys.version_info[:3]`).
23 :return: `True` if the given Python version satisfies the requirement.
24 Otherwise, return `False`.
26 :raises InvalidSpecifier: If `requires_python` has an invalid format.
27 """
28 if requires_python is None:
29 # The package provides no information
30 return True
31 requires_python_specifier = specifiers.SpecifierSet(requires_python)
33 python_version = version.parse(".".join(map(str, version_info)))
34 return python_version in requires_python_specifier
37@functools.lru_cache(maxsize=512)
38def get_requirement(req_string: str) -> Requirement:
39 """Construct a packaging.Requirement object with caching"""
40 # Parsing requirement strings is expensive, and is also expected to happen
41 # with a low diversity of different arguments (at least relative the number
42 # constructed). This method adds a cache to requirement object creation to
43 # minimize repeated parsing of the same string to construct equivalent
44 # Requirement objects.
45 return Requirement(req_string)
48def safe_extra(extra: str) -> NormalizedExtra:
49 """Convert an arbitrary string to a standard 'extra' name
51 Any runs of non-alphanumeric characters are replaced with a single '_',
52 and the result is always lowercased.
54 This function is duplicated from ``pkg_resources``. Note that this is not
55 the same to either ``canonicalize_name`` or ``_egg_link_name``.
56 """
57 return cast(NormalizedExtra, re.sub("[^A-Za-z0-9.-]+", "_", extra).lower())