Skip to content

Commit 548dbbb

Browse files
pmhahnJelleZijlstra
authored andcommitted
yaml.dump(..., stream) is optional (#2289)
Depending on if a "stream" or the "encoding" is given, the functions either return None, str/unicode or bytes. Use @overload fix distinguish those cases. Also fix the functions using **kwds as they delegate their work to the more generic functions: copy their signatures. * yaml.dump(): Drop distinguishing encoding As far as I know it's currently impossible to use "overloads with return types depending on optional arguments" (Issue #5621). As "encoding" is in the middle of 11 optional arguments, it would require ~ 2^6 overloads for each of those 6 functions. For now just return Any and wait for Issue #5621 to get fixed.
1 parent 5081d1e commit 548dbbb

File tree

1 file changed

+40
-7
lines changed

1 file changed

+40
-7
lines changed

third_party/2and3/yaml/__init__.pyi

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from typing import Optional, Any, Iterator, Sequence, Union, IO
1+
from typing import Any, IO, Iterator, Optional, overload, Sequence, Text, Union
2+
import sys
23
from yaml.error import * # noqa: F403
34
from yaml.tokens import * # noqa: F403
45
from yaml.events import * # noqa: F403
@@ -8,6 +9,13 @@ from yaml.dumper import * # noqa: F403
89
from . import resolver # Help mypy a bit; this is implied by loader and dumper
910
from .cyaml import *
1011

12+
if sys.version_info < (3,):
13+
_Str = Union[Text, str]
14+
else:
15+
_Str = str
16+
# FIXME: the functions really return py2:unicode/py3:str if encoding is None, otherwise py2:str/py3:bytes. Waiting for python/mypy#5621
17+
_Yaml = Any
18+
1119
__with_libyaml__: Any
1220
__version__: str
1321

@@ -22,12 +30,37 @@ def full_load_all(stream: Union[str, IO[str]]) -> Iterator[Any]: ...
2230
def safe_load(stream: Union[str, IO[str]]) -> Any: ...
2331
def safe_load_all(stream: Union[str, IO[str]]) -> Iterator[Any]: ...
2432
def emit(events, stream=..., Dumper=..., canonical=..., indent=..., width=..., allow_unicode=..., line_break=...): ...
25-
def serialize_all(nodes, stream=..., Dumper=..., canonical=..., indent=..., width=..., allow_unicode=..., line_break=..., encoding=..., explicit_start=..., explicit_end=..., version=..., tags=...): ...
26-
def serialize(node, stream=..., Dumper=..., **kwds): ...
27-
def dump_all(documents: Sequence[Any], stream: IO[str] = ..., Dumper=..., default_style=..., default_flow_style=..., canonical=..., indent=..., width=..., allow_unicode=..., line_break=..., encoding=..., explicit_start=..., explicit_end=..., version=..., tags=...) -> Any: ...
28-
def dump(data: Any, stream: Optional[IO[str]] = ..., Dumper=..., **kwds) -> Any: ...
29-
def safe_dump_all(documents: Sequence[Any], stream: IO[str] = ..., **kwds) -> Any: ...
30-
def safe_dump(data: Any, stream: IO[str] = ..., **kwds) -> Any: ...
33+
34+
@overload
35+
def serialize_all(nodes, stream: IO[str], Dumper=..., canonical=..., indent=..., width=..., allow_unicode=..., line_break=..., encoding=..., explicit_start=..., explicit_end=..., version=..., tags=...) -> None: ...
36+
@overload
37+
def serialize_all(nodes, stream: None = ..., Dumper=..., canonical=..., indent=..., width=..., allow_unicode=..., line_break=..., encoding: Optional[_Str] = ..., explicit_start=..., explicit_end=..., version=..., tags=...) -> _Yaml: ...
38+
39+
@overload
40+
def serialize(node, stream: IO[str], Dumper=..., canonical=..., indent=..., width=..., allow_unicode=..., line_break=..., encoding=..., explicit_start=..., explicit_end=..., version=..., tags=...) -> None: ...
41+
@overload
42+
def serialize(node, stream: None = ..., Dumper=..., canonical=..., indent=..., width=..., allow_unicode=..., line_break=..., encoding: Optional[_Str] = ..., explicit_start=..., explicit_end=..., version=..., tags=...) -> _Yaml: ...
43+
44+
@overload
45+
def dump_all(documents: Sequence[Any], stream: IO[str], Dumper=..., default_style=..., default_flow_style=..., canonical=..., indent=..., width=..., allow_unicode=..., line_break=..., encoding=..., explicit_start=..., explicit_end=..., version=..., tags=...) -> None: ...
46+
@overload
47+
def dump_all(documents: Sequence[Any], stream: None = ..., Dumper=..., default_style=..., default_flow_style=..., canonical=..., indent=..., width=..., allow_unicode=..., line_break=..., encoding: Optional[_Str] = ..., explicit_start=..., explicit_end=..., version=..., tags=...) -> _Yaml: ...
48+
49+
@overload
50+
def dump(data: Any, stream: IO[str], Dumper=..., default_style=..., default_flow_style=..., canonical=..., indent=..., width=..., allow_unicode=..., line_break=..., encoding=..., explicit_start=..., explicit_end=..., version=..., tags=...) -> None: ...
51+
@overload
52+
def dump(data: Any, stream: None = ..., Dumper=..., default_style=..., default_flow_style=..., canonical=..., indent=..., width=..., allow_unicode=..., line_break=..., encoding: Optional[_Str] = ..., explicit_start=..., explicit_end=..., version=..., tags=...) -> _Yaml: ...
53+
54+
@overload
55+
def save_dump_all(documents: Sequence[Any], stream: IO[str], default_style=..., default_flow_style=..., canonical=..., indent=..., width=..., allow_unicode=..., line_break=..., encoding=..., explicit_start=..., explicit_end=..., version=..., tags=...) -> None: ...
56+
@overload
57+
def save_dump_all(documents: Sequence[Any], stream: None = ..., default_style=..., default_flow_style=..., canonical=..., indent=..., width=..., allow_unicode=..., line_break=..., encoding: Optional[_Str] = ..., explicit_start=..., explicit_end=..., version=..., tags=...) -> _Yaml: ...
58+
59+
@overload
60+
def save_dump(data: Any, stream: IO[str], default_style=..., default_flow_style=..., canonical=..., indent=..., width=..., allow_unicode=..., line_break=..., encoding=..., explicit_start=..., explicit_end=..., version=..., tags=...) -> None: ...
61+
@overload
62+
def save_dump(data: Any, stream: None = ..., default_style=..., default_flow_style=..., canonical=..., indent=..., width=..., allow_unicode=..., line_break=..., encoding: Optional[_Str] = ..., explicit_start=..., explicit_end=..., version=..., tags=...) -> _Yaml: ...
63+
3164
def add_implicit_resolver(tag, regexp, first=..., Loader=..., Dumper=...): ...
3265
def add_path_resolver(tag, path, kind=..., Loader=..., Dumper=...): ...
3366
def add_constructor(tag, constructor, Loader=...): ...

0 commit comments

Comments
 (0)