Skip to content
Myr

Merge pull request #1647 from pallets/doc-paramtype-requirements

Before adoption: this commit came in with the history the project adopted, so no landing records it.

update API docs about custom ParamType requirements

authored by David Lord · committed by GitHub · parents fcbaee6, 783d63f · Browse files · 1 file+32 added−12 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+32 added−12 removed

  • other:+32 added−12 removed

1 file+32 added−12 removed

modified:src/click/types.py+32 added-12 removed
11 unmodified lines
12from .utils import safecall131415class ParamType:16 """Helper for converting values through types. The following is17 necessary for a valid type:16 """Represents the type of a parameter. Validates and converts values17 from the command line or Python into the correct type.1819 * it needs a name20 * it needs to pass through None unchanged21 * it needs to convert from a string22 * it needs to convert its result type through unchanged23 (eg: needs to be idempotent)24 * it needs to be able to deal with param and context being `None`.25 This can be the case when the object is used with prompt26 inputs.19 To implement a custom type, subclass and implement at least the20 following:2122 - The :attr:`name` class attribute must be set.23 - Calling an instance of the type with ``None`` must return24 ``None``. This is already implemented by default.25 - :meth:`convert` must convert string values to the correct type.26 - :meth:`convert` must accept values that are already the correct27 type.28 - It must be able to convert a value if the ``ctx`` and ``param``29 arguments are ``None``. This can occur when converting prompt30 input.31 """3233 is_composite = False34
22 unmodified lines
57 .. versionadded:: 2.058 """5960 def convert(self, value, param, ctx):57 """Converts the value. This is not invoked for values that are58 `None` (the missing value).61 """Convert the value to the correct type. This is not called if62 the value is ``None`` (the missing value).6364 This must accept string values from the command line, as well as65 values that are already the correct type. It may also convert66 other compatible types.6768 The ``param`` and ``ctx`` arguments may be ``None`` in certain69 situations, such as when converting prompt input.7071 If the value cannot be converted, call :meth:`fail` with a72 descriptive message.7374 :param value: The value to convert.75 :param param: The parameter that is using this type to convert76 its value. May be ``None``.77 :param ctx: The current context that arrived at this value. May78 be ``None``.79 """80 return value8182 def split_envvar_value(self, rv):
685 unmodified lines