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
Provenance
- Commit
55189e3- Tree
2868347- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
341207b
2 files changed+62 added−59 removed
added:docs/support-multiple-versions.md+62 added
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162# Supporting Multiple Versions
If you are a library maintainer, you may want to support multiple versions ofClick. See the Pallets [version policy] for information about our versionnumbers and support policy.
[version policy]: https://palletsprojects.com/versions
Most features of Click are stable across releases, and don't require specialhandling. However, feature releases may deprecate and change APIs. Occasionally,a change will require special handling.
## Use Feature Detection
Prefer using feature detection. Looking at the version can be tempting, but isoften more brittle or results in more complicated code. Try to use `if` or `try`blocks to decide whether to use a new or old pattern.
If you do need to look at the version, use {func}`importlib.metadata.version`,the standardized way to get versions for any installed Python package.
## Changes in 8.2
### `ParamType` methods require `ctx`
In 8.2, several methods of `ParamType` now have a `ctx: click.Context`argument. Because this changes the signature of the methods from 8.1, it's notobvious how to support both when subclassing or calling.
This example uses `ParamType.get_metavar`, and the same technique should beapplicable to other methods such as `get_missing_message`.
Update your methods overrides to take the new `ctx` argument. Use thefollowing decorator to wrap each method. In 8.1, it will get the context wherepossible and pass it using the 8.2 signature.
```pythonimport functoolsimport typing as timport click
F = t.TypeVar("F", bound=t.Callable[..., t.Any])
def add_ctx_arg(f: F) -> F: @functools.wraps(f) def wrapper(*args: t.Any, **kwargs: t.Any) -> t.Any: if "ctx" not in kwargs: kwargs["ctx"] = click.get_current_context(silent=True)
return f(*args, **kwargs)
return wrapper # type: ignore[return-value]```
Here's an example ``ParamType`` subclass which uses this:
```pythonclass CommaDelimitedString(click.ParamType): @add_ctx_arg def get_metavar(self, param: click.Parameter, ctx: click.Context | None) -> str: return "TEXT,TEXT,..."```deleted:docs/support-multiple-versions.rst-59 removed
1-Supporting Multiple Versions2-============================3-4-Supporting multiple versions of ``click`` in a single codebase is usually5-straightforward. Most features of ``click`` are stable across releases, and don't6-require special handling.7-8-However, feature releases may deprecate and change APIs. Some features require special9-handling to function correctly across a range of versions.10-11-click 8.1.x and 8.2.x12----------------------13-14-This section will help you support click version 8.1 and 8.2 simultaneously.15-16-17-``ParamType`` methods require ``ctx``18-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~19-20-In 8.2, several methods of ``ParamType`` now accept a ``ctx: click.Context`` argument.21-By way of example, this section covers ``ParamType.get_metavar``.22-23-To handle this while supporting 8.1, you can define a decorator:24-25-.. code-block:: python26-27- import functools28- import typing as t29-30- C = t.TypeVar("C", bound=t.Callable[..., t.Any])31-32- 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)38-39- return wrapper # type: ignore[return-value]40-41-And then use the decorator on your ``ParamType`` to write a compatible ``get_metavar``42-method.43-For example:44-45-.. code-block:: python46-47- import click48-49- class CommaDelimitedString(click.ParamType):50- @shim_get_metavar51- def get_metavar(self, param: click.Parameter, ctx: click.Context | None) -> str:52- return "TEXT,TEXT,..."53-54-55-.. note::56-57- 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``.