From 008648f47c0eb8db87e989174ee3709ebe09138e Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Tue, 8 Oct 2024 12:03:06 -0400 Subject: [PATCH] make style=... consistent for unquoted scalar tokens --- lib/yaml/scanner.py | 2 +- tests/test_scan.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 tests/test_scan.py diff --git a/lib/yaml/scanner.py b/lib/yaml/scanner.py index de925b07..4cea1a2e 100644 --- a/lib/yaml/scanner.py +++ b/lib/yaml/scanner.py @@ -1306,7 +1306,7 @@ def scan_plain(self): if not spaces or self.peek() == '#' \ or (not self.flow_level and self.column < indent): break - return ScalarToken(''.join(chunks), True, start_mark, end_mark) + return ScalarToken(''.join(chunks), True, start_mark, end_mark, style='') def scan_plain_spaces(self, indent, start_mark): # See the specification for details. diff --git a/tests/test_scan.py b/tests/test_scan.py new file mode 100644 index 00000000..fa65610b --- /dev/null +++ b/tests/test_scan.py @@ -0,0 +1,18 @@ +import pytest + +import yaml +from yaml.tokens import StreamStartToken, ScalarToken, StreamEndToken + + +_loaders = (yaml.SafeLoader,) +if yaml.__with_libyaml__: + _loaders += (yaml.CSafeLoader,) + + +@pytest.mark.parametrize('loader_cls', _loaders) +def test_scan_produces_empty_string_style_for_scalar_node(loader_cls): + start, scalar, end = yaml.scan('example', Loader=loader_cls) + assert isinstance(start, StreamStartToken) + assert isinstance(scalar, ScalarToken) + assert scalar.style == '' + assert isinstance(end, StreamEndToken)