Coverage for typer / _completion_classes.py: 100%

115 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-02-09 12:36 +0000

1import importlib.util 1abcdefgh

2import os 1abcdefgh

3import re 1abcdefgh

4import sys 1abcdefgh

5from typing import Any 1abcdefgh

6 

7import click 1abcdefgh

8import click.parser 1abcdefgh

9import click.shell_completion 1abcdefgh

10 

11from ._completion_shared import ( 1abcdefgh

12 COMPLETION_SCRIPT_BASH, 

13 COMPLETION_SCRIPT_FISH, 

14 COMPLETION_SCRIPT_POWER_SHELL, 

15 COMPLETION_SCRIPT_ZSH, 

16 Shells, 

17) 

18 

19try: 1abcdefgh

20 from click.shell_completion import split_arg_string as click_split_arg_string 1abcdefgh

21except ImportError: # pragma: no cover 

22 # TODO: when removing support for Click < 8.2, remove this import 

23 from click.parser import ( # type: ignore[no-redef] 

24 split_arg_string as click_split_arg_string, 

25 ) 

26 

27 

28def _sanitize_help_text(text: str) -> str: 1abcdefgh

29 """Sanitizes the help text by removing rich tags""" 

30 if not importlib.util.find_spec("rich"): 1abcdefgh

31 return text 1abcdefgh

32 from . import rich_utils 1abcdefgh

33 

34 return rich_utils.rich_render_text(text) 1abcdefgh

35 

36 

37class BashComplete(click.shell_completion.BashComplete): 1abcdefgh

38 name = Shells.bash.value 1abcdefgh

39 source_template = COMPLETION_SCRIPT_BASH 1abcdefgh

40 

41 def source_vars(self) -> dict[str, Any]: 1abcdefgh

42 return { 1abcdefgh

43 "complete_func": self.func_name, 

44 "autocomplete_var": self.complete_var, 

45 "prog_name": self.prog_name, 

46 } 

47 

48 def get_completion_args(self) -> tuple[list[str], str]: 1abcdefgh

49 cwords = click_split_arg_string(os.environ["COMP_WORDS"]) 1abcdefgh

50 cword = int(os.environ["COMP_CWORD"]) 1abcdefgh

51 args = cwords[1:cword] 1abcdefgh

52 

53 try: 1abcdefgh

54 incomplete = cwords[cword] 1abcdefgh

55 except IndexError: 1abcdefgh

56 incomplete = "" 1abcdefgh

57 

58 return args, incomplete 1abcdefgh

59 

60 def format_completion(self, item: click.shell_completion.CompletionItem) -> str: 1abcdefgh

61 # TODO: Explore replicating the new behavior from Click, with item types and 

62 # triggering completion for files and directories 

63 # return f"{item.type},{item.value}" 

64 return f"{item.value}" 1abcdefgh

65 

66 def complete(self) -> str: 1abcdefgh

67 args, incomplete = self.get_completion_args() 1abcdefgh

68 completions = self.get_completions(args, incomplete) 1abcdefgh

69 out = [self.format_completion(item) for item in completions] 1abcdefgh

70 return "\n".join(out) 1abcdefgh

71 

72 

73class ZshComplete(click.shell_completion.ZshComplete): 1abcdefgh

74 name = Shells.zsh.value 1abcdefgh

75 source_template = COMPLETION_SCRIPT_ZSH 1abcdefgh

76 

77 def source_vars(self) -> dict[str, Any]: 1abcdefgh

78 return { 1abcdefgh

79 "complete_func": self.func_name, 

80 "autocomplete_var": self.complete_var, 

81 "prog_name": self.prog_name, 

82 } 

83 

84 def get_completion_args(self) -> tuple[list[str], str]: 1abcdefgh

85 completion_args = os.getenv("_TYPER_COMPLETE_ARGS", "") 1abcdefgh

86 cwords = click_split_arg_string(completion_args) 1abcdefgh

87 args = cwords[1:] 1abcdefgh

88 if args and not completion_args.endswith(" "): 1abcdefgh

89 incomplete = args[-1] 1abcdefgh

90 args = args[:-1] 1abcdefgh

91 else: 

92 incomplete = "" 1abcdefgh

93 return args, incomplete 1abcdefgh

94 

95 def format_completion(self, item: click.shell_completion.CompletionItem) -> str: 1abcdefgh

96 def escape(s: str) -> str: 1abcdefgh

97 return ( 1abcdefgh

98 s.replace('"', '""') 

99 .replace("'", "''") 

100 .replace("$", "\\$") 

101 .replace("`", "\\`") 

102 .replace(":", r"\\:") 

103 ) 

104 

105 # TODO: Explore replicating the new behavior from Click, pay attention to 

106 # the difference with and without escape 

107 # return f"{item.type}\n{item.value}\n{item.help if item.help else '_'}" 

108 if item.help: 1abcdefgh

109 return f'"{escape(item.value)}":"{_sanitize_help_text(escape(item.help))}"' 1abcdefgh

110 else: 

