Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/pip/_internal/models/release_control.py: 74%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

42 statements  

1from __future__ import annotations 

2 

3from dataclasses import dataclass, field 

4 

5from pip._vendor.packaging.utils import NormalizedName, canonicalize_name 

6 

7from pip._internal.exceptions import CommandError 

8 

9 

10@dataclass(slots=True) 

11class ReleaseControl: 

12 """Helper for managing which release types can be installed.""" 

13 

14 all_releases: set[str] = field(default_factory=set) 

15 only_final: set[str] = field(default_factory=set) 

16 _order: list[tuple[str, str]] = field( 

17 init=False, default_factory=list, compare=False, repr=False 

18 ) 

19 

20 def handle_mutual_excludes( 

21 self, value: str, target: set[str], other: set[str], attr_name: str 

22 ) -> None: 

23 """Parse and apply release control option value. 

24 

25 Processes comma-separated package names or special values `:all:` and `:none:`. 

26 

27 When adding packages to target, they're removed from other to maintain mutual 

28 exclusivity between all_releases and only_final. All operations are tracked in 

29 order so that the original command-line argument sequence can be reconstructed 

30 when passing options to build subprocesses. 

31 """ 

32 if value.startswith("-"): 

33 raise CommandError( 

34 "--all-releases / --only-final option requires 1 argument." 

35 ) 

36 new = value.split(",") 

37 while ":all:" in new: 

38 other.clear() 

39 target.clear() 

40 target.add(":all:") 

41 # Track :all: in order 

42 self._order.append((attr_name, ":all:")) 

43 del new[: new.index(":all:") + 1] 

44 # Without a none, we want to discard everything as :all: covers it 

45 if ":none:" not in new: 

46 return 

47 for name in new: 

48 if name == ":none:": 

49 target.clear() 

50 # Track :none: in order 

51 self._order.append((attr_name, ":none:")) 

52 continue 

53 name = canonicalize_name(name) 

54 other.discard(name) 

55 target.add(name) 

56 # Track package-specific setting in order 

57 self._order.append((attr_name, name)) 

58 

59 def get_ordered_args(self) -> list[tuple[str, str]]: 

60 """ 

61 Get ordered list of (flag_name, value) tuples for reconstructing CLI args. 

62 

63 Returns: 

64 List of tuples where each tuple is (attribute_name, value). 

65 The attribute_name is either 'all_releases' or 'only_final'. 

66 

67 Example: 

68 [("all_releases", ":all:"), ("only_final", "simple")] 

69 would be reconstructed as: 

70 ["--all-releases", ":all:", "--only-final", "simple"] 

71 """ 

72 return self._order[:] 

73 

74 def allows_prereleases(self, canonical_name: NormalizedName) -> bool | None: 

75 """ 

76 Determine if pre-releases are allowed for a package. 

77 

78 Returns: 

79 True: Pre-releases are allowed (package in all_releases) 

80 False: Only final releases allowed (package in only_final) 

81 None: No specific setting, use default behavior 

82 """ 

83 if canonical_name in self.all_releases: 

84 return True 

85 elif canonical_name in self.only_final: 

86 return False 

87 elif ":all:" in self.all_releases: 

88 return True 

89 elif ":all:" in self.only_final: 

90 return False 

91 return None