Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions stubs/PyYAML/@tests/test_cases/check_dump_streams.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from io import BytesIO, StringIO
from typing_extensions import assert_type

import yaml


class PlainTextWriter:
def write(self, data: str) -> int:
return len(data)


class TextWriterWithEncoding:
encoding: str | None = None

def write(self, data: str) -> int:
return len(data)


data = {"answer": 42}
node = yaml.ScalarNode("tag:yaml.org,2002:int", "42")
maybe_encoding: str | None = None

# stream=None preserves str/bytes return types
assert_type(yaml.dump(data), str)
assert_type(yaml.dump(data, encoding="utf-8"), bytes)
assert_type(yaml.safe_dump(data), str)
assert_type(yaml.safe_dump(data, encoding="utf-8"), bytes)
assert_type(yaml.dump_all([data]), str)
assert_type(yaml.dump_all([data], encoding="utf-8"), bytes)
assert_type(yaml.safe_dump_all([data]), str)
assert_type(yaml.safe_dump_all([data], encoding="utf-8"), bytes)
assert_type(yaml.serialize(node), str)

Check failure on line 32 in stubs/PyYAML/@tests/test_cases/check_dump_streams.py

View workflow job for this annotation

GitHub Actions / pyright: regression tests (Linux, 3.13)

Type of "serialize" is partially unknown   Type of "serialize" is "Overload[(node: Unknown, stream: _WriteStream[Any], Dumper: ... = ..., *, canonical: bool | None = None, indent: int | None = None, width: int | float | None = None, allow_unicode: bool | None = None, line_break: str | None = None, encoding: str | 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, (node: Unknown, stream: None = None, Dumper: ... = ..., *, canonical: bool | None = None, indent: int | None = None, width: int | float | 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) -> str, (node: Unknown, stream: None = None, Dumper: ... = ..., *, canonical: bool | None = None, indent: int | None = None, width: int | float | 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) -> bytes]" (reportUnknownMemberType)
assert_type(yaml.serialize(node, encoding="utf-8"), bytes)

Check failure on line 33 in stubs/PyYAML/@tests/test_cases/check_dump_streams.py

View workflow job for this annotation

GitHub Actions / pyright: regression tests (Linux, 3.13)

Type of "serialize" is partially unknown   Type of "serialize" is "Overload[(node: Unknown, stream: _WriteStream[Any], Dumper: ... = ..., *, canonical: bool | None = None, indent: int | None = None, width: int | float | None = None, allow_unicode: bool | None = None, line_break: str | None = None, encoding: str | 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, (node: Unknown, stream: None = None, Dumper: ... = ..., *, canonical: bool | None = None, indent: int | None = None, width: int | float | 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) -> str, (node: Unknown, stream: None = None, Dumper: ... = ..., *, canonical: bool | None = None, indent: int | None = None, width: int | float | 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) -> bytes]" (reportUnknownMemberType)
assert_type(yaml.serialize_all([node]), str)

Check failure on line 34 in stubs/PyYAML/@tests/test_cases/check_dump_streams.py

View workflow job for this annotation

GitHub Actions / pyright: regression tests (Linux, 3.13)

Type of "serialize_all" is partially unknown   Type of "serialize_all" is "Overload[(nodes: Unknown, stream: _WriteStream[Any], Dumper: ... = ..., *, canonical: bool | None = None, indent: int | None = None, width: int | float | None = None, allow_unicode: bool | None = None, line_break: str | None = None, encoding: str | 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, (nodes: Unknown, stream: None = None, Dumper: ... = ..., *, canonical: bool | None = None, indent: int | None = None, width: int | float | 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) -> str, (nodes: Unknown, stream: None = None, Dumper: ... = ..., *, canonical: bool | None = None, indent: int | None = None, width: int | float | 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) -> bytes]" (reportUnknownMemberType)
assert_type(yaml.serialize_all([node], encoding="utf-8"), bytes)

Check failure on line 35 in stubs/PyYAML/@tests/test_cases/check_dump_streams.py

View workflow job for this annotation

GitHub Actions / pyright: regression tests (Linux, 3.13)