111 return f'"{escape(item.value)}"' 1abcdefgh

112 

113 def complete(self) -> str: 1abcdefgh

114 args, incomplete = self.get_completion_args() 1abcdefgh

115 completions = self.get_completions(args, incomplete) 1abcdefgh

116 res = [self.format_completion(item) for item in completions] 1abcdefgh

117 if res: 1abcdefgh

118 args_str = "\n".join(res) 1abcdefgh

119 return f"_arguments '*: :(({args_str}))'" 1abcdefgh

120 else: 

121 return "_files" 1abcdefgh

122 

123 

124class FishComplete(click.shell_completion.FishComplete): 1abcdefgh

125 name = Shells.fish.value 1abcdefgh

126 source_template = COMPLETION_SCRIPT_FISH 1abcdefgh

127 

128 def source_vars(self) -> dict[str, Any]: 1abcdefgh

129 return { 1abcdefgh

130 "complete_func": self.func_name, 

131 "autocomplete_var": self.complete_var, 

132 "prog_name": self.prog_name, 

133 } 

134 

135 def get_completion_args(self) -> tuple[list[str], str]: 1abcdefgh

136 completion_args = os.getenv("_TYPER_COMPLETE_ARGS", "") 1abcdefgh

137 cwords = click_split_arg_string(completion_args) 1abcdefgh

138 args = cwords[1:] 1abcdefgh

139 if args and not completion_args.endswith(" "): 1abcdefgh

140 incomplete = args[-1] 1abcdefgh

141 args = args[:-1] 1abcdefgh

142 else: 

143 incomplete = "" 1abcdefgh

144 return args, incomplete 1abcdefgh

145 

146 def format_completion(self, item: click.shell_completion.CompletionItem) -> str: 1abcdefgh

147 # TODO: Explore replicating the new behavior from Click, pay attention to 

148 # the difference with and without formatted help 

149 # if item.help: 

150 # return f"{item.type},{item.value}\t{item.help}" 

151 

152 # return f"{item.type},{item.value} 

153 if item.help: 1abcdefgh

154 formatted_help = re.sub(r"\s", " ", item.help) 1abcdefgh

155 return f"{item.value}\t{_sanitize_help_text(formatted_help)}" 1abcdefgh

156 else: 

157 return f"{item.value}" 1abcdefgh

158 

159 def complete(self) -> str: 1abcdefgh

160 complete_action = os.getenv("_TYPER_COMPLETE_FISH_ACTION", "") 1abcdefgh

161 args, incomplete = self.get_completion_args() 1abcdefgh

162 completions = self.get_completions(args, incomplete) 1abcdefgh

163 show_args = [self.format_completion(item) for item in completions] 1abcdefgh

164 if complete_action == "get-args": 1abcdefgh

165 if show_args: 1abcdefgh

166 return "\n".join(show_args) 1abcdefgh

167 elif complete_action == "is-args": 1abcdefgh

168 if show_args: 1abcdefgh

169 # Activate complete args (no files) 

170 sys.exit(0) 1abcdefgh

171 else: 

172 # Deactivate complete args (allow files) 

173 sys.exit(1) 1abcdefgh

174 return "" # pragma: no cover 

175 

176 

177class PowerShellComplete(click.shell_completion.ShellComplete): 1abcdefgh

178 name = Shells.powershell.value 1abcdefgh

179 source_template = COMPLETION_SCRIPT_POWER_SHELL 1abcdefgh

180 

181 def source_vars(self) -> dict[str, Any]: 1abcdefgh

182 return { 1abcdefgh

183 "complete_func": self.func_name, 

184 "autocomplete_var": self.complete_var, 

185 "prog_name": self.prog_name, 

186 } 

187 

188 def get_completion_args(self) -> tuple[list[str], str]: 1abcdefgh

189 completion_args = os.getenv("_TYPER_COMPLETE_ARGS", "") 1abcdefgh

190 incomplete = os.getenv("_TYPER_COMPLETE_WORD_TO_COMPLETE", "") 1abcdefgh

191 cwords = click_split_arg_string(completion_args) 1abcdefgh

192 args = cwords[1:-1] if incomplete else cwords[1:] 1abcdefgh

193 return args, incomplete 1abcdefgh

194 

195 def format_completion(self, item: click.shell_completion.CompletionItem) -> str: 1abcdefgh

196 return f"{item.value}:::{_sanitize_help_text(item.help) if item.help else ' '}" 1abcdefgh

197 

198 

199def completion_init() -> None: 1abcdefgh

200 click.shell_completion.add_completion_class(BashComplete, Shells.bash.value) 1abcdefgh

201 click.shell_completion.add_completion_class(ZshComplete, Shells.zsh.value) 1abcdefgh

202 click.shell_completion.add_completion_class(FishComplete, Shells.fish.value) 1abcdefgh

203 click.shell_completion.add_completion_class( 1abcdefgh

204 PowerShellComplete, Shells.powershell.value 

205 ) 

206 click.shell_completion.add_completion_class(PowerShellComplete, Shells.pwsh.value) 1abcdefgh