Skip to content

Fix failed pyproject.toml read when Unicode characters present#1422

Merged
nathanjmcdougall merged 2 commits into
mainfrom
copilot/fix-pyproject-toml-issue
Mar 19, 2026
Merged

Fix failed pyproject.toml read when Unicode characters present#1422
nathanjmcdougall merged 2 commits into
mainfrom
copilot/fix-pyproject-toml-issue

Conversation

Copilot AI commented Mar 19, 2026

Copy link
Copy Markdown
Contributor

File I/O throughout the codebase omitted encoding="utf-8", falling back to the system default. On platforms where the default is not UTF-8 (e.g. Windows), reading a file containing non-ASCII characters like ā would raise a UnicodeDecodeError.

Changes

  • _io.py (UsethisFileManager): Add encoding="utf-8" to read_text() and write_text() — this is the base class for all file managers including PyprojectTOMLManager, so it covers the primary reported failure
  • _file/yaml/io_.py: Add encoding="utf-8" to open() for YAML file reads
  • _init.py: Add encoding="utf-8" to write_text() for pyproject.toml and __init__.py creation
  • _integrations/pytest/core.py: Add encoding="utf-8" to write_text() for conftest.py
  • _integrations/mkdocs/core.py: Add encoding="utf-8" to write_text() for docs/index.md
  • _integrations/sonarqube/config.py: Add encoding="utf-8" to read_text() for .python-version
  • Test: Added test_read_file_with_unicode_chars to test_pyproject_toml_io_.py asserting a pyproject.toml containing ā in a comment is parsed successfully
Original prompt

This section details on the original issue you should resolve

<issue_title>Failed pyproject.toml read with unicode char present</issue_title>
<issue_description>The character ā in a comment meant that usethis failed to read pyproject.toml when running the command usethis tool import-linter

Details
uvx usethis tool import-linter
Installed 26 packages in 889ms
╭──────────────────────────────────────────────────────── Traceback (most recent call last) ────────────────────────────────────────────────────────╮
│ C:\Users\namc\AppData\Local\uv\cache\archive-v0\-9VPuSvSg0z_h-I2P96_m\Lib\site-packages\usethis\_ui\interface\tool.py:136 in import_linter        │
│                                                                                                                                                   │
│   133 │   │   ),                                                                                                                                  │
│   134 │   │   files_manager(),                                                                                                                    │
│   135 │   ):                                                                                                                                      │
│ ❱ 136 │   │   _run_tool(use_import_linter, remove=remove, how=how)                                                                                │
│   137                                                                                                                                             │
│   138                                                                                                                                             │
│   139 @app.command(                                                                                                                               │
│                                                                                                                                                   │
│ C:\Users\namc\AppData\Local\uv\cache\archive-v0\-9VPuSvSg0z_h-I2P96_m\Lib\site-packages\usethis\_ui\interface\tool.py:339 in _run_tool            │
│                                                                                                                                                   │
│   336 │   from usethis.errors import UsethisError                                                                                                 │
│   337 │                                                                                                                                           │
│   338 │   try:                                                                                                                                    │
│ ❱ 339 │   │   caller(remove=remove, how=how, **kwargs)                                                                                            │
│   340 │   except UsethisError as err:                                                                                                             │
│   341 │   │   err_print(err)                                                                                                                      │
│   342 │   │   raise typer.Exit(code=1) from None                                                                                                  │
│                                                                                                                                                   │
│ C:\Users\namc\AppData\Local\uv\cache\archive-v0\-9VPuSvSg0z_h-I2P96_m\Lib\site-packages\usethis\_core\tool.py:140 in use_import_linter            │
│                                                                                                                                                   │
│   137 │   rule_config = tool.get_rule_config()                                                                                                    │
│   138 │                                                                                                                                           │
│   139 │   if not remove:                                                                                                                          │
│ ❱ 140 │   │   ensure_dep_declaration_file()                                                                                                       │
│   141 │   │                                                                                                                                       │
│   142 │   │   tool.add_dev_deps()                                                                                                                 │
│   143 │   │   tool.add_configs()                                                                                                                  │
│                                                                                                           ...

</details>



<!-- START COPILOT CODING AGENT SUFFIX -->

- Fixes usethis-python/usethis-python#1421

<!-- START COPILOT CODING AGENT TIPS -->
---

📱 Kick off Copilot coding agent tasks wherever you are with [GitHub Mobile](https://gh.io/cca-mobile-docs), available on iOS and Android.

Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix failed pyproject.toml read with unicode char present Fix failed pyproject.toml read when Unicode characters present Mar 19, 2026
Copilot AI requested a review from nathanjmcdougall March 19, 2026 01:42
@codspeed-hq

codspeed-hq Bot commented Mar 19, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 2 untouched benchmarks


Comparing copilot/fix-pyproject-toml-issue (5fc937a) with main (889eb18)

Open in CodSpeed

@nathanjmcdougall nathanjmcdougall marked this pull request as ready for review March 19, 2026 02:28
@nathanjmcdougall nathanjmcdougall merged commit c15d458 into main Mar 19, 2026
19 checks passed
@nathanjmcdougall nathanjmcdougall deleted the copilot/fix-pyproject-toml-issue branch March 19, 2026 02:29
@codecov

codecov Bot commented Mar 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

@nathanjmcdougall nathanjmcdougall linked an issue Mar 19, 2026 that may be closed by this pull request
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.

Failed pyproject.toml read with unicode char present

2 participants