Skip to content

check-yaml: report invalid UTF-8 instead of raising a traceback - #1275

Closed
0xacee wants to merge 1 commit into
pre-commit:mainfrom
0xacee:fix/check-yaml-utf8-decode
Closed

0xacee wants to merge 1 commit into
pre-commit:mainfrom
0xacee:fix/check-yaml-utf8-decode

Conversation

@0xacee

@0xacee 0xacee commented Sep 21, 2026

Copy link
Copy Markdown

Problem

check-yaml opens each file as UTF-8 text, but the parse is only guarded against ruamel.yaml.YAMLError. A file that is not valid UTF-8 (for example latin-1) makes load_fn raise UnicodeDecodeError, which escapes main():

$ printf 'key: caf\xe9\n' > latin1.yaml
$ check-yaml latin1.yaml
Traceback (most recent call last):
  ...
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 8: invalid continuation byte

The exception aborts the loop, so any remaining files are never checked, and the user gets a stack trace instead of a diagnostic.

The sibling hooks already handle this cleanly: check-json catches ValueError (a superclass of UnicodeDecodeError), and check-toml gets a TOMLDecodeError from tomllib.

Change

Catch UnicodeDecodeError next to YAMLError and report filename: invalid UTF-8: ..., so the hook marks the file as failed with a readable message and still checks the rest of the arguments.

Testing

  • New regression test test_main_non_utf8_file fails on main (UnicodeDecodeError propagates) and passes with the fix.
  • pytest tests/ — 414 passed, 4 xfailed.

@asottile

Copy link
Copy Markdown
Member

we don't accept ai slop

@asottile asottile closed this Sep 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants