10000 PyYAML: Permit `width: float` for pure-Python `dump(...)` (#8973) · python/typeshed@0884fe1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0884fe1

Browse files
authored
PyYAML: Permit width: float for pure-Python dump(...) (#8973)
* PyYAML: Permit `width: float` for pure-Python `dump(...)` To prevent `PyYAML` from wrapping *any* lines, it's possible to pass `width=float("inf")`, but the current type hints don't like that. This works at runtime: >>> s = yaml.dump({"foo": "bar" * 1000}, width=float("inf")) >>> print(s) foo: barbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbar... but `mypy` says floatwidth.py:2: error: No overload variant of "dump" matches argument types "Dict[str, str]", "float" floatwidth.py:2: note: Possible overload variants: floatwidth.py:2: note: def dump(data: Any, stream: _WriteStream[Any], Dumper: Any = ..., *, default_style: Optional[str] = ..., default_flow_style: Optional[bool] = ..., canonical: Optional[bool] = ..., indent: Optional[int] = ..., width: Optional[int] = ..., allow_unicode: Optional[bool] = ..., line_break: Optional[str] = ..., encoding: Optional[str] = ..., explicit_start: Optional[bool] = ..., explicit_end: Optional[bool] = ..., version: Optional[Tuple[int, int]] = ..., tags: Optional[Mapping[str, str]] = ..., sort_keys: bool = ...) -> None floatwidth.py:2: note: def dump(data: Any, stream: None = ..., Dumper: Any = ..., *, default_style: Optional[str] = ..., default_flow_style: Optional[bool] = ..., canonical: Optional[bool] = ..., indent: Optional[int] = ..., width: Optional[int] = ..., allow_unicode: Optional[bool] = ..., line_break: Optional[str] = ..., encoding: Optional[str] = ..., explicit_start: Optional[bool] = ..., explicit_end: Optional[bool] = ..., version: Optional[Tuple[int, int]] = ..., tags: Optional[Mapping[str, str]] = ..., sort_keys: bool = ...) -> Any Found 1 error in 1 file (checked 1 source file) Poking through the `PyYAML` source, it looks the `width` parameter could probably be anything "comparable", as it's only compared via the `<` operator[1]. For the LibYAML implementation, however, we have to use `int`s: >>> stream = StringIO() >>> dumper = yaml.CDumper(stream, width=float("inf")) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python3/dist-packages/yaml/cyaml.py", line 81, in __init__ version=version, tags=tags) File "ext/_yaml.pyx", line 973, in _yaml.CEmitter.__init__ (ext/_yaml.c:14797) OverflowError: cannot convert float infinity to integer
1 parent 62a6c3d commit 0884fe1

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

stubs/PyYAML/yaml/__init__.pyi

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ from . import resolver as resolver # Help mypy a bit; this is implied by loader
77
from .constructor import BaseConstructor
88
from .cyaml import *
99
from .dumper import *
10+
from .dumper import _Inf
1011
from .emitter import _WriteStream
1112
from .error import *
1213
from .events import *
@@ -46,7 +47,7 @@ def emit(
4647
Dumper=...,
4748
canonical: bool | None = ...,
4849
indent: int | None = ...,
49-
width: int | None = ...,
50+
width: int | _Inf | None = ...,
5051
allow_unicode: bool | None = ...,
5152
line_break: str | None = ...,
5253
): ...
@@ -57,7 +58,7 @@ def serialize_all(
5758
Dumper=...,
5859
canonical: bool | None = ...,
5960
indent: int | None = ...,
60-
width: int | None = ...,
61+
width: int | _Inf | None = ...,
6162
allow_unicode: bool | None = ...,
6263
line_break: str | None = ...,
6364
encoding: str | None = ...,
@@ -73,7 +74,7 @@ def serialize_all(
7374
Dumper=...,
7475
canonical: bool | None = ...,
7576
indent: int | None = ...,
76-
width: int | None = ...,
77+
width: int | _Inf | None = ...,
7778
allow_unicode: bool | None = ...,
7879
line_break: str | None = ...,
7980
encoding: str | None = ...,
@@ -90,7 +91,7 @@ def serialize(
9091
*,
9192
canonical: bool | None = ...,
9293
indent: int | None = ...,
93-
width: int | None = ...,
94+
width: int | _Inf | None = ...,
9495
allow_unicode: bool | None = ...,
9596
line_break: str | None = ...,
9697
encoding: str | None = ...,
@@ -107,7 +108,7 @@ def serialize(
107108
*,
108109
canonical: bool | None = ...,
109110
indent: int | None = ...,
110-
width: int | None = ...,
111+
width: int | _Inf | None = ...,
111112
allow_unicode: bool | None = ...,
112113
line_break: str | None = ...,
113114
encoding: str | None = ...,
@@ -125,7 +126,7 @@ def dump_all(
125126
default_flow_style: bool | None = ...,
126127
canonical: bool | None = ...,
127128
indent: int | None = ...,
128-
width: int | None = ...,
129+
width: int | _Inf | None = ...,
129130
allow_unicode: bool | None = ...,
130131
line_break: str | None = ...,
131132
encoding: str | None = ...,
@@ -144,7 +145,7 @@ def dump_all(
144145
default_flow_style: bool | None = ...,
145146
canonical: bool | None = ...,
146147
indent: int | None = ...,
147-
width: int | None = ...,
148+
width: int | _Inf | None = ...,
148149
allow_unicode: bool | None = ...,
149150
line_break: str | None = ...,
150151
encoding: str | None = ...,
@@ -164,7 +165,7 @@ def dump(
164165
default_flow_style: bool | None = ...,
165166
canonical: bool | None = ...,
166167
indent: int | None = ...,
167-
width: int | None = ...,
168+
width: int | _Inf | None = ...,
168169
allow_unicode: bool | None = ...,
169170
line_break: str | None = ...,
170171
encoding: str | None = ...,
@@ -184,7 +185,7 @@ def dump(
184185
default_flow_style: bool | None = ...,
185186
canonical: bool | None = ...,
186187
indent: int | None = ...,
187-
width: int | None = ...,
188+
width: int | _Inf | None = ...,
188189
allow_unicode: bool | None = ...,
189190
line_break: str | None = ...,
190191
encoding: str | None = ...,
@@ -203,7 +204,7 @@ def safe_dump_all(
203204
default_flow_style: bool | None = ...,
204205
canonical: bool | None = ...,
205206
indent: int | None = ...,
206-
width: int | None = ...,
207+
width: int | _Inf | None = ...,
207208
allow_unicode: bool | None = ...,
208209
line_break: str | None = ...,
209210
encoding: str | None = ...,
@@ -222,7 +223,7 @@ def safe_dump_all(
222223
default_flow_style: bool | None = ...,
223224
canonical: bool | None = ...,
224225
indent: int | None = ...,
225-
width: int | None = ...,
226+
width: int | _Inf | None = ...,
226227
allow_unicode: bool | None = ...,
227228
line_break: str | None = ...,
228229
encoding: str | None = ...,
@@ -241,7 +242,7 @@ def safe_dump(
241242
default_flow_style: bool | None = ...,
242243
canonical: bool | None = ...,
243244
indent: int | None = ...,
244-
width: int | None = ...,
245+
width: int | _Inf | None = ...,
245246
allow_unicode: bool | None = ...,
246247
line_break: str | None = ...,
247248
encoding: str | None = ...,
@@ -260,7 +261,7 @@ def safe_dump(
260261
default_flow_style: bool | None = ...,
261262
canonical: bool | None = ...,
262263
indent: int | None = ...,
263-
width: int | None = ...,
264+
width: int | _Inf | None = ...,
264265
allow_unicode: bool | None = ...,
265266
line_break: str | None = ...,
266267
encoding: str | None = ...,

stubs/PyYAML/yaml/dumper.pyi

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from collections.abc import Mapping
22
from typing import Any
3+
from typing_extensions import TypeAlias
34

45
from yaml.emitter import Emitter
56
from yaml.representer import BaseRepresenter, Representer, SafeRepresenter
@@ -8,6 +9,10 @@ from yaml.serializer import Serializer
89

910
from .emitter import _WriteStream
1011

12+
# Ideally, there would be a way to limit these values to only +/- float("inf"),
13+
# but that's not possible at the moment (https://github.com/python/typing/issues/1160).
14+
_Inf: TypeAlias = float
15+
1116
class BaseDumper(Emitter, Serializer, BaseRepresenter, BaseResolver):
1217
def __init__(
1318
self,
@@ -16,7 +21,7 @@ class BaseDumper(Emitter, Serializer, BaseRepresenter, BaseResolver):
1621
default_flow_style: bool | None = ...,
1722
canonical: bool | None = ...,
1823
indent: int | None = ...,
19-
width: int | None = ...,
24+
width: int | _Inf | None = ...,
2025
allow_unicode: bool | None = ...,
2126
line_break: str | None = ...,
2227
encoding: str | None = ...,
@@ -35,7 +40,7 @@ class SafeDumper(Emitter, Serializer, SafeRepresenter, Resolver):
3540
default_flow_style: bool | None = ...,
3641
canonical: bool | None = ...,
3742
indent: int | None = ...,
38-
width: int | None = ...,
43+
width: int | _Inf | None = ...,
3944
allow_unicode: bool | None = ...,
4045
line_break: str | None = ...,
4146
encoding: str | None = ...,
@@ -54,7 +59,7 @@ class Dumper(Emitter, Serializer, Representer, Resolver):
5459
default_flow_style: bool | None = ...,
5560
canonical: bool | None = ...,
5661
indent: int | None = ...,
57-
width: int | None = ...,
62+
width: int | _Inf | None = ...,
5863
allow_unicode: bool | None = ...,
5964
line_break: str | None = ...,
6065
encoding: str | None = ...,

0 commit comments

Comments
 (0)
0