diff --git a/stubs/PyYAML/@tests/test_cases/check_streams.py b/stubs/PyYAML/@tests/test_cases/check_streams.py new file mode 100644 index 000000000000..0a5e859af3df --- /dev/null +++ b/stubs/PyYAML/@tests/test_cases/check_streams.py @@ -0,0 +1,35 @@ +# 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 + +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..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 @@ -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] @@ -54,7 +59,24 @@ def emit( @overload def serialize_all( nodes, - stream: _WriteStream[_YAMLObject], + stream: _WriteStream[str], + 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: 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, @@ -62,7 +84,24 @@ def serialize_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, +) -> 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 +145,41 @@ def serialize_all( @overload def serialize( node, - stream: _WriteStream[_YAMLObject], + stream: _WriteStream[str], + 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: 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, @@ -114,7 +187,7 @@ def serialize( 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, @@ -158,7 +231,7 @@ def serialize( @overload def dump_all( documents: Iterable[_YAMLObject], - stream: _WriteStream[_YAMLObject], + stream: _WriteStream[str], Dumper=..., *, default_style: str | None = None, @@ -168,7 +241,47 @@ def 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 dump_all( + documents: Iterable[_YAMLObject], + stream: _TextWriteStream, + 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, + 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 +332,7 @@ def dump_all( @overload def dump( data: _YAMLObject, - stream: _WriteStream[_YAMLObject], + stream: _WriteStream[str], Dumper=..., *, default_style: str | None = None, @@ -229,7 +342,47 @@ def dump( 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 dump( + data: _YAMLObject, + stream: _TextWriteStream, + 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, + 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 +433,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 +442,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 +529,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 +557,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,