Skip to content

fix(cmd-version): read & write toml and pattern version files as UTF-8 - #1518

Open
stella-h-choi wants to merge 2 commits into
python-semantic-release:masterfrom
stella-h-choi:fix/utf8-version-files
Open

stella-h-choi wants to merge 2 commits into
python-semantic-release:masterfrom
stella-h-choi:fix/utf8-version-files

Conversation

@stella-h-choi

Copy link
Copy Markdown

Purpose

semantic-release version fails where the locale's encoding is not UTF-8 (Windows with cp1252 or cp949) and pyproject.toml or a stamped source file holds non-ASCII text, such as an author's name: the files are read and written with Path.read_text() / write_text() without an encoding.

Rationale

TOML is UTF-8 by its specification, and Python source is UTF-8 by default, so these files are now read and written as UTF-8:

  • TomlVersionDeclaration and PatternVersionDeclaration, when a version is stamped
  • the pyproject.toml read for the project metadata in RuntimeContext.from_raw_config

FileVersionDeclaration (the file is only the version) and the deprecated VersionDeclarationABC are left as they are. #1505 does the same for the changelog and does not touch these files.

A file matched by version_variables that is saved in a legacy code page with non-ASCII text was stamped on a Windows of that code page before and is refused as not UTF-8 now. It was already refused where the locale is UTF-8.

How did you test?

A unit test for each of the two declarations stamps a file with non-ASCII text and compares it byte-decoded with the original. Linux, Python 3.14, tests/unit/semantic_release/version and tests/unit/semantic_release/cli/test_config.py (474 tests):

locale master with the new tests this branch
C.UTF-8 1 failed the same 1 failed
ko_KR.EUC-KR 3 failed (that one and the two new tests) the same 1 failed

The one that fails everywhere here, test_toml_declaration_from_definition[Default number format for project.version], is about the quote style and fails on master without this change.

All unit tests (pytest -m unit, C.UTF-8): 4309 tests with 1 failure on master, 4311 with the same 1 failure on this branch.

The command itself, in a new repository whose pyproject.toml and m.py hold Korean text (version --no-commit --no-tag --no-push --no-vcs-release --no-changelog --skip-build):

locale master this branch
C.UTF-8 exit 0, only the version changed exit 0, only the version changed
ko_KR.EUC-KR exit 1 exit 0, only the version changed

On a UTF-8 host the new unit tests pass without the fix as well; they guard it on a host with another locale, such as Windows. The suite was not run on Windows, and the end-to-end tests were not run.

How to Verify

  1. Check out only the test commit and run the two new tests with a non-UTF-8 locale, for example LC_ALL=ko_KR.EUC-KR PYTHONUTF8=0 pytest tests/unit/semantic_release/version/declarations -k utf8: both fail with UnicodeDecodeError.
  2. Check out the branch and run them again: both pass.

PR Completion Checklist

  • Reviewed & followed the Contributor Guidelines

  • Changes Implemented & Validation pipeline succeeds

  • Commits follow the Conventional Commits standard
    and are separated into the proper commit type and scope (recommended order: test, build, feat/fix, docs)

  • Appropriate Unit tests added/updated

  • Appropriate End-to-End tests added/updated

  • Appropriate Documentation added/updated and syntax validated for sphinx build (see Contributor Guidelines)

🤖 Generated with Claude Code

stella-h-choi and others added 2 commits December 1, 2026 15:36
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
They were read and written in the locale's encoding, so stamping a file
with non-ASCII text failed on a Windows with a legacy code page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant