Skip to content

Commit 8569525

Browse files
committed
Bugfix for flow_map_start.
To my best understanding, in 4e5edc9, the `flow_map_*` and `flow_seq_*` class variables were added to allow different formatting for flow mappings and flow sequences. However, it seems that these are currently not functional due to an assert. This commit makes a small change to allow, in particular, the `flow_map_start` variable to be set and adds a test to help prevent a future regression.
1 parent d6a82e8 commit 8569525

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

_test/test_z_check_debug_leftovers.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import pytest # type: ignore # NOQA
77
from roundtrip import dedent, round_trip_dump, round_trip_load # type: ignore
8+
from ruyaml.emitter import Emitter
89

910

1011
class TestLeftOverDebug:
@@ -38,3 +39,43 @@ def test_01(self, capsys: Any) -> None:
3839
round_trip_dump(d, sys.stdout)
3940
out, err = capsys.readouterr()
4041
assert out == s
42+
43+
def test_02(self, capsys: Any) -> None:
44+
s = dedent(
45+
"""
46+
- 1
47+
- { f: 3.14 , g: 42 }
48+
- { }
49+
- [ 3.14 , 42 ]
50+
- [ ]
51+
""")
52+
d = round_trip_load(s)
53+
54+
current_map_start = Emitter.flow_map_start
55+
current_map_end = Emitter.flow_map_end
56+
current_map_separator = Emitter.flow_map_separator
57+
current_seq_start = Emitter.flow_seq_start
58+
current_seq_end = Emitter.flow_seq_end
59+
current_seq_separator = Emitter.flow_seq_separator
60+
61+
Emitter.flow_map_start = '{ '
62+
Emitter.flow_map_end = ' }'
63+
Emitter.flow_map_separator = ' ,'
64+
65+
Emitter.flow_seq_start = '[ '
66+
Emitter.flow_seq_end = ' ]'
67+
Emitter.flow_seq_separator = ' ,'
68+
69+
try:
70+
round_trip_dump(d, sys.stdout)
71+
finally:
72+
Emitter.flow_map_start = current_map_start
73+
Emitter.flow_map_end = current_map_end
74+
Emitter.flow_map_separator = current_map_separator
75+
76+
Emitter.flow_seq_start = current_seq_start
77+
Emitter.flow_seq_end = current_seq_end
78+
Emitter.flow_seq_separator = current_seq_separator
79+
80+
out, err = capsys.readouterr()
81+
assert out == s

lib/ruyaml/emitter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ def expect_first_flow_mapping_key(self) -> None:
596596
if isinstance(self.event, MappingEndEvent):
597597
self.indent = self.indents.pop()
598598
popped = self.flow_context.pop()
599-
assert popped == '{' # empty flow mapping
599+
assert popped == self.flow_map_start # empty flow mapping
600600
self.write_indicator(self.flow_map_end, False)
601601
if self.event.comment and self.event.comment[0]:
602602
# eol comment on empty mapping
@@ -621,7 +621,7 @@ def expect_flow_mapping_key(self) -> None:
621621
# self.write_pre_comment(self.event)
622622
self.indent = self.indents.pop()
623623
popped = self.flow_context.pop()
624-
assert popped in ['{', '']
624+
assert popped in [self.flow_map_start, '']
625625
if self.canonical:
626626
self.write_indicator(self.flow_map_separator, False)
627627
self.write_indent()

0 commit comments

Comments
 (0)