Skip to content

Commit 291e4e9

Browse files
committed
remove _click/types and ParamType entirely
1 parent 07c724a commit 291e4e9

8 files changed

Lines changed: 21 additions & 111 deletions

File tree

typer/_click/core.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from typing import (
99
TYPE_CHECKING,
1010
Any,
11+
ClassVar,
1112
Literal,
1213
NoReturn,
1314
TypeVar,
@@ -27,7 +28,6 @@
2728
from .globals import pop_context, push_context
2829
from .parser import _OptionParser
2930
from .termui import style
30-
from .types import ParamType
3131
from .utils import echo, make_default_short_help
3232

3333
if TYPE_CHECKING:
@@ -818,7 +818,6 @@ class Parameter(ABC):
818818
def __init__(
819819
self,
820820
param_decls: Sequence[str] | None = None,
821-
type: ParamType | None = None,
822821
required: bool = False,
823822
default: Any | Callable[[], Any] | None = None,
824823
callback: Callable[[Context, "Parameter", Any], Any] | None = None,
@@ -839,15 +838,11 @@ def __init__(
839838
self.name, self.opts, self.secondary_opts = self._parse_decls(
840839
param_decls or (), expose_value
841840
)
842-
self.type = type if type is not None else ParamType()
843841

844842
# Default nargs to what the type tells us if we have that
845843
# information available.
846844
if nargs is None:
847-
if self.type.is_composite:
848-
nargs = self.type.arity
849-
else:
850-
nargs = 1
845+
nargs = 1
851846

852847
self.required = required
853848
self.callback = callback
@@ -969,6 +964,18 @@ def resolve_envvar_value(self, ctx: Context) -> str | None:
969964

970965
return None
971966

967+
envvar_list_splitter: ClassVar[str | None] = None
968+
969+
def split_envvar_value(self, rv: str) -> Sequence[str]:
970+
"""Given a value from an environment variable this splits it up
971+
into small chunks depending on the defined envvar list splitter.
972+
973+
If the splitter is set to `None`, which means that whitespace splits,
974+
then leading and trailing whitespace is ignored. Otherwise, leading
975+
and trailing splitters usually lead to empty items being included.
976+
"""
977+
return (rv or "").split(self.envvar_list_splitter)
978+
972979
def value_from_envvar(self, ctx: Context) -> str | Sequence[str] | None:
973980
"""Process the value from the environment variable.
974981
@@ -978,7 +985,7 @@ def value_from_envvar(self, ctx: Context) -> str | Sequence[str] | None:
978985
rv: Any | None = self.resolve_envvar_value(ctx)
979986

980987
if rv is not None and (self.nargs != 1 or self.multiple):
981-
rv = self.type.split_envvar_value(rv)
988+
rv = self.split_envvar_value(rv)
982989

983990
return rv
984991

@@ -1031,7 +1038,6 @@ def get_error_hint(self, ctx: Context) -> str:
10311038
def shell_complete(self, ctx: Context, incomplete: str) -> list["CompletionItem"]:
10321039
"""Return a list of completions for the incomplete value. If a
10331040
``shell_complete`` function was given during init, it is used.
1034-
Otherwise, the `type` `ParamType.shell_complete` function is used.
10351041
"""
10361042
if self._custom_shell_complete is not None:
10371043
results = self._custom_shell_complete(ctx, self, incomplete)

typer/_click/decorators.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ def decorator(f: Command) -> Command:
4141
def help_option(param_decls: list[str]) -> Callable[[Command], Command]:
4242
"""Help option which prints the help page and exits the program."""
4343
from ..coercion import bool_flag_runtime_param, bool_flag_type_descriptor
44-
from .types import ParamType
4544

4645
def show_help(ctx: Context, param: Parameter, value: bool) -> None:
4746
"""Callback that print the help page on ``<stdout>`` and exits."""
@@ -59,7 +58,6 @@ def show_help(ctx: Context, param: Parameter, value: bool) -> None:
5958
help="Show this message and exit.",
6059
callback=show_help,
6160
required=False,
62-
type=ParamType(),
6361
runtime_param=bool_flag_runtime_param(),
6462
type_descriptor=bool_flag_type_descriptor(),
6563
)

typer/_click/termui.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
from .exceptions import Abort, UsageError
77
from .globals import resolve_color_default
8-
from .types import ParamType
98
from .utils import LazyFile, echo
109

1110
if TYPE_CHECKING:
@@ -80,7 +79,7 @@ def prompt(
8079
default: Any | None = None,
8180
hide_input: bool = False,
8281
confirmation_prompt: bool | str = False,
83-
type: ParamType | Any | None = None,
82+
type: Any | None = None,
8483
value_proc: Callable[[str], Any] | None = None,
8584
prompt_suffix: str = ": ",
8685
show_default: bool = True,

typer/_click/types.py

Lines changed: 0 additions & 61 deletions
This file was deleted.

typer/coercion.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from . import adapters
1111
from ._click import Context
1212
from ._click.exceptions import BadParameter, UsageError
13-
from ._click.types import ParamType
1413
from ._typing import get_args, get_origin, is_number_type
1514
from .adapters import validation_context
1615
from .display import get_error_msg
@@ -38,7 +37,6 @@ class TypeDescriptor:
3837

3938
annotation: ParameterAnnotation
4039
parameter_info: ParameterInfo
41-
param_type: ParamType
4240
adapter: TypeAdapter[Any] | None
4341
file_annotation: Any | None
4442

@@ -134,16 +132,14 @@ def resolve_type_descriptor(
134132
annotation: ParameterAnnotation,
135133
parameter_info: ParameterInfo,
136134
) -> TypeDescriptor:
137-
"""Resolve ParamType and Pydantic adapter for one parameter annotation."""
138-
param_type = ParamType()
135+
"""Resolve Pydantic adapter for one parameter annotation."""
139136
file_annotation = file_coercion_annotation(annotation)
140137
adapter = None
141138
if file_annotation is None:
142139
adapter = adapters.try_build_adapter(annotation, parameter_info)
143140
return TypeDescriptor(
144141
annotation=annotation,
145142
parameter_info=parameter_info,
146-
param_type=param_type,
147143
adapter=adapter,
148144
file_annotation=file_annotation,
149145
)

typer/core.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from . import _click, param_types
1919
from ._click.parser import _OptionParser
2020
from ._click.shell_completion import CompletionItem
21-
from ._click.types import ParamType
2221
from ._typing import Literal
2322
from .coercion import RuntimeParam, TypeDescriptor
2423
from .display import describe_number_range
@@ -125,7 +124,7 @@ def value_from_envvar(self, ctx: _click.Context) -> str | Sequence[str] | None:
125124
if splitter is not None:
126125
rv = (rv or "").split(splitter)
127126
else:
128-
rv = self.type.split_envvar_value(rv)
127+
rv = self.split_envvar_value(rv)
129128
return rv
130129

131130
def shell_complete(
@@ -150,11 +149,8 @@ def shell_complete(
150149
# file
151150
if desc.is_file:
152151
return [CompletionItem(incomplete, type="file")]
153-
# path
154-
if desc.is_path:
155-
return []
156-
# fall-back
157-
return self.type.shell_complete(ctx, self, incomplete)
152+
# fall-back, specifically also required for path's
153+
return []
158154

159155
def make_metavar(self, ctx: _click.Context) -> str | None:
160156
return self.metavar
@@ -386,7 +382,6 @@ def __init__(
386382
*,
387383
# Parameter
388384
param_decls: list[str],
389-
type: ParamType,
390385
runtime_param: RuntimeParam,
391386
type_descriptor: TypeDescriptor,
392387
required: bool = False,
@@ -430,7 +425,6 @@ def __init__(
430425

431426
super().__init__(
432427
param_decls=param_decls,
433-
type=type,
434428
required=required,
435429
default=default,
436430
callback=callback,
@@ -607,7 +601,6 @@ def __init__(
607601
*,
608602
# Parameter
609603
param_decls: list[str],
610-
type: ParamType,
611604
runtime_param: RuntimeParam,
612605
type_descriptor: TypeDescriptor,
613606
required: bool = False,
@@ -656,7 +649,6 @@ def __init__(
656649

657650
super().__init__(
658651
param_decls,
659-
type=type,
660652
multiple=multiple,
661653
required=required,
662654
default=default,

typer/main.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,7 +1484,6 @@ def get_param(param: ParamMeta) -> TyperArgument | TyperOption:
14841484
annotation=annotation,
14851485
parameter_info=parameter_info,
14861486
)
1487-
parameter_type = descriptor.param_type
14881487
runtime_param = build_runtime_param(descriptor)
14891488
tuple_nargs = descriptor.tuple_arity
14901489

@@ -1513,7 +1512,6 @@ def get_param(param: ParamMeta) -> TyperArgument | TyperOption:
15131512
multiple=is_list,
15141513
count=parameter_info.count,
15151514
allow_from_autoenv=parameter_info.allow_from_autoenv,
1516-
type=parameter_type,
15171515
help=parameter_info.help,
15181516
hidden=parameter_info.hidden,
15191517
show_choices=parameter_info.show_choices,
@@ -1546,7 +1544,6 @@ def get_param(param: ParamMeta) -> TyperArgument | TyperOption:
15461544
return TyperArgument(
15471545
# Argument
15481546
param_decls=param_decls,
1549-
type=parameter_type,
15501547
required=required,
15511548
nargs=nargs,
15521549
# TyperArgument

typer/param_types.py

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
from ._click import Context
1717
from ._click._compat import open_stream
1818
from ._click.exceptions import BadParameter
19-
from ._click.shell_completion import CompletionItem
20-
from ._click.types import ParamType
2119
from ._click.utils import LazyFile, format_filename, safecall
2220
from ._typing import get_args, get_origin, is_literal_type, is_union, literal_values
2321
from .display import get_error_msg
@@ -60,7 +58,7 @@ def infer_annotation_from_default(default: Any | None) -> ParameterAnnotation:
6058

6159

6260
def annotation_from_prompt(t: Any | None, default: Any | None) -> ParameterAnnotation:
63-
if t is not None and not isinstance(t, ParamType):
61+
if t is not None:
6462
return t
6563
return infer_annotation_from_default(default)
6664

@@ -153,21 +151,6 @@ def choice_as_str(choice: Any) -> str:
153151
return str(choice)
154152

155153

156-
def choice_shell_complete(
157-
choices: Sequence[Any],
158-
*,
159-
case_sensitive: bool,
160-
incomplete: str,
161-
) -> list[CompletionItem]:
162-
str_choices = map(choice_as_str, choices)
163-
if case_sensitive:
164-
matched = (c for c in str_choices if c.startswith(incomplete))
165-
else:
166-
incomplete = incomplete.lower()
167-
matched = (c for c in str_choices if c.lower().startswith(incomplete))
168-
return [CompletionItem(c) for c in matched]
169-
170-
171154
# PATH #
172155
def path_metavar_label(parameter_info: ParameterInfo) -> str:
173156
if parameter_info.file_okay and not parameter_info.dir_okay:

0 commit comments

Comments
 (0)