Let us walk on the 3-isogeny graph
Loading...
Searching...
No Matches
packaging.py
Go to the documentation of this file.
1import functools
2import logging
3import re
4from typing import NewType, Optional, Tuple, cast
5
6from pip._vendor.packaging import specifiers, version
7from pip._vendor.packaging.requirements import Requirement
8
9NormalizedExtra = NewType("NormalizedExtra", str)
10
11logger = logging.getLogger(__name__)
12
13
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.
19
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]`).
22
23 :return: `True` if the given Python version satisfies the requirement.
24 Otherwise, return `False`.
25
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)
32
33 python_version = version.parse(".".join(map(str, version_info)))
34 return python_version in requires_python_specifier
35
36
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)
46
47
48def safe_extra(extra: str) -> NormalizedExtra:
49 """Convert an arbitrary string to a standard 'extra' name
50
51 Any runs of non-alphanumeric characters are replaced with a single '_',
52 and the result is always lowercased.
53
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())
for i