Skip to content

[PyYAML] Type output streams according to encoding - #16838

Open
hemtilva wants to merge 2 commits into
python:mainfrom
hemtilva:fix/pyyaml-stream-encoding
Open

hemtilva wants to merge 2 commits into
python:mainfrom
hemtilva:fix/pyyaml-stream-encoding

Conversation

@hemtilva

Copy link
Copy Markdown
Contributor

Fixes #16837

PyYAML writes text to a binary stream when no encoding is supplied, so calls such as yaml.safe_dump(data, BytesIO()) type-check but raise TypeError. Distinguish text writers, text writers exposing encoding, and binary writers with an explicit encoding across the dump and serialize APIs.

PyYAML chooses what to write based on the presence of a stream's encoding attribute. Python protocols cannot express that an arbitrary binary writer lacks that attribute, so the overloads cover common streams without claiming to model every custom writer. A binary writer that also exposes encoding remains a known limitation.

I used AI to help investigate and implement this change.

Checks: runtime stream matrix across the six APIs, plus the matrix with both SafeDumper and CSafeDumper for dump; mypy and Pyright; regression tests on all supported Python/platform targets; Python 3.13 stubtest; Ruff and Black on the changed files.

Copilot AI balanced review requested due to automatic review settings November 16, 2026 14:19

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@github-actions

This comment has been minimized.

1 similar comment
@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

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.

[PyYAML] Dump and serialize stubs accept binary streams without an encoding

2 participants