Skip to content

Commit 524825e

Browse files
committed
fix: Fix compatibility with Click 8.2.0+
Closes espressif#1105 Closes espressif#1104
1 parent 0f32306 commit 524825e

File tree

4 files changed

+33
-16
lines changed

4 files changed

+33
-16
lines changed

espefuse/efuse/base_operations.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,22 @@
2323
from .emulate_efuse_controller_base import EmulateEfuseControllerBase
2424

2525

26-
class EfuseValuePairArg(click.Argument):
26+
class EfuseArgument(click.Argument):
27+
def make_metavar(self, ctx: click.Context | None = None) -> str:
28+
"""Compatibility layer for Click 8.2.0+; which now requires a ctx parameter."""
29+
try:
30+
return super().make_metavar(ctx) # type: ignore
31+
except TypeError:
32+
# Fall back to the old signature (pre-Click 8.2.0)
33+
return super().make_metavar() # type: ignore
34+
35+
36+
class EfuseValuePairArg(EfuseArgument):
2737
def __init__(self, *args, **kwargs):
2838
super().__init__(*args, **kwargs)
2939

30-
def make_metavar(self) -> str:
31-
return f"[{super().make_metavar()}] ..."
40+
def make_metavar(self, ctx=None) -> str:
41+
return f"[{super().make_metavar(ctx)}] ..."
3242

3343
def type_cast_value(self, ctx: click.Context, value: list[str]):
3444
return self.type.convert(value, None, ctx)
@@ -84,17 +94,17 @@ def convert(self, value: str, param: click.Parameter | None, ctx: click.Context)
8494
return base_fields.CheckArgValue(ctx.obj["efuses"], "CUSTOM_MAC")(value)
8595

8696

87-
class TupleParameter(click.Argument):
97+
class TupleParameter(EfuseArgument):
8898
def __init__(self, *args, **kwargs):
8999
self.max_arity = kwargs.pop("max_arity", None)
90100
super().__init__(*args, **kwargs)
91101

92-
def make_metavar(self) -> str:
102+
def make_metavar(self, ctx=None) -> str:
93103
if self.nargs == 1:
94-
return super().make_metavar() # type: ignore
104+
return super().make_metavar(ctx) # type: ignore
95105
if self.max_arity is None:
96-
return f"[{super().make_metavar()}] ..."
97-
return f"[{super().make_metavar()}] ... (max {self.max_arity} groups)"
106+
return f"[{super().make_metavar(ctx)}] ..."
107+
return f"[{super().make_metavar(ctx)}] ... (max {self.max_arity} groups)"
98108

99109
def type_cast_value(self, ctx: click.Context, value: list[str]) -> tuple[Any, ...]:
100110
# This is by default eating all options, so we need to check for help option

esptool/__init__.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -930,11 +930,6 @@ def erase_flash_cli(ctx, force, **kwargs):
930930
)
931931
@click.argument("address", type=AnyIntType())
932932
@click.argument("size", type=AutoSizeType())
933-
@click.option(
934-
"--force",
935-
is_flag=True,
936-
help="Erase region even if security features are enabled. Use with caution!",
937-
)
938933
@add_spi_connection_arg
939934
@click.pass_context
940935
def erase_region_cli(ctx, address, size, force, **kwargs):

esptool/cli_util.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ class AddrFilenamePairType(click.Path):
155155

156156
name = "addr-filename-pair"
157157

158-
def get_metavar(self, param):
158+
def get_metavar(
159+
self, param: click.Parameter | None, ctx: click.Context | None = None
160+
):
159161
return "<address> <filename>"
160162

161163
def convert(
@@ -294,7 +296,17 @@ def __init__(self, *args, **kwargs):
294296
self._eat_all_parser = None
295297
# Set the metavar dynamically based on the type's metavar
296298
if self.type and hasattr(self.type, "name"):
297-
self.metavar = f"[{self.type.get_metavar(None) or self.type.name.upper()}]"
299+
self.metavar = f"[{self._get_metavar() or self.type.name.upper()}]"
300+
301+
def _get_metavar(self):
302+
"""Get the metavar for the option. Wrapper for compatibility reasons.
303+
In Click 8.2.0+, the `get_metavar` requires new parameter `ctx`.
304+
"""
305+
try:
306+
ctx = click.get_current_context(silent=True)
307+
return self.type.get_metavar(None, ctx)
308+
except TypeError:
309+
return self.type.get_metavar(None)
298310

299311
def add_to_parser(self, parser, ctx):
300312
def parser_process(value, state):

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"PyYAML>=5.1",
3939
"intelhex",
4040
"rich_click",
41-
"click<8.2.0",
41+
"click<9",
4242
]
4343

4444
[project.urls]

0 commit comments

Comments
 (0)