From ae23f60b51c7c65e91d26cbd90e7235dc6d41dee Mon Sep 17 00:00:00 2001 From: Dan Yeaw Date: Sun, 21 Jan 2024 16:32:09 -0500 Subject: [PATCH 1/2] Fix encoding warnings with PEP 597 enabled --- .github/workflows/tests.yml | 3 + news/398.misc.md | 1 + src/cleo/ui/exception_trace/frame.py | 2 +- src/cleo/ui/question.py | 2 +- .../completion/test_completions_command.py | 14 +++- tests/test_application.py | 74 +++++++++---------- tests/ui/exception_trace/test_frame.py | 2 +- tests/ui/test_question.py | 2 +- 8 files changed, 52 insertions(+), 48 deletions(-) create mode 100644 news/398.misc.md diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 3bb1af86..657351c3 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -5,6 +5,9 @@ on: push: branches: [main] +env: + PYTHONWARNDEFAULTENCODING: 'true' + jobs: Tests: name: ${{ matrix.os }} / ${{ matrix.python-version }} diff --git a/news/398.misc.md b/news/398.misc.md new file mode 100644 index 00000000..36fdec41 --- /dev/null +++ b/news/398.misc.md @@ -0,0 +1 @@ +Fix encoding warnings with PEP 597 enabled diff --git a/src/cleo/ui/exception_trace/frame.py b/src/cleo/ui/exception_trace/frame.py index 610f2cdc..499b39e3 100644 --- a/src/cleo/ui/exception_trace/frame.py +++ b/src/cleo/ui/exception_trace/frame.py @@ -62,7 +62,7 @@ def file_content(self) -> str: return "" if self._filename not in type(self)._content_cache: try: - file_content = Path(self._filename).read_text() + file_content = Path(self._filename).read_text(encoding="utf-8") except OSError: file_content = "" type(self)._content_cache[self._filename] = file_content diff --git a/src/cleo/ui/question.py b/src/cleo/ui/question.py index 11a30e7d..17fc0612 100644 --- a/src/cleo/ui/question.py +++ b/src/cleo/ui/question.py @@ -265,7 +265,7 @@ def _read_from_input(self, io: IO) -> str: return ret.strip() def _has_stty_available(self) -> bool: - with Path(os.devnull).open("w") as devnull: + with Path(os.devnull).open("w", encoding="utf-8") as devnull: try: exit_code = subprocess.call(["stty"], stdout=devnull, stderr=devnull) except Exception: diff --git a/tests/commands/completion/test_completions_command.py b/tests/commands/completion/test_completions_command.py index 190326a2..82c42144 100644 --- a/tests/commands/completion/test_completions_command.py +++ b/tests/commands/completion/test_completions_command.py @@ -48,7 +48,10 @@ def test_bash(mocker: MockerFixture) -> None: tester = CommandTester(command) tester.execute("bash") - with open(os.path.join(os.path.dirname(__file__), "fixtures", "bash.txt")) as f: + with open( + os.path.join(os.path.dirname(__file__), "fixtures", "bash.txt"), + encoding="utf-8", + ) as f: expected = f.read() assert expected == tester.io.fetch_output().replace("\r\n", "\n") @@ -70,7 +73,9 @@ def test_zsh(mocker: MockerFixture) -> None: tester = CommandTester(command) tester.execute("zsh") - with open(os.path.join(os.path.dirname(__file__), "fixtures", "zsh.txt")) as f: + with open( + os.path.join(os.path.dirname(__file__), "fixtures", "zsh.txt"), encoding="utf-8" + ) as f: expected = f.read() assert expected == tester.io.fetch_output().replace("\r\n", "\n") @@ -92,7 +97,10 @@ def test_fish(mocker: MockerFixture) -> None: tester = CommandTester(command) tester.execute("fish") - with open(os.path.join(os.path.dirname(__file__), "fixtures", "fish.txt")) as f: + with open( + os.path.join(os.path.dirname(__file__), "fixtures", "fish.txt"), + encoding="utf-8", + ) as f: expected = f.read() assert expected == tester.io.fetch_output().replace("\r\n", "\n") diff --git a/tests/test_application.py b/tests/test_application.py index 6bb6bb65..00f61627 100644 --- a/tests/test_application.py +++ b/tests/test_application.py @@ -68,7 +68,9 @@ def test_long_version() -> None: def test_help(app: Application) -> None: - assert app.help == FIXTURES_PATH.joinpath("application_help.txt").read_text() + assert app.help == FIXTURES_PATH.joinpath("application_help.txt").read_text( + encoding="utf-8" + ) def test_all(app: Application) -> None: @@ -218,10 +220,9 @@ def test_set_catch_exceptions(app: Application, environ: dict[str, str]) -> None tester.execute("foo", decorated=False) assert tester.io.fetch_output() == "" - assert ( - tester.io.fetch_error() - == FIXTURES_PATH.joinpath("application_exception1.txt").read_text() - ) + assert tester.io.fetch_error() == FIXTURES_PATH.joinpath( + "application_exception1.txt" + ).read_text(encoding="utf-8") app.catch_exceptions(False) @@ -256,42 +257,37 @@ def test_run(app: Application, argv: list[str]) -> None: def test_run_runs_the_list_command_without_arguments(tester: ApplicationTester) -> None: tester.execute("", decorated=False) - assert ( - tester.io.fetch_output() - == FIXTURES_PATH.joinpath("application_run1.txt").read_text() - ) + assert tester.io.fetch_output() == FIXTURES_PATH.joinpath( + "application_run1.txt" + ).read_text(encoding="utf-8") def test_run_runs_help_command_if_required(tester: ApplicationTester) -> None: tester.execute("--help", decorated=False) - assert ( - tester.io.fetch_output() - == FIXTURES_PATH.joinpath("application_run2.txt").read_text() - ) + assert tester.io.fetch_output() == FIXTURES_PATH.joinpath( + "application_run2.txt" + ).read_text(encoding="utf-8") tester.execute("-h", decorated=False) - assert ( - tester.io.fetch_output() - == FIXTURES_PATH.joinpath("application_run2.txt").read_text() - ) + assert tester.io.fetch_output() == FIXTURES_PATH.joinpath( + "application_run2.txt" + ).read_text(encoding="utf-8") def test_run_runs_help_command_with_command(tester: ApplicationTester) -> None: tester.execute("--help list", decorated=False) - assert ( - tester.io.fetch_output() - == FIXTURES_PATH.joinpath("application_run3.txt").read_text() - ) + assert tester.io.fetch_output() == FIXTURES_PATH.joinpath( + "application_run3.txt" + ).read_text(encoding="utf-8") tester.execute("list -h", decorated=False) - assert ( - tester.io.fetch_output() - == FIXTURES_PATH.joinpath("application_run3.txt").read_text() - ) + assert tester.io.fetch_output() == FIXTURES_PATH.joinpath( + "application_run3.txt" + ).read_text(encoding="utf-8") def test_run_removes_all_output_if_quiet(tester: ApplicationTester) -> None: @@ -325,33 +321,29 @@ def test_run_with_verbosity(tester: ApplicationTester) -> None: def test_run_with_version(tester: ApplicationTester) -> None: tester.execute("--version") - assert ( - tester.io.fetch_output() - == FIXTURES_PATH.joinpath("application_run4.txt").read_text() - ) + assert tester.io.fetch_output() == FIXTURES_PATH.joinpath( + "application_run4.txt" + ).read_text(encoding="utf-8") tester.execute("-V") - assert ( - tester.io.fetch_output() - == FIXTURES_PATH.joinpath("application_run4.txt").read_text() - ) + assert tester.io.fetch_output() == FIXTURES_PATH.joinpath( + "application_run4.txt" + ).read_text(encoding="utf-8") def test_run_with_help(tester: ApplicationTester) -> None: tester.execute("help --help") - assert ( - tester.io.fetch_output() - == FIXTURES_PATH.joinpath("application_run5.txt").read_text() - ) + assert tester.io.fetch_output() == FIXTURES_PATH.joinpath( + "application_run5.txt" + ).read_text(encoding="utf-8") tester.execute("-h help") - assert ( - tester.io.fetch_output() - == FIXTURES_PATH.joinpath("application_run5.txt").read_text() - ) + assert tester.io.fetch_output() == FIXTURES_PATH.joinpath( + "application_run5.txt" + ).read_text(encoding="utf-8") def test_run_with_input() -> None: diff --git a/tests/ui/exception_trace/test_frame.py b/tests/ui/exception_trace/test_frame.py index 6932cb30..76a9dbf7 100644 --- a/tests/ui/exception_trace/test_frame.py +++ b/tests/ui/exception_trace/test_frame.py @@ -22,7 +22,7 @@ def test_frame() -> None: assert frame.function == "test_frame" assert frame.line == " simple_exception()\n" - with open(__file__) as f: + with open(__file__, encoding="utf-8") as f: assert f.read() == frame.file_content assert repr(frame) == f"" diff --git a/tests/ui/test_question.py b/tests/ui/test_question.py index d96acd61..450b8e9f 100644 --- a/tests/ui/test_question.py +++ b/tests/ui/test_question.py @@ -15,7 +15,7 @@ def has_tty_available() -> bool: - with open(os.devnull, "w") as devnull: + with open(os.devnull, "w", encoding="utf-8") as devnull: exit_code = subprocess.call(["stty", "2"], stdout=devnull, stderr=devnull) return exit_code == 0 From 56b70ce55b4e11e4e2552bae5bb36be51ed3d14c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20S=C5=82awecki?= Date: Sun, 21 Jan 2024 23:37:42 +0100 Subject: [PATCH 2/2] Unquote boolean value of `env.PYTHONWARNDEFAULTENCODING` --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 657351c3..40e69717 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -6,7 +6,7 @@ on: branches: [main] env: - PYTHONWARNDEFAULTENCODING: 'true' + PYTHONWARNDEFAULTENCODING: true jobs: Tests: