typer ... utils docs --output crashes with UnicodeEncodeError on non-UTF-8 locales (e.g. Windows cp1252) when help contains emojis
#1882
Replies: 2 comments
-
|
Hello, |
Beta Was this translation helpful? Give feedback.
-
|
Thanks @phalberg, and thanks for confirming it. That For the actual fix I opened PR #1881: it writes the docs file as UTF-8 (one line in typer/cli.py) and adds a regression test that forces a non UTF-8 locale, so the behavior stays covered on every platform and not only Windows. Glad to take it whichever way the maintainers prefer. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Example Code
Description
typer utils docs --output FILE writes the Markdown with Path.write_text(clean_docs), which uses the platform's default encoding. When the CLI's help contains non-ASCII characters - emojis are common in Typer/Rich apps - this raises UnicodeEncodeError on interpreters whose locale encoding isn't UTF-8 (e.g. cp1252 on Windows).
The fix is a one-liner - write the file as UTF-8 (which matches how the tests read it back). I have the fix plus a regression test ready and would be happy to open a PR.
Operating System
Windows
Operating System Details
Windows 11, cp1252 default locale encoding. Also reproducible on any OS with a non-UTF-8 locale, e.g. LC_ALL=C on Linux.
Project Version
0.26.8
Python Version
3.10.11
Additional Context
The crash is in typer/cli.py in the
docscommand:output.write_text(clean_docs)writes without an explicit encoding, so it uses the locale default (cp1252 on Windows) and fails on non-ASCII help. The fix is a one-liner - write as UTF-8 (matching how the tests read the file back). I have the fix plus a regression test ready and am happy to open a PR.Beta Was this translation helpful? Give feedback.
All reactions