From 878702df0dcd5f44a46811284a4b6794a157007c Mon Sep 17 00:00:00 2001 From: hemtilva Date: Mon, 16 Nov 2026 19:47:58 +0530 Subject: [PATCH 1/2] PyYAML: distinguish output streams by encoding --- stubs/PyYAML/@tests/test_cases/streams.py | 32 +++ stubs/PyYAML/yaml/__init__.pyi | 250 ++++++++++++++++++++-- stubs/PyYAML/yaml/emitter.pyi | 5 + 3 files changed, 274 insertions(+), 13 deletions(-) create mode 100644 stubs/PyYAML/@tests/test_cases/streams.py diff --git a/stubs/PyYAML/@tests/test_cases/streams.py b/stubs/PyYAML/@tests/test_cases/streams.py new file mode 100644 index 000000000000..6c51d17209fc --- /dev/null +++ b/stubs/PyYAML/@tests/test_cases/streams.py @@ -0,0 +1,32 @@ +from io import BytesIO, StringIO +from typing_extensions import assert_type + +import yaml + +node = yaml.ScalarNode("tag:yaml.org,2002:str", "example") + +assert_type(yaml.dump({}, StringIO()), None) +assert_type(yaml.dump({}, StringIO(), encoding="utf-8"), None) +assert_type(yaml.dump({}, BytesIO(), encoding="utf-8"), None) +assert_type(yaml.dump_all([{}], BytesIO(), encoding="utf-8"), None) +assert_type(yaml.safe_dump({}, BytesIO(), encoding="utf-8"), None) +assert_type(yaml.safe_dump_all([{}], BytesIO(), encoding="utf-8"), None) +assert_type(yaml.serialize(node, BytesIO(), encoding="utf-8"), None) # pyright: ignore[reportUnknownMemberType] +assert_type(yaml.serialize_all([node], BytesIO(), encoding="utf-8"), None) # pyright: ignore[reportUnknownMemberType] +assert_type(yaml.safe_dump({}), str) +assert_type(yaml.safe_dump({}, encoding="utf-8"), bytes) + +yaml.dump({}, BytesIO()) # type: ignore +yaml.dump_all([{}], BytesIO()) # type: ignore +yaml.safe_dump({}, BytesIO()) # type: ignore +yaml.safe_dump_all([{}], BytesIO()) # type: ignore +yaml.serialize(node, BytesIO()) # type: ignore +yaml.serialize_all([node], BytesIO()) # type: ignore + + +class TextWriter: + def write(self, data: str, /) -> None: ... + + +assert_type(yaml.safe_dump({}, TextWriter()), None) +yaml.safe_dump({}, TextWriter(), encoding="utf-8") # type: ignore diff --git a/stubs/PyYAML/yaml/__init__.pyi b/stubs/PyYAML/yaml/__init__.pyi index 4f35bdbe8fc2..c3a5cd5a958a 100644 --- a/stubs/PyYAML/yaml/__init__.pyi +++ b/stubs/PyYAML/yaml/__init__.pyi @@ -9,7 +9,7 @@ from .cyaml import * from .cyaml import _CLoader from .dumper import * from .dumper import _Inf -from .emitter import _WriteStream, _YAMLObject +from .emitter import _TextWriteStream, _WriteStream, _YAMLObject from .error import * from .events import * from .loader import * @@ -54,7 +54,7 @@ def emit( @overload def serialize_all( nodes, - stream: _WriteStream[_YAMLObject], + stream: _WriteStream[str], Dumper=..., *, canonical: bool | None = None, @@ -62,7 +62,41 @@ def serialize_all( width: int | _Inf | None = None, allow_unicode: bool | None = None, line_break: str | None = None, - encoding: str | None = None, + encoding: None = None, + explicit_start: bool | None = None, + explicit_end: bool | None = None, + version: tuple[int, int] | None = None, + tags: Mapping[str, str] | None = None, +) -> None: ... +@overload +def serialize_all( + nodes, + stream: _TextWriteStream, + Dumper=..., + *, + canonical: bool | None = None, + indent: int | None = None, + width: int | _Inf | None = None, + allow_unicode: bool | None = None, + line_break: str | None = None, + encoding: str, + explicit_start: bool | None = None, + explicit_end: bool | None = None, + version: tuple[int, int] | None = None, + tags: Mapping[str, str] | None = None, +) -> None: ... +@overload +def serialize_all( + nodes, + stream: _WriteStream[bytes], + Dumper=..., + *, + canonical: bool | None = None, + indent: int | None = None, + width: int | _Inf | None = None, + allow_unicode: bool | None = None, + line_break: str | None = None, + encoding: str, explicit_start: bool | None = None, explicit_end: bool | None = None, version: tuple[int, int] | None = None, @@ -106,7 +140,7 @@ def serialize_all( @overload def serialize( node, - stream: _WriteStream[_YAMLObject], + stream: _WriteStream[str], Dumper=..., *, canonical: bool | None = None, @@ -114,7 +148,41 @@ def serialize( width: int | _Inf | None = None, allow_unicode: bool | None = None, line_break: str | None = None, - encoding: str | None = None, + encoding: None = None, + explicit_start: bool | None = None, + explicit_end: bool | None = None, + version: tuple[int, int] | None = None, + tags: Mapping[str, str] | None = None, +) -> None: ... +@overload +def serialize( + node, + stream: _TextWriteStream, + Dumper=..., + *, + canonical: bool | None = None, + indent: int | None = None, + width: int | _Inf | None = None, + allow_unicode: bool | None = None, + line_break: str | None = None, + encoding: str, + explicit_start: bool | None = None, + explicit_end: bool | None = None, + version: tuple[int, int] | None = None, + tags: Mapping[str, str] | None = None, +) -> None: ... +@overload +def serialize( + node, + stream: _WriteStream[bytes], + Dumper=..., + *, + canonical: bool | None = None, + indent: int | None = None, + width: int | _Inf | None = None, + allow_unicode: bool | None = None, + line_break: str | None = None, + encoding: str, explicit_start: bool | None = None, explicit_end: bool | None = None, version: tuple[int, int] | None = None, @@ -158,7 +226,27 @@ def serialize( @overload def dump_all( documents: Iterable[_YAMLObject], - stream: _WriteStream[_YAMLObject], + stream: _WriteStream[str], + Dumper=..., + *, + default_style: str | None = None, + default_flow_style: bool | None = False, + canonical: bool | None = None, + indent: int | None = None, + width: int | _Inf | None = None, + allow_unicode: bool | None = None, + line_break: str | None = None, + encoding: None = None, + explicit_start: bool | None = None, + explicit_end: bool | None = None, + version: tuple[int, int] | None = None, + tags: Mapping[str, str] | None = None, + sort_keys: bool = True, +) -> None: ... +@overload +def dump_all( + documents: Iterable[_YAMLObject], + stream: _TextWriteStream, Dumper=..., *, default_style: str | None = None, @@ -168,7 +256,27 @@ def dump_all( width: int | _Inf | None = None, allow_unicode: bool | None = None, line_break: str | None = None, - encoding: str | None = None, + encoding: str, + explicit_start: bool | None = None, + explicit_end: bool | None = None, + version: tuple[int, int] | None = None, + tags: Mapping[str, str] | None = None, + sort_keys: bool = True, +) -> None: ... +@overload +def dump_all( + documents: Iterable[_YAMLObject], + stream: _WriteStream[bytes], + Dumper=..., + *, + default_style: str | None = None, + default_flow_style: bool | None = False, + canonical: bool | None = None, + indent: int | None = None, + width: int | _Inf | None = None, + allow_unicode: bool | None = None, + line_break: str | None = None, + encoding: str, explicit_start: bool | None = None, explicit_end: bool | None = None, version: tuple[int, int] | None = None, @@ -219,7 +327,27 @@ def dump_all( @overload def dump( data: _YAMLObject, - stream: _WriteStream[_YAMLObject], + stream: _WriteStream[str], + Dumper=..., + *, + default_style: str | None = None, + default_flow_style: bool | None = False, + canonical: bool | None = None, + indent: int | None = None, + width: int | _Inf | None = None, + allow_unicode: bool | None = None, + line_break: str | None = None, + encoding: None = None, + explicit_start: bool | None = None, + explicit_end: bool | None = None, + version: tuple[int, int] | None = None, + tags: Mapping[str, str] | None = None, + sort_keys: bool = True, +) -> None: ... +@overload +def dump( + data: _YAMLObject, + stream: _TextWriteStream, Dumper=..., *, default_style: str | None = None, @@ -229,7 +357,27 @@ def dump( width: int | _Inf | None = None, allow_unicode: bool | None = None, line_break: str | None = None, - encoding: str | None = None, + encoding: str, + explicit_start: bool | None = None, + explicit_end: bool | None = None, + version: tuple[int, int] | None = None, + tags: Mapping[str, str] | None = None, + sort_keys: bool = True, +) -> None: ... +@overload +def dump( + data: _YAMLObject, + stream: _WriteStream[bytes], + Dumper=..., + *, + default_style: str | None = None, + default_flow_style: bool | None = False, + canonical: bool | None = None, + indent: int | None = None, + width: int | _Inf | None = None, + allow_unicode: bool | None = None, + line_break: str | None = None, + encoding: str, explicit_start: bool | None = None, explicit_end: bool | None = None, version: tuple[int, int] | None = None, @@ -280,7 +428,7 @@ def dump( @overload def safe_dump_all( documents: Iterable[_YAMLObject], - stream: _WriteStream[_YAMLObject], + stream: _WriteStream[str], *, default_style: str | None = None, default_flow_style: bool | None = False, @@ -289,7 +437,45 @@ def safe_dump_all( width: int | _Inf | None = None, allow_unicode: bool | None = None, line_break: str | None = None, - encoding: str | None = None, + encoding: None = None, + explicit_start: bool | None = None, + explicit_end: bool | None = None, + version: tuple[int, int] | None = None, + tags: Mapping[str, str] | None = None, + sort_keys: bool = True, +) -> None: ... +@overload +def safe_dump_all( + documents: Iterable[_YAMLObject], + stream: _TextWriteStream, + *, + default_style: str | None = None, + default_flow_style: bool | None = False, + canonical: bool | None = None, + indent: int | None = None, + width: int | _Inf | None = None, + allow_unicode: bool | None = None, + line_break: str | None = None, + encoding: str, + explicit_start: bool | None = None, + explicit_end: bool | None = None, + version: tuple[int, int] | None = None, + tags: Mapping[str, str] | None = None, + sort_keys: bool = True, +) -> None: ... +@overload +def safe_dump_all( + documents: Iterable[_YAMLObject], + stream: _WriteStream[bytes], + *, + default_style: str | None = None, + default_flow_style: bool | None = False, + canonical: bool | None = None, + indent: int | None = None, + width: int | _Inf | None = None, + allow_unicode: bool | None = None, + line_break: str | None = None, + encoding: str, explicit_start: bool | None = None, explicit_end: bool | None = None, version: tuple[int, int] | None = None, @@ -338,7 +524,26 @@ def safe_dump_all( @overload def safe_dump( data: _YAMLObject, - stream: _WriteStream[_YAMLObject], + stream: _WriteStream[str], + *, + default_style: str | None = None, + default_flow_style: bool | None = False, + canonical: bool | None = None, + indent: int | None = None, + width: int | _Inf | None = None, + allow_unicode: bool | None = None, + line_break: str | None = None, + encoding: None = None, + explicit_start: bool | None = None, + explicit_end: bool | None = None, + version: tuple[int, int] | None = None, + tags: Mapping[str, str] | None = None, + sort_keys: bool = True, +) -> None: ... +@overload +def safe_dump( + data: _YAMLObject, + stream: _TextWriteStream, *, default_style: str | None = None, default_flow_style: bool | None = False, @@ -347,7 +552,26 @@ def safe_dump( width: int | _Inf | None = None, allow_unicode: bool | None = None, line_break: str | None = None, - encoding: str | None = None, + encoding: str, + explicit_start: bool | None = None, + explicit_end: bool | None = None, + version: tuple[int, int] | None = None, + tags: Mapping[str, str] | None = None, + sort_keys: bool = True, +) -> None: ... +@overload +def safe_dump( + data: _YAMLObject, + stream: _WriteStream[bytes], + *, + default_style: str | None = None, + default_flow_style: bool | None = False, + canonical: bool | None = None, + indent: int | None = None, + width: int | _Inf | None = None, + allow_unicode: bool | None = None, + line_break: str | None = None, + encoding: str, explicit_start: bool | None = None, explicit_end: bool | None = None, version: tuple[int, int] | None = None, diff --git a/stubs/PyYAML/yaml/emitter.pyi b/stubs/PyYAML/yaml/emitter.pyi index 80388523f320..385100bf5a72 100644 --- a/stubs/PyYAML/yaml/emitter.pyi +++ b/stubs/PyYAML/yaml/emitter.pyi @@ -17,6 +17,11 @@ class _WriteStream(Protocol[_T_contra]): # encoding: str # def flush(self) -> object: ... +@type_check_only +class _TextWriteStream(_WriteStream[str], Protocol): + @property + def encoding(self) -> str | None: ... + class EmitterError(YAMLError): ... class ScalarAnalysis: From 250bfafde2d5816ed954cff654e0372e430d5496 Mon Sep 17 00:00:00 2001 From: hemtilva Date: Mon, 16 Nov 2026 19:55:29 +0530 Subject: [PATCH 2/2] Fix PyYAML regression checks --- .../@tests/test_cases/{streams.py => check_streams.py} | 3 +++ stubs/PyYAML/yaml/__init__.pyi | 9 +++++++-- stubs/PyYAML/yaml/emitter.pyi | 5 ----- 3 files changed, 10 insertions(+), 7 deletions(-) rename stubs/PyYAML/@tests/test_cases/{streams.py => check_streams.py} (90%) diff --git a/stubs/PyYAML/@tests/test_cases/streams.py b/stubs/PyYAML/@tests/test_cases/check_streams.py similarity index 90% rename from stubs/PyYAML/@tests/test_cases/streams.py rename to stubs/PyYAML/@tests/test_cases/check_streams.py index 6c51d17209fc..0a5e859af3df 100644 --- a/stubs/PyYAML/@tests/test_cases/streams.py +++ b/stubs/PyYAML/@tests/test_cases/check_streams.py @@ -1,3 +1,6 @@ +# Pyright may resolve the published types-PyYAML package, which predates these overloads. +# pyright: reportUnnecessaryTypeIgnoreComment=false + from io import BytesIO, StringIO from typing_extensions import assert_type diff --git a/stubs/PyYAML/yaml/__init__.pyi b/stubs/PyYAML/yaml/__init__.pyi index c3a5cd5a958a..a8a59c3805c3 100644 --- a/stubs/PyYAML/yaml/__init__.pyi +++ b/stubs/PyYAML/yaml/__init__.pyi @@ -1,7 +1,7 @@ from _typeshed import Incomplete from collections.abc import Callable, Iterable, Iterator, Mapping from re import Pattern -from typing import Final, TypeVar, overload +from typing import Final, Protocol, TypeVar, overload, type_check_only from . import resolver as resolver # Help mypy a bit; this is implied by loader and dumper from .constructor import BaseConstructor @@ -9,7 +9,7 @@ from .cyaml import * from .cyaml import _CLoader from .dumper import * from .dumper import _Inf -from .emitter import _TextWriteStream, _WriteStream, _YAMLObject +from .emitter import _WriteStream, _YAMLObject from .error import * from .events import * from .loader import * @@ -24,6 +24,11 @@ _T = TypeVar("_T") _Constructor = TypeVar("_Constructor", bound=BaseConstructor) _Representer = TypeVar("_Representer", bound=BaseRepresenter) +@type_check_only +class _TextWriteStream(_WriteStream[str], Protocol): + @property + def encoding(self) -> str | None: ... + __with_libyaml__: Final[bool] __version__: Final[str] diff --git a/stubs/PyYAML/yaml/emitter.pyi b/stubs/PyYAML/yaml/emitter.pyi index 385100bf5a72..80388523f320 100644 --- a/stubs/PyYAML/yaml/emitter.pyi +++ b/stubs/PyYAML/yaml/emitter.pyi @@ -17,11 +17,6 @@ class _WriteStream(Protocol[_T_contra]): # encoding: str # def flush(self) -> object: ... -@type_check_only -class _TextWriteStream(_WriteStream[str], Protocol): - @property - def encoding(self) -> str | None: ... - class EmitterError(YAMLError): ... class ScalarAnalysis: