Skip to content

Commit

Permalink
Avoid creating new load method in our custom formatter (#3874)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Oct 27, 2023
1 parent 4b94326 commit bbc7f8c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 15 deletions.
15 changes: 8 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -205,18 +205,19 @@ junit_family = "xunit1"
junit_suite_name = "ansible_lint_test_suite"
minversion = "4.6.6"
norecursedirs = [
"build",
"collections",
"dist",
"docs",
"src/ansible_lint.egg-info",
"*.egg",
".cache",
".eggs",
".git",
".github",
".tox",
"*.egg",
".mypy_cache",
".projects",
".tox",
"build",
"collections",
"dist",
"docs",
"src/ansible_lint.egg-info",
]
python_files = [
"test_*.py",
Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def run(self) -> None:
# any other files. (Based on suggestion from ruamel.yaml author)
yaml = FormattedYAML()

ruamel_data = yaml.loads(data)
ruamel_data = yaml.load(data)
if not isinstance(ruamel_data, (CommentedMap, CommentedSeq)):
# This is an empty vars file or similar which loads as None.
# It is not safe to write this file or data-loss is likely.
Expand Down
5 changes: 3 additions & 2 deletions src/ansiblelint/yaml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
if TYPE_CHECKING:
# noinspection PyProtectedMember
from ruamel.yaml.comments import LineCol
from ruamel.yaml.compat import StreamTextType
from ruamel.yaml.nodes import ScalarNode
from ruamel.yaml.representer import RoundTripRepresenter
from ruamel.yaml.tokens import CommentToken
Expand Down Expand Up @@ -940,7 +941,7 @@ def version(self, value: str | tuple[int, int] | None) -> None:
"""
self._yaml_version = value if value is not None else self._yaml_version_default

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

text, preamble_comment = self._pre_process_yaml(stream)
try:
data = self.load(stream=text)
data = super().load(stream=text)
except ComposerError:
data = self.load_all(stream=text)
except ParserError:
Expand Down
2 changes: 1 addition & 1 deletion test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def regenerate_formatting_fixtures() -> None:

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

Expand Down
8 changes: 4 additions & 4 deletions test/test_yaml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,11 @@ def test_formatted_yaml_loader_dumper(

yaml = ansiblelint.yaml_utils.FormattedYAML()

data_before = yaml.loads(before_content)
data_before = yaml.load(before_content)
dump_from_before = yaml.dumps(data_before)
data_prettier = yaml.loads(prettier_content)
data_prettier = yaml.load(prettier_content)
dump_from_prettier = yaml.dumps(data_prettier)
data_after = yaml.loads(after_content)
data_after = yaml.load(after_content)
dump_from_after = yaml.dumps(data_after)

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


Expand Down

0 comments on commit bbc7f8c

Please sign in to comment.