Type of "serialize_all" is partially unknown   Type of "serialize_all" is "Overload[(nodes: Unknown, stream: _WriteStream[Any], Dumper: ... = ..., *, canonical: bool | None = None, indent: int | None = None, width: int | float | None = None, allow_unicode: bool | None = None, line_break: str | None = None, encoding: str | 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, (nodes: Unknown, stream: None = None, Dumper: ... = ..., *, canonical: bool | None = None, indent: int | None = None, width: int | float | 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) -> str, (nodes: Unknown, stream: None = None, Dumper: ... = ..., *, canonical: bool | None = None, indent: int | None = None, width: int | float | 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) -> bytes]" (reportUnknownMemberType)

# Binary streams require an explicit encoding
yaml.dump(data, BytesIO()) # type: ignore

Check failure on line 38 in stubs/PyYAML/@tests/test_cases/check_dump_streams.py

View workflow job for this annotation

GitHub Actions / pyright: regression tests (Linux, 3.13)

Unnecessary "# type: ignore" comment (reportUnnecessaryTypeIgnoreComment)
yaml.dump_all([data], BytesIO()) # type: ignore

Check failure on line 39 in stubs/PyYAML/@tests/test_cases/check_dump_streams.py

View workflow job for this annotation

GitHub Actions / pyright: regression tests (Linux, 3.13)

Unnecessary "# type: ignore" comment (reportUnnecessaryTypeIgnoreComment)
yaml.safe_dump(data, BytesIO()) # type: ignore

Check failure on line 40 in stubs/PyYAML/@tests/test_cases/check_dump_streams.py

View workflow job for this annotation

GitHub Actions / pyright: regression tests (Linux, 3.13)

Unnecessary "# type: ignore" comment (reportUnnecessaryTypeIgnoreComment)
yaml.safe_dump_all([data], BytesIO()) # type: ignore

Check failure on line 41 in stubs/PyYAML/@tests/test_cases/check_dump_streams.py

View workflow job for this annotation

GitHub Actions / pyright: regression tests (Linux, 3.13)

Unnecessary "# type: ignore" comment (reportUnnecessaryTypeIgnoreComment)
yaml.serialize(node, BytesIO()) # type: ignore
yaml.serialize_all([node], BytesIO()) # type: ignore

assert_type(yaml.dump(data, BytesIO(), encoding="utf-8"), None)
assert_type(yaml.dump_all([data], BytesIO(), encoding="utf-8"), None)
assert_type(yaml.safe_dump(data, BytesIO(), encoding="utf-8"), None)
assert_type(yaml.safe_dump_all([data], BytesIO(), encoding="utf-8"), None)
assert_type(yaml.serialize(node, BytesIO(), encoding="utf-8"), None)

Check failure on line 49 in stubs/PyYAML/@tests/test_cases/check_dump_streams.py

View workflow job for this annotation

GitHub Actions / pyright: regression tests (Linux, 3.13)

Type of "serialize" is partially unknown   Type of "serialize" is "Overload[(node: Unknown, stream: _WriteStream[Any], Dumper: ... = ..., *, canonical: bool | None = None, indent: int | None = None, width: int | float | None = None, allow_unicode: bool | None = None, line_break: str | None = None, encoding: str | 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, (node: Unknown, stream: None = None, Dumper: ... = ..., *, canonical: bool | None = None, indent: int | None = None, width: int | float | 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) -> str, (node: Unknown, stream: None = None, Dumper: ... = ..., *, canonical: bool | None = None, indent: int | None = None, width: int | float | 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) -> bytes]" (reportUnknownMemberType)
assert_type(yaml.serialize_all([node], BytesIO(), encoding="utf-8"), None)

Check failure on line 50 in stubs/PyYAML/@tests/test_cases/check_dump_streams.py

View workflow job for this annotation

GitHub Actions / pyright: regression tests (Linux, 3.13)

Type of "serialize_all" is partially unknown   Type of "serialize_all" is "Overload[(nodes: Unknown, stream: _WriteStream[Any], Dumper: ... = ..., *, canonical: bool | None = None, indent: int | None = None, width: int | float | None = None, allow_unicode: bool | None = None, line_break: str | None = None, encoding: str | 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, (nodes: Unknown, stream: None = None, Dumper: ... = ..., *, canonical: bool | None = None, indent: int | None = None, width: int | float | 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) -> str, (nodes: Unknown, stream: None = None, Dumper: ... = ..., *, canonical: bool | None = None, indent: int | None = None, width: int | float | 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) -> bytes]" (reportUnknownMemberType)

