|
19 | 19 | from ._click.parser import _OptionParser |
20 | 20 | from ._click.shell_completion import CompletionItem |
21 | 21 | from ._typing import Literal |
22 | | -from .coercion import RuntimeParam, TypeDescriptor |
| 22 | +from .coercion import RuntimeParam, TypeDescriptor, build_runtime_param |
23 | 23 | from .display import describe_number_range |
24 | 24 | from .param_types import choice_as_str, normalize_choice_value |
25 | 25 | from .utils import parse_boolean_env_var |
@@ -88,12 +88,19 @@ def compat_autocompletion( |
88 | 88 | class TyperParameter(_click.core.Parameter): |
89 | 89 | """Typer parameter with runtime coercion.""" |
90 | 90 |
|
91 | | - runtime_param: RuntimeParam |
| 91 | + _runtime_param: RuntimeParam | None |
92 | 92 | type_descriptor: TypeDescriptor |
93 | 93 | show_choices: bool |
94 | 94 |
|
| 95 | + def get_runtime_param(self) -> RuntimeParam: |
| 96 | + """lazy definition to avoid up-front costs""" |
| 97 | + if self._runtime_param is None: |
| 98 | + self._runtime_param = build_runtime_param(self.type_descriptor) |
| 99 | + assert self._runtime_param is not None |
| 100 | + return self._runtime_param |
| 101 | + |
95 | 102 | def process_value(self, ctx: _click.Context, value: Any) -> Any: |
96 | | - value = self.runtime_param.coerce(value, param=self, ctx=ctx) |
| 103 | + value = self.get_runtime_param().coerce(value, param=self, ctx=ctx) |
97 | 104 | if self.required and self.value_is_missing(value): |
98 | 105 | raise _click.exceptions.MissingParameter(ctx=ctx, param=self) |
99 | 106 | if self.callback is not None: |
@@ -200,7 +207,7 @@ def display_type_rich(self, ctx: _click.Context) -> str | None: |
200 | 207 | return self.display_type(ctx) |
201 | 208 |
|
202 | 209 | def bare_type(self) -> str: |
203 | | - annotation = self.runtime_param.annotation |
| 210 | + annotation = self.type_descriptor.annotation |
204 | 211 | return self._bare_type(annotation) |
205 | 212 |
|
206 | 213 | def _bare_type(self, annotation: type) -> str: |
@@ -389,7 +396,6 @@ def __init__( |
389 | 396 | *, |
390 | 397 | # Parameter |
391 | 398 | param_decls: list[str], |
392 | | - runtime_param: RuntimeParam, |
393 | 399 | type_descriptor: TypeDescriptor, |
394 | 400 | required: bool = False, |
395 | 401 | default: Any | None = None, |
@@ -427,8 +433,8 @@ def __init__( |
427 | 433 | self.min = min |
428 | 434 | self.max = max |
429 | 435 | self.rich_help_panel = rich_help_panel |
430 | | - self.runtime_param = runtime_param |
431 | 436 | self.type_descriptor = type_descriptor |
| 437 | + self._runtime_param = None |
432 | 438 |
|
433 | 439 | super().__init__( |
434 | 440 | param_decls=param_decls, |
@@ -570,7 +576,6 @@ def __init__( |
570 | 576 | *, |
571 | 577 | # Parameter |
572 | 578 | param_decls: list[str], |
573 | | - runtime_param: RuntimeParam, |
574 | 579 | type_descriptor: TypeDescriptor, |
575 | 580 | required: bool = False, |
576 | 581 | default: Any | None = None, |
@@ -613,8 +618,8 @@ def __init__( |
613 | 618 |
|
614 | 619 | self.min = min |
615 | 620 | self.max = max |
616 | | - self.runtime_param = runtime_param |
617 | 621 | self.type_descriptor = type_descriptor |
| 622 | + self._runtime_param = None |
618 | 623 |
|
619 | 624 | super().__init__( |
620 | 625 | param_decls, |
|
0 commit comments