Skip to content

Commit 74ce708

Browse files
committed
Add click compatibility for 8.1.x vs 8.2.x
`get_metavar` changed in 8.2.0 and has a workaround covered in `click` docs, which is now applied here.
1 parent 2f3de0a commit 74ce708

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

piptools/_internal/_cli/_param_types.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,40 @@
11
from __future__ import annotations
22

3+
import functools
4+
import typing as _t
5+
36
import click
47

58
from . import _parsed_param_types
69

10+
F = _t.TypeVar("F", bound=_t.Callable[..., _t.Any])
11+
12+
13+
def _add_ctx_arg(f: F) -> F:
14+
"""
15+
Make ``ParamType`` methods compatible with various click versions, as documented
16+
in the click docs here:
17+
https://click.palletsprojects.com/en/stable/support-multiple-versions/
18+
"""
19+
20+
@functools.wraps(f)
21+
def wrapper(*args: _t.Any, **kwargs: _t.Any) -> _t.Any:
22+
# NOTE: this check is skipped in coverage as it requires a lower `click` version
23+
# NOTE: once we have a coverage plugin for library version pragmas, we can make
24+
# NOTE: this do the appropriate dispatch
25+
if "ctx" not in kwargs: # pragma: no cover
26+
kwargs["ctx"] = click.get_current_context(silent=True)
27+
28+
return f(*args, **kwargs)
29+
30+
return wrapper # type: ignore[return-value]
31+
732

833
class DependencyGroupParamType(click.ParamType):
9-
def get_metavar(self, param: click.Parameter) -> str:
34+
@_add_ctx_arg
35+
def get_metavar( # type: ignore[override]
36+
self, param: click.Parameter, ctx: click.Context
37+
) -> str:
1038
return "[pyproject-path:]groupname"
1139

1240
def convert(

0 commit comments

Comments
 (0)