Skip to content

Commit ded5b69

Browse files
committed
Update examples in docs, adds version update admonitions
Refs: #3371 (review)
1 parent 78a4efc commit ded5b69

4 files changed

Lines changed: 25 additions & 6 deletions

File tree

docs/parameter-types.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ The resulting value from an option will always be one of the originally passed c
7070
regardless of `case_sensitive`.
7171
```
7272

73+
```{versionchanged} 8.4.0
74+
{class}`Choice` is now generic. Parameterize it with the choice value type
75+
({class}`!Choice[HashType]` for an enum, {class}`!Choice[str]` for plain
76+
strings) to enable type-checked consumers.
77+
```
78+
7379
(ranges)=
7480

7581
### Int and Float Ranges
@@ -153,16 +159,21 @@ To implement a custom type, you need to subclass the {class}`ParamType` class. F
153159
function that fails with a `ValueError` is also supported, though discouraged. Override the {meth}`~ParamType.convert`
154160
method to convert the value from a string to the correct type.
155161

162+
{class}`ParamType` is generic in the converted value type: parameterize it with
163+
the type returned by `convert` so that consumers (and type checkers) can rely
164+
on the narrowed return type.
165+
156166
The following code implements an integer type that accepts hex and octal numbers in addition to normal integers, and
157167
converts them into regular integers.
158168

159169
```python
160170
import click
161171

162-
class BasedIntParamType(click.ParamType):
172+
173+
class BasedIntParamType(click.ParamType[int]):
163174
name = "integer"
164175

165-
def convert(self, value, param, ctx):
176+
def convert(self, value, param, ctx) -> int:
166177
if isinstance(value, int):
167178
return value
168179

@@ -175,6 +186,7 @@ class BasedIntParamType(click.ParamType):
175186
except ValueError:
176187
self.fail(f"{value!r} is not a valid integer", param, ctx)
177188

189+
178190
BASED_INT = BasedIntParamType()
179191
```
180192

@@ -184,3 +196,10 @@ conversion fails. The `param` and `ctx` arguments may be `None` in some cases su
184196
Values from user input or the command line will be strings, but default values and Python arguments may already be the
185197
correct type. The custom type should check at the top if the value is already valid and pass it through to support those
186198
cases.
199+
200+
```{versionchanged} 8.4.0
201+
{class}`ParamType` is now a generic abstract base class. Parameterize it with
202+
the converted value type ({class}`!ParamType[int]` for an integer-returning
203+
type) so that {meth}`~ParamType.convert` and downstream consumers carry the
204+
narrowed type.
205+
```

docs/shell-completion.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ indicate special handling for paths, and `help` for shells that support showing
120120
In this example, the type will suggest environment variables that start with the incomplete value.
121121

122122
```python
123-
class EnvVarType(ParamType):
123+
class EnvVarType(ParamType[str]):
124124
name = "envvar"
125125

126126
def shell_complete(self, ctx, param, incomplete):

docs/support-multiple-versions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def add_ctx_arg(f: F) -> F:
5555
Here's an example ``ParamType`` subclass which uses this:
5656

5757
```python
58-
class CommaDelimitedString(click.ParamType):
58+
class CommaDelimitedString(click.ParamType[str]):
5959
@add_ctx_arg
6060
def get_metavar(self, param: click.Parameter, ctx: click.Context | None) -> str:
6161
return "TEXT,TEXT,..."

examples/validation/validation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ def validate_count(ctx, param, value):
99
return value
1010

1111

12-
class URL(click.ParamType):
12+
class URL(click.ParamType[urlparse.ParseResult]):
1313
name = "url"
1414

15-
def convert(self, value, param, ctx):
15+
def convert(self, value, param, ctx) -> urlparse.ParseResult:
1616
if not isinstance(value, tuple):
1717
value = urlparse.urlparse(value)
1818
if value.scheme not in ("http", "https"):

0 commit comments

Comments
 (0)