Coverage for typer / utils.py: 100%

99 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-03-26 21:46 +0000

1import inspect 1acedfbg

2from collections.abc import Callable 1acedfbg

3from copy import copy 1acedfbg

4from typing import Any, cast 1acedfbg

5 

6from ._typing import Annotated, get_args, get_origin, get_type_hints 1acedfbg

7from .models import ArgumentInfo, OptionInfo, ParameterInfo, ParamMeta 1acedfbg

8 

9 

10def _param_type_to_user_string(param_type: type[ParameterInfo]) -> str: 1acedfbg

11 # Render a `ParameterInfo` subclass for use in error messages. 

12 # User code doesn't call `*Info` directly, so errors should present the classes how 

13 # they were (probably) defined in the user code. 

14 if param_type is OptionInfo: 1acedfbg

15 return "`Option`" 1acedfbg

16 elif param_type is ArgumentInfo: 1acedfbg

17 return "`Argument`" 1acedfbg

18 # This line shouldn't be reachable during normal use. 

19 return f"`{param_type.__name__}`" # pragma: no cover 

20 

21 

22class AnnotatedParamWithDefaultValueError(Exception): 1acedfbg

23 argument_name: str 1acdb

24 param_type: type[ParameterInfo] 1acdb

25 

26 def __init__(self, argument_name: str, param_type: type[ParameterInfo]): 1acedfbg

27 self.argument_name = argument_name 1acedfbg

28 self.param_type = param_type 1acedfbg

29 

30 def __str__(self) -> str: 1acedfbg

31 param_type_str = _param_type_to_user_string(self.param_type) 1acedfbg

32 return ( 1acedfbg

33 f"{param_type_str} default value cannot be set in `Annotated`" 

34 f" for {self.argument_name!r}. Set the default value with `=` instead." 

35 ) 

36 

37 

38class MixedAnnotatedAndDefaultStyleError(Exception): 1acedfbg

39 argument_name: str 1acdb

40 annotated_param_type: type[ParameterInfo] 1acdb

41 default_param_type: type[ParameterInfo] 1acdb

42 

43 def __init__( 1acedfbg

44 self, 

45 argument_name: str, 

46 annotated_param_type: type[ParameterInfo], 

47 default_param_type: type[ParameterInfo], 

48 ): 

49 self.argument_name = argument_name 1acedfbg

50 self.annotated_param_type = annotated_param_type 1acedfbg

51 self.default_param_type = default_param_type 1acedfbg

52 

53 def __str__(self) -> str: 1acedfbg

54 annotated_param_type_str = _param_type_to_user_string(self.annotated_param_type) 1acedfbg

55 default_param_type_str = _param_type_to_user_string(self.default_param_type) 1acedfbg

56 msg = f"Cannot specify {annotated_param_type_str} in `Annotated` and" 1acedfbg

57 if self.annotated_param_type is self.default_param_type: 1acedfbg

58 msg += " default value" 1acedfbg

59 else: 

60 msg += f" {default_param_type_str} as a default value" 1acedfbg

61 msg += f" together for {self.argument_name!r}" 1acedfbg

62 return msg 1acedfbg

63 

64 

65class MultipleTyperAnnotationsError(Exception): 1acedfbg

66 argument_name: str 1acdb

67 

68 def __init__(self, argument_name: str): 1acedfbg

69 self.argument_name = argument_name 1acedfbg

70 

71 def __str__(self) -> str: 1acedfbg

72 return ( 1acedfbg

73 "Cannot specify multiple `Annotated` Typer arguments" 

74 f" for {self.argument_name!r}" 

75 ) 

76 

77 

78class DefaultFactoryAndDefaultValueError(Exception): 1acedfbg

79 argument_name: str 1acdb

80 param_type: type[ParameterInfo] 1acdb

81 

82 def __init__(self, argument_name: str, param_type: type[ParameterInfo]): 1acedfbg

83 self.argument_name = argument_name 1acedfbg

84 self.param_type = param_type 1acedfbg

85 

86 def __str__(self) -> str: 1acedfbg

87 param_type_str = _param_type_to_user_string(self.param_type) 1acedfbg

88 return ( 1acedfbg

89 "Cannot specify `default_factory` and a default value together" 

90 f" for {param_type_str}" 

91 ) 

92 

93 

94def _split_annotation_from_typer_annotations( 1acedfbg

95 base_annotation: type[Any], 

96) -> tuple[type[Any], list[ParameterInfo]]: 

97 if get_origin(base_annotation) is not Annotated: 1acedfbg

98 return base_annotation, [] 1acedfbg

