Skip to content

Commit bbc7f8c

Browse files
authored
Avoid creating new load method in our custom formatter (#3874)
1 parent 4b94326 commit bbc7f8c

5 files changed

Lines changed: 17 additions & 15 deletions

File tree

pyproject.toml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,18 +205,19 @@ junit_family = "xunit1"
205205
junit_suite_name = "ansible_lint_test_suite"
206206
minversion = "4.6.6"
207207
norecursedirs = [
208-
"build",
209-
"collections",
210-
"dist",
211-
"docs",
212-
"src/ansible_lint.egg-info",
208+
"*.egg",
213209
".cache",
214210
".eggs",
215211
".git",
216212
".github",
217-
".tox",
218-
"*.egg",
213+
".mypy_cache",
219214
".projects",
215+
".tox",
216+
"build",
217+
"collections",
218+
"dist",
219+
"docs",
220+
"src/ansible_lint.egg-info",
220221
]
221222
python_files = [
222223
"test_*.py",

src/ansiblelint/transformer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def run(self) -> None:
9494
# any other files. (Based on suggestion from ruamel.yaml author)
9595
yaml = FormattedYAML()
9696

97-
ruamel_data = yaml.loads(data)
97+
ruamel_data = yaml.load(data)
9898
if not isinstance(ruamel_data, (CommentedMap, CommentedSeq)):
9999
# This is an empty vars file or similar which loads as None.
100100
# It is not safe to write this file or data-loss is likely.

src/ansiblelint/yaml_utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
if TYPE_CHECKING:
3636
# noinspection PyProtectedMember
3737
from ruamel.yaml.comments import LineCol
38+
from ruamel.yaml.compat import StreamTextType
3839
from ruamel.yaml.nodes import ScalarNode
3940
from ruamel.yaml.representer import RoundTripRepresenter
4041
from ruamel.yaml.tokens import CommentToken
@@ -940,7 +941,7 @@ def version(self, value: str | tuple[int, int] | None) -> None:
940941
"""
941942
self._yaml_version = value if value is not None else self._yaml_version_default
942943

943-
def loads(self, stream: str) -> Any:
944+
def load(self, stream: Path | StreamTextType) -> Any:
944945
"""Load YAML content from a string while avoiding known ruamel.yaml issues."""
945946
if not isinstance(stream, str):
946947
msg = f"expected a str but got {type(stream)}"
@@ -951,7 +952,7 @@ def loads(self, stream: str) -> Any:
951952

952953
text, preamble_comment = self._pre_process_yaml(stream)
953954
try:
954-
data = self.load(stream=text)
955+
data = super().load(stream=text)
955956
except ComposerError:
956957
data = self.load_all(stream=text)
957958
except ParserError:

test/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def regenerate_formatting_fixtures() -> None:
8383

8484
# Writing fixtures with ansiblelint.yaml_utils.FormattedYAML()
8585
for fixture in fixtures_dir_after.glob("fmt-[0-9].yml"):
86-
data = yaml.loads(fixture.read_text())
86+
data = yaml.load(fixture.read_text())
8787
output = yaml.dumps(data)
8888
fixture.write_text(output)
8989

test/test_yaml_utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,11 @@ def test_formatted_yaml_loader_dumper(
241241

242242
yaml = ansiblelint.yaml_utils.FormattedYAML()
243243

244-
data_before = yaml.loads(before_content)
244+
data_before = yaml.load(before_content)
245245
dump_from_before = yaml.dumps(data_before)
246-
data_prettier = yaml.loads(prettier_content)
246+
data_prettier = yaml.load(prettier_content)
247247
dump_from_prettier = yaml.dumps(data_prettier)
248-
data_after = yaml.loads(after_content)
248+
data_after = yaml.load(after_content)
249249
dump_from_after = yaml.dumps(data_after)
250250

251251
# comparing data does not work because the Comment objects
@@ -276,7 +276,7 @@ def fixture_lintable(file_path: str) -> Lintable:
276276
def fixture_ruamel_data(lintable: Lintable) -> CommentedMap | CommentedSeq:
277277
"""Return the loaded YAML data for the Lintable."""
278278
yaml = ansiblelint.yaml_utils.FormattedYAML()
279-
data: CommentedMap | CommentedSeq = yaml.loads(lintable.content)
279+
data: CommentedMap | CommentedSeq = yaml.load(lintable.content)
280280
return data
281281

282282

0 commit comments

Comments
 (0)