Coverage for typer/models.py: 100%
130 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-11-13 11:07 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-11-13 11:07 +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 # TODO: remove is_flag and flag_value in a future release
302 is_flag: Optional[bool] = None,
303 flag_value: Optional[Any] = None,
304 count: bool = False,
305 allow_from_autoenv: bool = True,
306 help: Optional[str] = None,
307 hidden: bool = False,
308 show_choices: bool = True,
309 show_envvar: bool = True,
310 # Choice
311 case_sensitive: bool = True,
312 # Numbers
313 min: Optional[Union[int, float]] = None,
314 max: Optional[Union[int, float]] = None,
315 clamp: bool = False,
316 # DateTime
317 formats: Optional[List[str]] = None,
318 # File
319 mode: Optional[str] = None,
320 encoding: Optional[str] = None,
321 errors: Optional[str] = "strict",
322 lazy: Optional[bool] = None,
323 atomic: bool = False,
324 # Path
325 exists: bool = False,
326 file_okay: bool = True,
327 dir_okay: bool = True,
328 writable: bool = False,
329 readable: bool = True,
330 resolve_path: bool = False,
331 allow_dash: bool = False,
332 path_type: Union[None, Type[str], Type[bytes]] = None,
333 # Rich settings
334 rich_help_panel: Union[str, None] = None,
335 ):
336 super().__init__( 1habcdefg
337 default=default,
338 param_decls=param_decls,
339 callback=callback,
340 metavar=metavar,
341 expose_value=expose_value,
342 is_eager=is_eager,
343 envvar=envvar,
344 shell_complete=shell_complete,
345 autocompletion=autocompletion,
346 default_factory=default_factory,
347 # Custom type
348 parser=parser,
349 click_type=click_type,
350 # TyperArgument
351 show_default=show_default,
352 show_choices=show_choices,
353 show_envvar=show_envvar,
354 help=help,
355 hidden=hidden,
356 # Choice
357 case_sensitive=case_sensitive,
358 # Numbers
359 min=min,
360 max=max,
361 clamp=clamp,
362 # DateTime
363 formats=formats,
364 # File
365 mode=mode,
366 encoding=encoding,
367 errors=errors,
368 lazy=lazy,
369 atomic=atomic,
370 # Path
371 exists=exists,
372 file_okay=file_okay,
373 dir_okay=dir_okay,
374 writable=writable,
375 readable=readable,
376 resolve_path=resolve_path,
377 allow_dash=allow_dash,
378 path_type=path_type,
379 # Rich settings
380 rich_help_panel=rich_help_panel,
381 )
382 if is_flag is not None or flag_value is not None: 1habcdefg
383 import warnings 1habcdefg
385 warnings.warn( 1habcdefg
386 "The 'is_flag' and 'flag_value' parameters are not supported by Typer "
387 "and will be removed entirely in a future release.",
388 DeprecationWarning,
389 stacklevel=2,
390 )
391 self.prompt = prompt 1habcdefg
392 self.confirmation_prompt = confirmation_prompt 1habcdefg
393 self.prompt_required = prompt_required 1habcdefg
394 self.hide_input = hide_input 1habcdefg
395 self.count = count 1habcdefg
396 self.allow_from_autoenv = allow_from_autoenv 1habcdefg
399class ArgumentInfo(ParameterInfo): 1habcdefg
400 def __init__( 1abcdefg
401 self,
402 *,
403 # ParameterInfo
404 default: Optional[Any] = None,
405 param_decls: Optional[Sequence[str]] = None,
406 callback: Optional[Callable[..., Any]] = None,
407 metavar: Optional[str] = None,
408 expose_value: bool = True,
409 is_eager: bool = False,
410 envvar: Optional[Union[str, List[str]]] = None,
411 shell_complete: Optional[
412 Callable[
413 [click.Context, click.Parameter, str],
414 Union[List["click.shell_completion.CompletionItem"], List[str]],
415 ]
416 ] = None,
417 autocompletion: Optional[Callable[..., Any]] = None,
418 default_factory: Optional[Callable[[], Any]] = None,
419 # Custom type
420 parser: Optional[Callable[[str], Any]] = None,
421 click_type: Optional[click.ParamType] = None,
422 # TyperArgument
423 show_default: Union[bool, str] = True,
424 show_choices: bool = True,
425 show_envvar: bool = True,
426 help: Optional[str] = None,
427 hidden: bool = False,
428 # Choice
429 case_sensitive: bool = True,
430 # Numbers
431 min: Optional[Union[int, float]] = None,
432 max: Optional[Union[int, float]] = None,
433 clamp: bool = False,
434 # DateTime
435 formats: Optional[List[str]] = None,
436 # File
437 mode: Optional[str] = None,
438 encoding: Optional[str] = None,
439 errors: Optional[str] = "strict",
440 lazy: Optional[bool] = None,
441 atomic: bool = False,
442 # Path
443 exists: bool = False,
444 file_okay: bool = True,
445 dir_okay: bool = True,
446 writable: bool = False,
447 readable: bool = True,
448 resolve_path: bool = False,
449 allow_dash: bool = False,
450 path_type: Union[None, Type[str], Type[bytes]] = None,
451 # Rich settings
452 rich_help_panel: Union[str, None] = None,
453 ):
454 super().__init__( 1habcdefg
455 default=default,
456 param_decls=param_decls,
457 callback=callback,
458 metavar=metavar,
459 expose_value=expose_value,
460 is_eager=is_eager,
461 envvar=envvar,
462 shell_complete=shell_complete,
463 autocompletion=autocompletion,
464 default_factory=default_factory,
465 # Custom type
466 parser=parser,
467 click_type=click_type,
468 # TyperArgument
469 show_default=show_default,
470 show_choices=show_choices,
471 show_envvar=show_envvar,
472 help=help,
473 hidden=hidden,
474 # Choice
475 case_sensitive=case_sensitive,
476 # Numbers
477 min=min,
478 max=max,
479 clamp=clamp,
480 # DateTime
481 formats=formats,
482 # File
483 mode=mode,
484 encoding=encoding,
485 errors=errors,
486 lazy=lazy,
487 atomic=atomic,
488 # Path
489 exists=exists,
490 file_okay=file_okay,
491 dir_okay=dir_okay,
492 writable=writable,
493 readable=readable,
494 resolve_path=resolve_path,
495 allow_dash=allow_dash,
496 path_type=path_type,
497 # Rich settings
498 rich_help_panel=rich_help_panel,
499 )
502class ParamMeta: 1habcdefg
503 empty = inspect.Parameter.empty 1habcdefg
505 def __init__( 1abcdefg
506 self,
507 *,
508 name: str,
509 default: Any = inspect.Parameter.empty,
510 annotation: Any = inspect.Parameter.empty,
511 ) -> None:
512 self.name = name 1habcdefg
513 self.default = default 1habcdefg
514 self.annotation = annotation 1habcdefg
517class DeveloperExceptionConfig: 1habcdefg
518 def __init__( 1abcdefg
519 self,
520 *,
521 pretty_exceptions_enable: bool = True,
522 pretty_exceptions_show_locals: bool = True,
523 pretty_exceptions_short: bool = True,
524 ) -> None:
525 self.pretty_exceptions_enable = pretty_exceptions_enable 1habcdefg
526 self.pretty_exceptions_show_locals = pretty_exceptions_show_locals 1habcdefg
527 self.pretty_exceptions_short = pretty_exceptions_short 1habcdefg