Skip to content

Commit 1bb0212

Browse files
authored
fix: ensure utf-8 encoding if something else is default and unset (#236)
* tests: check for default encoding issues Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com> * Update ci.yml * fix: open console in utf-8 Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com> --------- Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
1 parent 4b16649 commit 1bb0212

File tree

5 files changed

+21
-5
lines changed

5 files changed

+21
-5
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ jobs:
4949
3.10
5050
3.11
5151
3.12
52+
3.13
53+
allow-prereleases: true
5254

5355
- name: Setup uv
5456
uses: yezz123/setup-uv@v4
@@ -58,8 +60,6 @@ jobs:
5860

5961
- name: Test package (all Pythons)
6062
run: hatch test -a
61-
env:
62-
PYTHONUTF8: "1"
6363

6464
dist:
6565
name: Distribution build

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ classifiers = [
2323
"Programming Language :: Python :: 3.10",
2424
"Programming Language :: Python :: 3.11",
2525
"Programming Language :: Python :: 3.12",
26+
"Programming Language :: Python :: 3.13",
2627
"Programming Language :: Python",
2728
"Topic :: Software Development :: Quality Assurance",
2829
"Topic :: Software Development :: Libraries :: Python Modules",
@@ -89,6 +90,8 @@ installer = "uv"
8990

9091
[tool.hatch.envs.hatch-test]
9192
features = ["test", "cli"]
93+
env-vars.PYTHONWARNDEFAULTENCODING = "1"
94+
9295

9396
[tool.hatch.envs.lint]
9497
dependencies = ["pre-commit"]
@@ -122,7 +125,7 @@ skip-install = true
122125
scripts.serve = "cd docs && echo 'Serving on http://localhost:8080' && python -m http.server 8080"
123126

124127
[[tool.hatch.envs.hatch-test.matrix]]
125-
python = ["3.12", "3.11", "3.10"]
128+
python = ["3.13", "3.12", "3.11", "3.10"]
126129

127130

128131
[tool.pytest.ini_options]

src/repo_review/__main__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import itertools
44
import json
5+
import os
56
import sys
67
import typing
78
from collections.abc import Mapping, Sequence
@@ -83,6 +84,17 @@ def rich_printer(
8384
status: Status,
8485
header: str = "",
8586
) -> None:
87+
# Before Python 3.15, this isn't always unicode
88+
if (
89+
sys.version_info < (3, 15)
90+
and "PYTHONIOENCODING" not in os.environ
91+
and "PYTHONUTF8" not in os.environ
92+
):
93+
if sys.stdout.encoding != "utf-8":
94+
sys.stdout.reconfigure(encoding="utf-8") # type: ignore[union-attr]
95+
if sys.stderr.encoding != "utf-8":
96+
sys.stderr.reconfigure(encoding="utf-8") # type: ignore[union-attr]
97+
8698
console = rich.console.Console(
8799
record=svg, quiet=svg, stderr=stderr, color_system="auto" if color else None
88100
)

tests/test_multi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def multiple_packages(tmp_path: Path) -> tuple[str, str]:
2828
"""
2929
)
3030

31-
package.joinpath("pyproject.toml").write_text(basic_toml)
31+
package.joinpath("pyproject.toml").write_text(basic_toml, encoding="utf-8")
3232
package.joinpath(".pre-commit-config.yaml").touch()
3333
package.joinpath("README.md").touch()
3434

tests/test_package.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ def test_broken_validate_pyproject(tmp_path: Path) -> None:
3636
[tool.repo-review]
3737
ignore = ["a2"]
3838
"""
39-
)
39+
),
40+
encoding="utf-8",
4041
)
4142

4243
results = process(tmp_path)

0 commit comments

Comments
 (0)