Skip to content

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

Open

Description

@hemtilva

PyYAML's dump and serialization stubs accept binary streams without an output encoding, even though those calls write str and fail at runtime.

from io import BytesIO
import yaml

yaml.safe_dump({'answer': 42}, BytesIO())

With the current PyYAML stubs, mypy reports no error. With PyYAML 6.0.3, this raises TypeError: a bytes-like object is required, not 'str'. Supplying encoding='utf-8' makes the call work. The same mismatch affects dump, dump_all, safe_dump_all, serialize, and serialize_all.

Text streams need separate handling: StringIO accepts an explicit encoding, while a custom text writer without an encoding attribute receives bytes and fails. I checked these combinations with both SafeDumper and CSafeDumper.

I used AI to help investigate this issue.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions