Skip to content

Commit 329b100

Browse files
authored
Merge pull request #1914 from pallets/custom-type-value
document values passed to types
2 parents ba9bb3c + 0c108f2 commit 329b100

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Unreleased
1818
line values are given. :issue:`1903`
1919
- Flag options guess their type from ``flag_value`` if given, like
2020
regular options do from ``default``. :issue:`1886`
21+
- Added documentation that custom parameter types may be passed
22+
already valid values in addition to strings. :issue:`1898`
2123

2224

2325
Version 8.0.0

docs/parameters.rst

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,15 @@ integers.
118118
name = "integer"
119119
120120
def convert(self, value, param, ctx):
121+
if isinstance(value, int):
122+
return value
123+
121124
try:
122125
if value[:2].lower() == "0x":
123126
return int(value[2:], 16)
124127
elif value[:1] == "0":
125128
return int(value, 8)
126129
return int(value, 10)
127-
except TypeError:
128-
self.fail(
129-
"expected string for int() conversion, got "
130-
f"{value!r} of type {type(value).__name__}",
131-
param,
132-
ctx,
133-
)
134130
except ValueError:
135131
self.fail(f"{value!r} is not a valid integer", param, ctx)
136132
@@ -140,3 +136,8 @@ The :attr:`~ParamType.name` attribute is optional and is used for
140136
documentation. Call :meth:`~ParamType.fail` if conversion fails. The
141137
``param`` and ``ctx`` arguments may be ``None`` in some cases such as
142138
prompts.
139+
140+
Values from user input or the command line will be strings, but default
141+
values and Python arguments may already be the correct type. The custom
142+
type should check at the top if the value is already valid and pass it
143+
through to support those cases.

0 commit comments

Comments
 (0)