Skip to content

Commit

Permalink
fix: ensure utf-8 encoding if something else is default and unset (#236)
Browse files Browse the repository at this point in the history
* tests: check for default encoding issues

Signed-off-by: Henry Schreiner <[email protected]>

* Update ci.yml

* fix: open console in utf-8

Signed-off-by: Henry Schreiner <[email protected]>

---------

Signed-off-by: Henry Schreiner <[email protected]>
  • Loading branch information
henryiii authored Aug 19, 2024
1 parent 4b16649 commit 1bb0212
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ jobs:
3.10
3.11
3.12
3.13
allow-prereleases: true

- name: Setup uv
uses: yezz123/setup-uv@v4
Expand All @@ -58,8 +60,6 @@ jobs:

- name: Test package (all Pythons)
run: hatch test -a
env:
PYTHONUTF8: "1"

dist:
name: Distribution build
Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ classifiers = [
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Programming Language :: Python",
"Topic :: Software Development :: Quality Assurance",
"Topic :: Software Development :: Libraries :: Python Modules",
Expand Down Expand Up @@ -89,6 +90,8 @@ installer = "uv"

[tool.hatch.envs.hatch-test]
features = ["test", "cli"]
env-vars.PYTHONWARNDEFAULTENCODING = "1"


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

[[tool.hatch.envs.hatch-test.matrix]]
python = ["3.12", "3.11", "3.10"]
python = ["3.13", "3.12", "3.11", "3.10"]


[tool.pytest.ini_options]
Expand Down
12 changes: 12 additions & 0 deletions src/repo_review/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import itertools
import json
import os
import sys
import typing
from collections.abc import Mapping, Sequence
Expand Down Expand Up @@ -83,6 +84,17 @@ def rich_printer(
status: Status,
header: str = "",
) -> None:
# Before Python 3.15, this isn't always unicode
if (
sys.version_info < (3, 15)
and "PYTHONIOENCODING" not in os.environ
and "PYTHONUTF8" not in os.environ
):
if sys.stdout.encoding != "utf-8":
sys.stdout.reconfigure(encoding="utf-8") # type: ignore[union-attr]
if sys.stderr.encoding != "utf-8":
sys.stderr.reconfigure(encoding="utf-8") # type: ignore[union-attr]

console = rich.console.Console(
record=svg, quiet=svg, stderr=stderr, color_system="auto" if color else None
)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def multiple_packages(tmp_path: Path) -> tuple[str, str]:
"""
)

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

Expand Down
3 changes: 2 additions & 1 deletion tests/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def test_broken_validate_pyproject(tmp_path: Path) -> None:
[tool.repo-review]
ignore = ["a2"]
"""
)
),
encoding="utf-8",
)

results = process(tmp_path)
Expand Down

0 comments on commit 1bb0212

Please sign in to comment.