Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pip/_internal/models/target_python.py: 28%

39 statements  

« prev     ^ index     » next       coverage.py v7.4.3, created at 2024-02-26 06:33 +0000

1import sys 

2from typing import List, Optional, Set, Tuple 

3 

4from pip._vendor.packaging.tags import Tag 

5 

6from pip._internal.utils.compatibility_tags import get_supported, version_info_to_nodot 

7from pip._internal.utils.misc import normalize_version_info 

8 

9 

10class TargetPython: 

11 

12 """ 

13 Encapsulates the properties of a Python interpreter one is targeting 

14 for a package install, download, etc. 

15 """ 

16 

17 __slots__ = [ 

18 "_given_py_version_info", 

19 "abis", 

20 "implementation", 

21 "platforms", 

22 "py_version", 

23 "py_version_info", 

24 "_valid_tags", 

25 "_valid_tags_set", 

26 ] 

27 

28 def __init__( 

29 self, 

30 platforms: Optional[List[str]] = None, 

31 py_version_info: Optional[Tuple[int, ...]] = None, 

32 abis: Optional[List[str]] = None, 

33 implementation: Optional[str] = None, 

34 ) -> None: 

35 """ 

36 :param platforms: A list of strings or None. If None, searches for 

37 packages that are supported by the current system. Otherwise, will 

38 find packages that can be built on the platforms passed in. These 

39 packages will only be downloaded for distribution: they will 

40 not be built locally. 

41 :param py_version_info: An optional tuple of ints representing the 

42 Python version information to use (e.g. `sys.version_info[:3]`). 

43 This can have length 1, 2, or 3 when provided. 

44 :param abis: A list of strings or None. This is passed to 

45 compatibility_tags.py's get_supported() function as is. 

46 :param implementation: A string or None. This is passed to 

47 compatibility_tags.py's get_supported() function as is. 

48 """ 

49 # Store the given py_version_info for when we call get_supported(). 

50 self._given_py_version_info = py_version_info 

51 

52 if py_version_info is None: 

53 py_version_info = sys.version_info[:3] 

54 else: 

55 py_version_info = normalize_version_info(py_version_info) 

56 

57 py_version = ".".join(map(str, py_version_info[:2])) 

58 

59 self.abis = abis 

60 self.implementation = implementation 

61 self.platforms = platforms 

62 self.py_version = py_version 

63 self.py_version_info = py_version_info 

64 

65 # This is used to cache the return value of get_(un)sorted_tags. 

66 self._valid_tags: Optional[List[Tag]] = None 

67 self._valid_tags_set: Optional[Set[Tag]] = None 

68 

69 def format_given(self) -> str: 

70 """ 

71 Format the given, non-None attributes for display. 

72 """ 

73 display_version = None 

74 if self._given_py_version_info is not None: 

75 display_version = ".".join( 

76 str(part) for part in self._given_py_version_info 

77 ) 

78 

79 key_values = [ 

80 ("platforms", self.platforms), 

81 ("version_info", display_version), 

82 ("abis", self.abis), 

83 ("implementation", self.implementation), 

84 ] 

85 return " ".join( 

86 f"{key}={value!r}" for key, value in key_values if value is not None 

87 ) 

88 

89 def get_sorted_tags(self) -> List[Tag]: 

90 """ 

91 Return the supported PEP 425 tags to check wheel candidates against. 

92 

93 The tags are returned in order of preference (most preferred first). 

94 """ 

95 if self._valid_tags is None: 

96 # Pass versions=None if no py_version_info was given since 

97 # versions=None uses special default logic. 

98 py_version_info = self._given_py_version_info 

99 if py_version_info is None: 

100 version = None 

101 else: 

102 version = version_info_to_nodot(py_version_info) 

103 

104 tags = get_supported( 

105 version=version, 

106 platforms=self.platforms, 

107 abis=self.abis, 

108 impl=self.implementation, 

109 ) 

110 self._valid_tags = tags 

111 

112 return self._valid_tags 

113 

114 def get_unsorted_tags(self) -> Set[Tag]: 

115 """Exactly the same as get_sorted_tags, but returns a set. 

116 

117 This is important for performance. 

118 """ 

119 if self._valid_tags_set is None: 

120 self._valid_tags_set = set(self.get_sorted_tags()) 

121 

122 return self._valid_tags_set