Coverage for typer/models.py: 100%
129 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-09 18:26 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-09 18:26 +0000
1import inspect 1habcdefg
2import io 1habcdefg
3from typing import ( 1habcdefg
4 TYPE_CHECKING,
5 Any,
6 Callable,
7 Dict,
8 List,
9 Optional,
10 Sequence,
11 Type,
12 TypeVar,
13 Union,
14)
16import click 1habcdefg
17import click.shell_completion 1habcdefg
19if TYPE_CHECKING: # pragma: no cover 1habcdefg
20 from .core import TyperCommand, TyperGroup
21 from .main import Typer
24NoneType = type(None) 1habcdefg
26AnyType = Type[Any] 1habcdefg
28Required = ... 1habcdefg
31class Context(click.Context): 1habcdefg
32 pass 1habcdefg
35class FileText(io.TextIOWrapper): 1habcdefg
36 pass 1habcdefg
39class FileTextWrite(FileText): 1habcdefg
40 pass 1habcdefg
43class FileBinaryRead(io.BufferedReader): 1habcdefg
44 pass 1habcdefg
47class FileBinaryWrite(io.BufferedWriter): 1habcdefg
48 pass 1habcdefg
51class CallbackParam(click.Parameter): 1habcdefg
52 pass 1habcdefg
55class DefaultPlaceholder: 1habcdefg
56 """
57 You shouldn't use this class directly.
59 It's used internally to recognize when a default value has been overwritten, even
60 if the new value is `None`.
61 """
63 def __init__(self, value: Any): 1habcdefg
64 self.value = value 1habcdefg
66 def __bool__(self) -> bool: 1habcdefg
67 return bool(self.value) 1habcdefg
70DefaultType = TypeVar("DefaultType") 1habcdefg
72CommandFunctionType = TypeVar("CommandFunctionType", bound=Callable[..., Any]) 1habcdefg
75def Default(value: DefaultType) -> DefaultType: 1habcdefg
76 """
77 You shouldn't use this function directly.
79 It's used internally to recognize when a default value has been overwritten, even
80 if the new value is `None`.
81 """
82 return DefaultPlaceholder(value) # type: ignore 1habcdefg
85class CommandInfo: 1habcdefg
86 def __init__( 1abcdefg
87 self,
88 name: Optional[str] = None,
89 *,
90 cls: Optional[Type["TyperCommand"]] = None,
91 context_settings: Optional[Dict[Any, Any]] = None,
92 callback: Optional[Callable[..., Any]] = None,
93 help: Optional[str] = None,
94 epilog: Optional[str] = None,
95 short_help: Optional[str] = None,
96 options_metavar: str = "[OPTIONS]",
97 add_help_option: bool = True,
98 no_args_is_help: bool = False,
99 hidden: bool = False,
100 deprecated: bool = False,
101 # Rich settings
102 rich_help_panel: Union[str, None] = None,
103 ):
104 self.name = name 1habcdefg
105 self.cls = cls 1habcdefg
106 self.context_settings = context_settings 1habcdefg
107 self.callback = callback 1habcdefg
108 self.help = help 1habcdefg
109 self.epilog = epilog 1habcdefg
110 self.short_help = short_help 1habcdefg
111 self.options_metavar = options_metavar 1habcdefg
112 self.add_help_option = add_help_option 1habcdefg
113 self.no_args_is_help = no_args_is_help 1habcdefg
114 self.hidden = hidden 1habcdefg
115 self.deprecated = deprecated 1habcdefg
116 # Rich settings
117 self.rich_help_panel = rich_help_panel 1habcdefg
120class TyperInfo: 1habcdefg
121 def __init__( 1abcdefg
122 self,
123 typer_instance: Optional["Typer"] = Default(None),
124 *,
125 name: Optional[str] = Default(None),
126 cls: Optional[Type["TyperGroup"]] = Default(None),
127 invoke_without_command: bool = Default(False),
128 no_args_is_help: bool = Default(False),
129 subcommand_metavar: Optional[str] = Default(None),
130 chain: bool = Default(False),
131 result_callback: Optional[Callable[..., Any]] = Default(None),
132 # Command
133 context_settings: Optional[Dict[Any, Any]] = Default(None),
134 callback: Optional[Callable[..., Any]] = Default(None),
135 help: Optional[str] = Default(None),
136 epilog: Optional[str] = Default(None),
137 short_help: Optional[str] = Default(None),
138 options_metavar: str = Default("[OPTIONS]"),
139 add_help_option: bool = Default(True),
140 hidden: bool = Default(False),
141 deprecated: bool = Default(False),
142 # Rich settings
143 rich_help_panel: Union[str, None] = Default(None),
144 ):
145 self.typer_instance = typer_instance 1habcdefg
146 self.name = name 1habcdefg
147 self.cls = cls 1habcdefg
148 self.invoke_without_command = invoke_without_command 1habcdefg
149 self.no_args_is_help = no_args_is_help 1habcdefg
150 self.subcommand_metavar = subcommand_metavar 1habcdefg
151 self.chain = chain 1habcdefg
152 self.result_callback = result_callback 1habcdefg
153 self.context_settings = context_settings 1habcdefg
154 self.callback = callback 1habcdefg
155 self.help = help 1habcdefg
156 self.epilog = epilog 1habcdefg
157 self.short_help = short_help 1habcdefg
158 self.options_metavar = options_metavar 1habcdefg
159 self.add_help_option = add_help_option 1habcdefg
160 self.hidden = hidden 1habcdefg
161 self.deprecated = deprecated 1habcdefg
162 self.rich_help_panel = rich_help_panel 1habcdefg
165class ParameterInfo: 1habcdefg
166 def __init__( 1abcdefg
167 self,
168 *,
169 default: Optional[Any] = None,
170 param_decls: Optional[Sequence[str]] = None,
171 callback: Optional[Callable[..., Any]] = None,
172 metavar: Optional[str] = None,
173 expose_value: bool = True,
174 is_eager: bool = False,
175 envvar: Optional[Union[str, List[str]]] = None,
176 shell_complete: Optional[
177 Callable[
178 [click.Context, click.Parameter, str],
179 Union[List["click.shell_completion.CompletionItem"], List[str]],
180 ]
181 ] = None,
182 autocompletion: Optional[Callable[..., Any]] = None,
183 default_factory: Optional[Callable[[], Any]] = None,
184 # Custom type
185 parser: Optional[Callable[[str], Any]] = None,
186 click_type: Optional[click.ParamType] = None,
187 # TyperArgument
188 show_default: Union[bool, str] = True,
189 show_choices: bool = True,
190 show_envvar: bool = True,
191 help: Optional[str] = None,
192 hidden: bool = False,
193 # Choice
194 case_sensitive: bool = True,
195 # Numbers
196 min: Optional[Union[int, float]] = None,
197 max: Optional[Union[int, float]] = None,
198 clamp: bool = False,
199 # DateTime
200 formats: Optional[List[str]] = None,
201 # File
202 mode: Optional[str] = None,
203 encoding: Optional[str] = None,
204 errors: Optional[str] = "strict",
205 lazy: Optional[bool] = None,
206 atomic: bool = False,
207 # Path
208 exists: bool = False,
209 file_okay: bool = True,
210 dir_okay: bool = True,
211 writable: bool = False,
212 readable: bool = True,
213 resolve_path: bool = False,
214 allow_dash: bool = False,
215 path_type: Union[None, Type[str], Type[bytes]] = None,
216 # Rich settings
217 rich_help_panel: Union[str, None] = None,
218 ):
219 # Check if user has provided multiple custom parsers
220 if parser and click_type: 1habcdefg
221 raise ValueError( 1habcdefg
222 "Multiple custom type parsers provided. "
223 "`parser` and `click_type` may not both be provided."
224 )
226 self.default = default 1habcdefg
227 self.param_decls = param_decls 1habcdefg
228 self.callback = callback 1habcdefg
229 self.metavar = metavar 1habcdefg
230 self.expose_value = expose_value 1habcdefg
231 self.is_eager = is_eager 1habcdefg
232 self.envvar = envvar 1habcdefg
233 self.shell_complete = shell_complete 1habcdefg
234 self.autocompletion = autocompletion 1habcdefg
235 self.default_factory = default_factory 1habcdefg
236 # Custom type
237 self.parser = parser 1habcdefg
238 self.click_type = click_type 1habcdefg
239 # TyperArgument
240 self.show_default = show_default 1habcdefg
241 self.show_choices = show_choices 1habcdefg
242 self.show_envvar = show_envvar 1habcdefg
243 self.help = help 1habcdefg
244 self.hidden = hidden 1habcdefg
245 # Choice
246 self.case_sensitive = case_sensitive 1habcdefg
247 # Numbers
248 self.min = min 1habcdefg
249 self.max = max 1habcdefg
250 self.clamp = clamp 1habcdefg
251 # DateTime
252 self.formats = formats 1habcdefg
253 # File
254 self.mode = mode 1habcdefg
255 self.encoding = encoding 1habcdefg
256 self.errors = errors 1habcdefg
257 self.lazy = lazy 1habcdefg
258 self.atomic = atomic 1habcdefg
259 # Path
260 self.exists = exists 1habcdefg
261 self.file_okay = file_okay 1habcdefg
262 self.dir_okay = dir_okay 1habcdefg
263 self.writable = writable 1habcdefg
264 self.readable = readable 1habcdefg
265 self.resolve_path = resolve_path 1habcdefg
266 self.allow_dash = allow_dash 1habcdefg
267 self.path_type = path_type 1habcdefg
268 # Rich settings
269 self.rich_help_panel = rich_help_panel 1habcdefg
272class OptionInfo(ParameterInfo): 1habcdefg
273 def __init__( 1abcdefg
274 self,
275 *,
276 # ParameterInfo
277 default: Optional[Any] = None,
278 param_decls: Optional[Sequence[str]] = None,
279 callback: Optional[Callable[..., Any]] = None,
280 metavar: Optional[str] = None,
281 expose_value: bool = True,
282 is_eager: bool = False,
283 envvar: Optional[Union[str, List[str]]] = None,
284 shell_complete: Optional[
285 Callable[
286 [click.Context, click.Parameter, str],
287 Union[List["click.shell_completion.CompletionItem"], List[str]],
288 ]
289 ] = None,
290 autocompletion: Optional[Callable[..., Any]] = None,
291 default_factory: Optional[Callable[[], Any]] = None,
292 # Custom type
293 parser: Optional[Callable[[str], Any]] = None,
294 click_type: Optional[click.ParamType] = None,
295 # Option
296 show_default: Union[bool, str] = True,
297 prompt: Union[bool, str] = False,
298 confirmation_prompt: bool = False,
299 prompt_required: bool = True,
300 hide_input: bool = False,
301 is_flag: Optional[bool] = None,
302 flag_value: Optional[Any] = None,
303 count: bool = False,
304 allow_from_autoenv: bool = True,
305 help: Optional[str] = None,
306 hidden: bool = False,
307 show_choices: bool = True,
308 show_envvar: bool = True,
309 # Choice
310 case_sensitive: bool = True,
311 # Numbers
312 min: Optional[Union[int, float]] = None,
313 max: Optional[Union[int, float]] = None,
314 clamp: bool = False,
315 # DateTime
316 formats: Optional[List[str]] = None,
317 # File
318 mode: Optional[str] = None,
319 encoding: Optional[str] = None,
320 errors: Optional[str] = "strict",
321 lazy: Optional[bool] = None,
322 atomic: bool = False,
323 # Path
324 exists: bool = False,
325 file_okay: bool = True,
326 dir_okay: bool = True,
327 writable: bool = False,
328 readable: bool = True,
329 resolve_path: bool = False,
330 allow_dash: bool = False,
331 path_type: Union[None, Type[str], Type[bytes]] = None,
332 # Rich settings
333 rich_help_panel: Union[str, None] = None,
334 ):
335 super().__init__( 1habcdefg
336 default=default,
337 param_decls=param_decls,
338 callback=callback,
339 metavar=metavar,
340 expose_value=expose_value,
341 is_eager=is_eager,
342 envvar=envvar,
343 shell_complete=shell_complete,
344 autocompletion=autocompletion,
345 default_factory=default_factory,
346 # Custom type
347 parser=parser,
348 click_type=click_type,
349 # TyperArgument
350 show_default=show_default,
351 show_choices=show_choices,
352 show_envvar=show_envvar,
353 help=help,
354 hidden=hidden,
355 # Choice
356 case_sensitive=case_sensitive,
357 # Numbers
358 min=min,
359 max=max,
360 clamp=clamp,
361 # DateTime
362 formats=formats,
363 # File
364 mode=mode,
365 encoding=encoding,
366 errors=errors,
367 lazy=lazy,
368 atomic=atomic,
369 # Path
370 exists=exists,
371 file_okay=file_okay,
372 dir_okay=dir_okay,
373 writable=writable,
374 readable=readable,
375 resolve_path=resolve_path,
376 allow_dash=allow_dash,
377 path_type=path_type,
378 # Rich settings
379 rich_help_panel=rich_help_panel,
380 )
381 self.prompt = prompt 1habcdefg
382 self.confirmation_prompt = confirmation_prompt 1habcdefg
383 self.prompt_required = prompt_required 1habcdefg
384 self.hide_input = hide_input 1habcdefg
385 self.is_flag = is_flag 1habcdefg
386 self.flag_value = flag_value 1habcdefg
387 self.count = count 1habcdefg
388 self.allow_from_autoenv = allow_from_autoenv 1habcdefg
391class ArgumentInfo(ParameterInfo): 1habcdefg
392 def __init__( 1abcdefg
393 self,
394 *,
395 # ParameterInfo
396 default: Optional[Any] = None,
397 param_decls: Optional[Sequence[str]] = None,
398 callback: Optional[Callable[..., Any]] = None,
399 metavar: Optional[str] = None,
400 expose_value: bool = True,
401 is_eager: bool = False,
402 envvar: Optional[Union[str, List[str]]] = None,
403 shell_complete: Optional[
404 Callable[
405 [click.Context, click.Parameter, str],
406 Union[List["click.shell_completion.CompletionItem"], List[str]],
407 ]
408 ] = None,
409 autocompletion: Optional[Callable[..., Any]] = None,
410 default_factory: Optional[Callable[[], Any]] = None,
411 # Custom type
412 parser: Optional[Callable[[str], Any]] = None,
413 click_type: Optional[click.ParamType] = None,
414 # TyperArgument
415 show_default: Union[bool, str] = True,
416 show_choices: bool = True,
417 show_envvar: bool = True,
418 help: Optional[str] = None,
419 hidden: bool = False,
420 # Choice
421 case_sensitive: bool = True,
422 # Numbers
423 min: Optional[Union[int, float]] = None,
424 max: Optional[Union[int, float]] = None,
425 clamp: bool = False,
426 # DateTime
427 formats: Optional[List[str]] = None,
428 # File
429 mode: Optional[str] = None,
430 encoding: Optional[str] = None,
431 errors: Optional[str] = "strict",
432 lazy: Optional[bool] = None,
433 atomic: bool = False,
434 # Path
435 exists: bool = False,
436 file_okay: bool = True,
437 dir_okay: bool = True,
438 writable: bool = False,
439 readable: bool = True,
440 resolve_path: bool = False,
441 allow_dash: bool = False,
442 path_type: Union[None, Type[str], Type[bytes]] = None,
443 # Rich settings
444 rich_help_panel: Union[str, None] = None,
445 ):
446 super().__init__( 1habcdefg
447 default=default,
448 param_decls=param_decls,
449 callback=callback,
450 metavar=metavar,
451 expose_value=expose_value,
452 is_eager=is_eager,
453 envvar=envvar,
454 shell_complete=shell_complete,
455 autocompletion=autocompletion,
456 default_factory=default_factory,
457 # Custom type
458 parser=parser,
459 click_type=click_type,
460 # TyperArgument
461 show_default=show_default,
462 show_choices=show_choices,
463 show_envvar=show_envvar,
464 help=help,
465 hidden=hidden,
466 # Choice
467 case_sensitive=case_sensitive,
468 # Numbers
469 min=min,
470 max=max,
471 clamp=clamp,
472 # DateTime
473 formats=formats,
474 # File
475 mode=mode,
476 encoding=encoding,
477 errors=errors,
478 lazy=lazy,
479 atomic=atomic,
480 # Path
481 exists=exists,
482 file_okay=file_okay,
483 dir_okay=dir_okay,
484 writable=writable,
485 readable=readable,
486 resolve_path=resolve_path,
487 allow_dash=allow_dash,
488 path_type=path_type,
489 # Rich settings
490 rich_help_panel=rich_help_panel,
491 )
494class ParamMeta: 1habcdefg
495 empty = inspect.Parameter.empty 1habcdefg
497 def __init__( 1abcdefg
498 self,
499 *,
500 name: str,
501 default: Any = inspect.Parameter.empty,
502 annotation: Any = inspect.Parameter.empty,
503 ) -> None:
504 self.name = name 1habcdefg
505 self.default = default 1habcdefg
506 self.annotation = annotation 1habcdefg
509class DeveloperExceptionConfig: 1habcdefg
510 def __init__( 1abcdefg
511 self,
512 *,
513 pretty_exceptions_enable: bool = True,
514 pretty_exceptions_show_locals: bool = True,
515 pretty_exceptions_short: bool = True,
516 ) -> None:
517 self.pretty_exceptions_enable = pretty_exceptions_enable 1habcdefg
518 self.pretty_exceptions_show_locals = pretty_exceptions_show_locals 1habcdefg
519 self.pretty_exceptions_short = pretty_exceptions_short 1habcdefg