From ac9160ae5407c4450e946e561e98bce5291e8adf Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Thu, 19 Oct 2023 20:18:02 -0700 Subject: [PATCH 1/2] aider: Add --encoding argument and pass it to `InputOutput`. # Aider chat conversation: USER: add --encoding and pass it to `InputOutput` ASSISTANT: To add the `--encoding` argument and pass it to `InputOutput`, we need to modify the `aider/main.py` file. Here are the steps: 1. Add a new argument `--encoding` to the `argparse.ArgumentParser` in the `main` function. 2. Pass the `args.encoding` to the `InputOutput` class instantiation. Here are the changes: ```python aider/main.py <<<<<<< HEAD ########## other_group = parser.add_argument_group("Other Settings") other_group.add_argument( "--version", action="version", version=f"%(prog)s {__version__}", help="Show the version number and exit", ) other_group.add_argument( "--apply", metavar="FILE", help="Apply the changes from the given file instead of running the chat (debug)", ) other_group.add_argument( "--yes", action="store_true", help="Always say yes to every confirmation", default=None, ) other_group.add_argument( "-v", "--verbose", action="store_true", help="Enable verbose output", default=False, ) other_group.add_argument( "--show-repo-map", action="store_true", help="Print the repo map and exit (debug)", default=False, ) other_group.add_argument( "--message", "--msg", "-m", metavar="COMMAND", help="Specify a single message to send GPT, process reply then exit (disables chat mode)", ) other_group.add_argument( "-c", "--config", is_config_file=True, metavar="CONFIG_FILE", help=( "Specify the config file (default: search for .aider.conf.yml in git root, cwd" " or home directory)" ), ) args = parser.parse_args(argv) ======= ########## other_group = parser.add_argument_group("Other Settings") other_group.add_argument( "--version", action="version", version=f"%(prog)s {__version__}", help="Show the version number and exit", ) other_group.add_argument( "--apply", metavar="FILE", help="Apply the changes from the given file instead of running the chat (debug)", ) other_group.add_argument( "--yes", action="store_true", help="Always say yes to every confirmation", default=None, ) other_group.add_argument( "-v", "--verbose", action="store_true", help="Enable verbose output", default=False, ) other_group.add_argument( "--show-repo-map", action="store_true", help="Print the repo map and exit (debug)", default=False, ) other_group.add_argument( "--message", "--msg", "-m", metavar="COMMAND", help="Specify a single message to send GPT, process reply then exit (disables chat mode)", ) other_group.add_argument( "--encoding", default="utf-8", help="Specify the encoding for input and output (default: utf-8)", ) other_group.add_argument( "-c", "--config", is_config_file=True, metavar="CONFIG_FILE", help=( "Specify the config file (default: search for .aider.conf.yml in git root, cwd" " or home directory)" ), ) args = parser.parse_args(argv) >>>>>>> updated ``` ```python aider/main.py <<<<<<< HEAD io = InputOutput( args.pretty, args.yes, args.input_history_file, args.chat_history_file, input=input, output=output, user_input_color=args.user_input_color, tool_output_color=args.tool_output_color, tool_error_color=args.tool_error_color, dry_run=args.dry_run, ) ======= io = InputOutput( args.pretty, args.yes, args.input_history_file, args.chat_history_file, input=input, output=output, user_input_color=args.user_input_color, tool_output_color=args.tool_output_color, tool_error_color=args.tool_error_color, dry_run=args.dry_run, encoding=args.encoding, ) >>>>>>> updated ``` Please note that the `InputOutput` class should be able to handle the `encoding` argument. If it's not the case, we need to modify the `InputOutput` class as well. --- aider/main.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/aider/main.py b/aider/main.py index 9b3b2597b..b0ebf5075 100644 --- a/aider/main.py +++ b/aider/main.py @@ -378,6 +378,11 @@ def main(argv=None, input=None, output=None, force_git_root=None): metavar="COMMAND", help="Specify a single message to send GPT, process reply then exit (disables chat mode)", ) + other_group.add_argument( + "--encoding", + default="utf-8", + help="Specify the encoding for input and output (default: utf-8)", + ) other_group.add_argument( "-c", "--config", @@ -414,6 +419,7 @@ def main(argv=None, input=None, output=None, force_git_root=None): tool_output_color=args.tool_output_color, tool_error_color=args.tool_error_color, dry_run=args.dry_run, + encoding=args.encoding, ) fnames = [str(Path(fn).resolve()) for fn in args.files] From 6791bc19c62c8b6d401e7645bc6e8a4a06f75c9c Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Fri, 20 Oct 2023 08:04:02 -0700 Subject: [PATCH 2/2] added test for --encoding --- tests/test_main.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/tests/test_main.py b/tests/test_main.py index 2abc23da1..214679843 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -4,7 +4,7 @@ import subprocess import tempfile from pathlib import Path from unittest import TestCase -from unittest.mock import patch +from unittest.mock import MagicMock, patch import git from prompt_toolkit.input import DummyInput @@ -13,7 +13,7 @@ from prompt_toolkit.output import DummyOutput from aider.dump import dump # noqa: F401 from aider.io import InputOutput from aider.main import check_gitignore, main, setup_git -from tests.utils import make_repo +from tests.utils import GitTemporaryDirectory, make_repo class TestMain(TestCase): @@ -178,3 +178,18 @@ class TestMain(TestCase): main(["--dirty-commits"], input=DummyInput()) _, kwargs = MockCoder.call_args assert kwargs["dirty_commits"] is True + + def test_encodings_arg(self): + fname = "foo.py" + + with GitTemporaryDirectory(): + with patch("aider.main.Coder.create") as MockCoder: # noqa: F841 + with patch("aider.main.InputOutput") as MockSend: + + def side_effect(*args, **kwargs): + self.assertEqual(kwargs["encoding"], "iso-8859-15") + return MagicMock() + + MockSend.side_effect = side_effect + + main(["--yes", fname, "--encoding", "iso-8859-15"])