99 base_annotation, *maybe_typer_annotations = get_args(base_annotation) 1acedfbg

100 return base_annotation, [ 1acedfbg

101 annotation 

102 for annotation in maybe_typer_annotations 

103 if isinstance(annotation, ParameterInfo) 

104 ] 

105 

106 

107def get_params_from_function(func: Callable[..., Any]) -> dict[str, ParamMeta]: 1acedfbg

108 signature = inspect.signature(func, eval_str=True) 1acedfbg

109 type_hints = get_type_hints(func) 1acedfbg

110 params = {} 1acedfbg

111 for param in signature.parameters.values(): 1acedfbg

112 annotation, typer_annotations = _split_annotation_from_typer_annotations( 1acedfbg

113 param.annotation, 

114 ) 

115 if len(typer_annotations) > 1: 1acedfbg

116 raise MultipleTyperAnnotationsError(param.name) 1acedfbg

117 

118 default = param.default 1acedfbg

119 if typer_annotations: 1acedfbg

120 # It's something like `my_param: Annotated[str, Argument()]` 

121 [parameter_info] = typer_annotations 1acedfbg

122 

123 # Forbid `my_param: Annotated[str, Argument()] = Argument("...")` 

124 if isinstance(param.default, ParameterInfo): 1acedfbg

125 raise MixedAnnotatedAndDefaultStyleError( 1acedfbg

126 argument_name=param.name, 

127 annotated_param_type=type(parameter_info), 

128 default_param_type=type(param.default), 

129 ) 

130 

131 parameter_info = copy(parameter_info) 1acedfbg

132 

133 # When used as a default, `Option` takes a default value and option names 

134 # as positional arguments: 

135 # `Option(some_value, "--some-argument", "-s")` 

136 # When used in `Annotated` (ie, what this is handling), `Option` just takes 

137 # option names as positional arguments: 

138 # `Option("--some-argument", "-s")` 

139 # In this case, the `default` attribute of `parameter_info` is actually 

140 # meant to be the first item of `param_decls`. 

141 if ( 1ab

142 isinstance(parameter_info, OptionInfo) 

143 and parameter_info.default is not ... 

144 ): 

145 parameter_info.param_decls = ( 1acedfbg

146 cast(str, parameter_info.default), 

147 *(parameter_info.param_decls or ()), 

148 ) 

149 parameter_info.default = ... 1acedfbg

150 

151 # Forbid `my_param: Annotated[str, Argument('some-default')]` 

152 if parameter_info.default is not ...: 1acedfbg

153 raise AnnotatedParamWithDefaultValueError( 1acedfbg

154 param_type=type(parameter_info), 

155 argument_name=param.name, 

156 ) 

157 if param.default is not param.empty: 1acedfbg

158 # Put the parameter's default (set by `=`) into `parameter_info`, where 

159 # typer can find it. 

160 parameter_info.default = param.default 1acedfbg

161 

162 default = parameter_info 1acedfbg

163 elif param.name in type_hints: 1acedfbg

164 # Resolve forward references. 

165 annotation = type_hints[param.name] 1acedfbg

166 

167 if isinstance(default, ParameterInfo): 1acedfbg

168 parameter_info = copy(default) 1acedfbg

169 # Click supports `default` as either 

170 # - an actual value; or 

171 # - a factory function (returning a default value.) 

172 # The two are not interchangeable for static typing, so typer allows 

173 # specifying `default_factory`. Move the `default_factory` into `default` 

174 # so click can find it. 

175 if parameter_info.default is ... and parameter_info.default_factory: 1acedfbg

176 parameter_info.default = parameter_info.default_factory 1acedfbg

177 elif parameter_info.default_factory: 1acedfbg

178 raise DefaultFactoryAndDefaultValueError( 1acedfbg

179 argument_name=param.name, param_type=type(parameter_info) 

180 ) 

181 default = parameter_info 1acedfbg

182 

183 params[param.name] = ParamMeta( 1acedfbg

184 name=param.name, default=default, annotation=annotation 

185 ) 

186 return params 1acedfbg

187 

188 

189def parse_boolean_env_var(env_var_value: str | None, default: bool) -> bool: 1acedfbg

190 if env_var_value is None: 1acedfbg

191 return default 1acedfbg

192 value = env_var_value.lower() 1acedfbg

193 if value in ("y", "yes", "t", "true", "on", "1"): 1acedfbg

194 return True 1acedfbg

195 if value in ("n", "no", "f", "false", "off", "0"): 1acedfbg

196 return False 1acedfbg

197 return default 1acedfbg