From 56eb0441da1ae85399fdbc5597cf181eeb408223 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Tue, 13 Jun 2017 10:26:03 +1200 Subject: [PATCH] commands: teach parser correct annotations for variable args We should annotate with the base type, not the resulting sequence. --- mitmproxy/addons/core.py | 6 +++--- mitmproxy/command.py | 8 ++++++-- mitmproxy/tools/console/master.py | 6 +++--- test/mitmproxy/test_command.py | 2 +- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/mitmproxy/addons/core.py b/mitmproxy/addons/core.py index d75949bc1..b0d8ee275 100644 --- a/mitmproxy/addons/core.py +++ b/mitmproxy/addons/core.py @@ -10,7 +10,7 @@ from mitmproxy.net.http import status_codes class Core: @command.command("set") - def set(self, *spec: typing.Sequence[str]) -> None: + def set(self, *spec: str) -> None: """ Set an option of the form "key[=value]". When the value is omitted, booleans are set to true, strings and integers are set to None (if @@ -18,9 +18,9 @@ class Core: false or toggle. If multiple specs are passed, they are joined into one separated by spaces. """ - spec = " ".join(spec) + strspec = " ".join(spec) try: - ctx.options.set(spec) + ctx.options.set(strspec) except exceptions.OptionsError as e: raise exceptions.CommandError(e) from e diff --git a/mitmproxy/command.py b/mitmproxy/command.py index 82b8fae41..2256e4ca5 100644 --- a/mitmproxy/command.py +++ b/mitmproxy/command.py @@ -59,7 +59,7 @@ class Command: def paramnames(self) -> typing.Sequence[str]: v = [typename(i, False) for i in self.paramtypes] if self.has_positional: - v[-1] = "*" + v[-1][1:-1] + v[-1] = "*" + v[-1] return v def retname(self) -> str: @@ -92,7 +92,11 @@ class Command: pargs.append(parsearg(self.manager, args[i], self.paramtypes[i])) if remainder: - if typecheck.check_command_type(remainder, self.paramtypes[-1]): + chk = typecheck.check_command_type( + remainder, + typing.Sequence[self.paramtypes[-1]] # type: ignore + ) + if chk: pargs.extend(remainder) else: raise exceptions.CommandError("Invalid value type.") diff --git a/mitmproxy/tools/console/master.py b/mitmproxy/tools/console/master.py index 92e8b7a2c..64e1a4443 100644 --- a/mitmproxy/tools/console/master.py +++ b/mitmproxy/tools/console/master.py @@ -189,7 +189,7 @@ class ConsoleAddon: @command.command("console.choose") def console_choose( - self, prompt: str, choices: typing.Sequence[str], *cmd: typing.Sequence[str] + self, prompt: str, choices: typing.Sequence[str], *cmd: str ) -> None: """ Prompt the user to choose from a specified list of strings, then @@ -211,7 +211,7 @@ class ConsoleAddon: @command.command("console.choose.cmd") def console_choose_cmd( - self, prompt: str, choicecmd: str, *cmd: typing.Sequence[str] + self, prompt: str, choicecmd: str, *cmd: str ) -> None: """ Prompt the user to choose from a list of strings returned by a @@ -234,7 +234,7 @@ class ConsoleAddon: ) @command.command("console.command") - def console_command(self, *partial: typing.Sequence[str]) -> None: + def console_command(self, *partial: str) -> None: """ Prompt the user to edit a command with a (possilby empty) starting value. """ diff --git a/test/mitmproxy/test_command.py b/test/mitmproxy/test_command.py index 958328b29..87432163a 100644 --- a/test/mitmproxy/test_command.py +++ b/test/mitmproxy/test_command.py @@ -22,7 +22,7 @@ class TAddon: def empty(self) -> None: pass - def varargs(self, one: str, *var: typing.Sequence[str]) -> typing.Sequence[str]: + def varargs(self, one: str, *var: str) -> typing.Sequence[str]: return list(var)