# Text streams with an encoding attribute accept both omitted/None and str encodings
assert_type(yaml.dump(data, StringIO()), None)
assert_type(yaml.dump(data, StringIO(), encoding="utf-8"), None)
assert_type(yaml.dump(data, StringIO(), encoding=maybe_encoding), None)
assert_type(yaml.safe_dump(data, StringIO()), None)
assert_type(yaml.safe_dump(data, StringIO(), encoding="utf-8"), None)
assert_type(yaml.safe_dump(data, StringIO(), encoding=maybe_encoding), None)
assert_type(yaml.dump_all([data], StringIO(), encoding="utf-8"), None)
assert_type(yaml.safe_dump_all([data], StringIO(), encoding="utf-8"), None)
assert_type(yaml.serialize(node, StringIO(), encoding="utf-8"), None)
assert_type(yaml.serialize_all([node], StringIO(), encoding="utf-8"), None)

assert_type(yaml.safe_dump(data, TextWriterWithEncoding()), None)
assert_type(yaml.safe_dump(data, TextWriterWithEncoding(), encoding="utf-8"), None)
assert_type(yaml.safe_dump(data, TextWriterWithEncoding(), encoding=maybe_encoding), None)

# Custom text writers without an encoding attribute reject explicit encodings
assert_type(yaml.dump(data, PlainTextWriter()), None)
assert_type(yaml.dump_all([data], PlainTextWriter()), None)
assert_type(yaml.safe_dump(data, PlainTextWriter()), None)
assert_type(yaml.safe_dump_all([data], PlainTextWriter()), None)
assert_type(yaml.serialize(node, PlainTextWriter()), None)
assert_type(yaml.serialize_all([node], PlainTextWriter()), None)

yaml.dump(data, PlainTextWriter(), encoding="utf-8") # type: ignore
yaml.dump_all([data], PlainTextWriter(), encoding="utf-8") # type: ignore
yaml.safe_dump(data, PlainTextWriter(), encoding="utf-8") # type: ignore
yaml.safe_dump_all([data], PlainTextWriter(), encoding="utf-8") # type: ignore
yaml.serialize(node, PlainTextWriter(), encoding="utf-8") # type: ignore
yaml.serialize_all([node], PlainTextWriter(), encoding="utf-8") # type: ignore
238 changes: 231 additions & 7 deletions stubs/PyYAML/yaml/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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 _TextWriteStreamWithEncoding, _WriteStream, _YAMLObject
from .error import *
from .events import *
from .loader import *
Expand Down Expand Up @@ -54,7 +54,41 @@ 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: _WriteStream[bytes] | _TextWriteStreamWithEncoding,
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: _TextWriteStreamWithEncoding,
Dumper=...,
*,
canonical: bool | None = None,
Expand Down Expand Up @@ -106,7 +140,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: _WriteStream[bytes] | _TextWriteStreamWithEncoding,
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: _TextWriteStreamWithEncoding,
Dumper=...,
*,
canonical: bool | None = None,
Expand Down Expand Up @@ -158,7 +226,47 @@ 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: _WriteStream[bytes] | _TextWriteStreamWithEncoding,
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: _TextWriteStreamWithEncoding,
Dumper=...,
*,
default_style: str | None = None,
Expand Down Expand Up @@ -219,7 +327,47 @@ 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: _WriteStream[bytes] | _TextWriteStreamWithEncoding,
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: _TextWriteStreamWithEncoding,
Dumper=...,
*,
default_style: str | None = None,
Expand Down Expand Up @@ -280,7 +428,45 @@ 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,
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_all(
documents: Iterable[_YAMLObject],
stream: _WriteStream[bytes] | _TextWriteStreamWithEncoding,
*,
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: _TextWriteStreamWithEncoding,
*,
default_style: str | None = None,
default_flow_style: bool | None = False,
Expand Down Expand Up @@ -338,7 +524,45 @@ 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: _WriteStream[bytes] | _TextWriteStreamWithEncoding,
*,
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(
data: _YAMLObject,
stream: _TextWriteStreamWithEncoding,
*,
default_style: str | None = None,
default_flow_style: bool | None = False,
Expand Down
6 changes: 6 additions & 0 deletions stubs/PyYAML/yaml/emitter.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ class _WriteStream(Protocol[_T_contra]):
# encoding: str
# def flush(self) -> object: ...

@type_check_only
class _TextWriteStreamWithEncoding(Protocol):
@property
def encoding(self) -> str | None: ...
def write(self, data: str, /) -> object: ...

class EmitterError(YAMLError): ...

class ScalarAnalysis:
Expand Down
Loading