Skip to content
Myr

more compat docs, use markdown

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

authored by David Lord · committed by David Lord · parent 341207b · Browse files · 2 files+62 added−59 removed
This commit

State

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

Diff stats

2 files+62 added−59 removed

  • other:+62 added−59 removed

2 files+62 added−59 removed

added:docs/support-multiple-versions.md+62 added
deleted:docs/support-multiple-versions.rst-59 removed
1Supporting Multiple Versions2============================34Supporting multiple versions of ``click`` in a single codebase is usually5straightforward. Most features of ``click`` are stable across releases, and don't6require special handling.78However, feature releases may deprecate and change APIs. Some features require special9handling to function correctly across a range of versions.1011click 8.1.x and 8.2.x12---------------------1314This section will help you support click version 8.1 and 8.2 simultaneously.151617``ParamType`` methods require ``ctx``18~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~1920In 8.2, several methods of ``ParamType`` now accept a ``ctx: click.Context`` argument.21By way of example, this section covers ``ParamType.get_metavar``.2223To handle this while supporting 8.1, you can define a decorator:2425.. code-block:: python2627 import functools28 import typing as t2930 C = t.TypeVar("C", bound=t.Callable[..., t.Any])3132 def shim_get_metavar(f: C) -> C:33 @functools.wraps(f)34 def wrapper(*args: t.Any, **kwargs: t.Any) -> t.Any:35 if "ctx" not in kwargs:36 kwargs["ctx"] = click.get_current_context(silent=True)37 return f(*args, **kwargs)3839 return wrapper # type: ignore[return-value]4041And then use the decorator on your ``ParamType`` to write a compatible ``get_metavar``42method.43For example:4445.. code-block:: python4647 import click4849 class CommaDelimitedString(click.ParamType):50 @shim_get_metavar51 def get_metavar(self, param: click.Parameter, ctx: click.Context | None) -> str:52 return "TEXT,TEXT,..."535455.. note::5657 This methodology, creating a wrapper which does feature detection based on ``"ctx"``58 being in ``kwargs``, works for several other methods, e.g.59 ``ParamType.get_missing_message``.