Skip to content

feat: Make windows-compatible#66

Merged
dmontagu merged 5 commits into
pydantic:mainfrom
Danipulok:feat/windows-compatible
Jul 13, 2026
Merged

feat: Make windows-compatible#66
dmontagu merged 5 commits into
pydantic:mainfrom
Danipulok:feat/windows-compatible

Conversation

@Danipulok

Copy link
Copy Markdown
Contributor

Summary

Hey here!
Thank you very much for the library, it is really useful and helpful

This PR fixes how pytest-examples works on Windows with sane (utf-8) default

Motivation

I'm using pytest-examples library in a small library I will release, and noticed that on Windows I get errors when try to run tests in docs with emojis
Windows has a thing where if you don't specify encoding, the default regional encoding is used, which is never utf-8

Traceback in my project, where I use pytest-examples with current code:

FAILED tests/test_docs.py::test_docs_examples[docs\\lax.md:9-22] - UnicodeEncodeError: 'charmap' codec can't encode character '\u2713' in position 237: character maps to <undefined>

Result of a test I have added in this PR with current code:

self = <encodings.cp1252.IncrementalEncoder object at 0x0000023B17AF9BB0>
input = "print('🚀 ✓')\r\n#> 🚀 ✓\r\n", final = False

    def encode(self, input, final=False):
>       return codecs.charmap_encode(input,self.errors,encoding_table)[0]
E       UnicodeEncodeError: 'charmap' codec can't encode character '\U0001f680' in position 7: character maps to <undefined>

..\..\..\..\..\Roaming\uv\python\cpython-3.14.0-windows-x86_64-none\Lib\encodings\cp1252.py:19: UnicodeEncodeError

@Danipulok

Copy link
Copy Markdown
Contributor Author

Good day, @samuelcolvin!
I know you are really busy and have a lot to do, but is there any way this PR would get merged?
I tested in on Windows with all possible ways and added a test that failed on Windows before my PR
I really like the library and IMHO this would be helpful to a lot of developers on Windows

@hramezani

hramezani commented Jul 10, 2026

Copy link
Copy Markdown

Thanks for this, and sorry for the slow response! 🙏

I reproduced the bug on Windows (cp1252) and confirmed the fix works with no regressions.

One suggestion before we merge: rather than adding a configurable encoding option, I think we should just hardcode encoding='utf-8' at the write/subprocess sites. Reasoning: the read path already hardcodes read_text('utf-8') in find_examples.py, so the library is effectively utf-8-only already — a configurable write encoding could produce files the reader can't read back. Python source is utf-8 by definition anyway, so utf-8 is really the only correct value here.

Concretely that means: drop the encoding field/param from ExamplesConfig, set_config, and _modify_files, and just pass encoding='utf-8' in _write_file, the ruff_check Popen, and _modify_files. Smaller diff, symmetric with the read path, nothing to misconfigure. The regression test is great as-is.

Happy to push this tweak myself if that's easier. Thanks again!

@Danipulok

Copy link
Copy Markdown
Contributor Author

@hramezani, hey!

Thank you very much for the response!
I will do it myself and change it today in a few hours

I think this is a nice idea to simply hardcode it. Because since 3.15 we will have "utf-8" by default anyway

@Danipulok

Copy link
Copy Markdown
Contributor Author

@hramezani, should be good, please check

@dmontagu
dmontagu merged commit e7a7add into pydantic:main Jul 13, 2026
9 checks passed
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.

3 participants