Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable YAML 1.2 support for non-ansible files #3809

Merged
merged 1 commit into from
Nov 3, 2023
Merged

Conversation

ssbarnea
Copy link
Member

@ssbarnea ssbarnea commented Oct 3, 2023

Fixes: #3790

@ssbarnea ssbarnea temporarily deployed to ack October 3, 2023 17:33 — with GitHub Actions Inactive
@ssbarnea ssbarnea temporarily deployed to ack October 3, 2023 18:07 — with GitHub Actions Inactive
@ssbarnea ssbarnea temporarily deployed to ack October 3, 2023 18:41 — with GitHub Actions Inactive
@ssbarnea ssbarnea temporarily deployed to ack October 3, 2023 19:40 — with GitHub Actions Inactive
@ssbarnea ssbarnea changed the title Correctly recognize specific syntax on GitHub workflows Enable YAML 1.2 support for non-ansible files Oct 11, 2023
ssbarnea added a commit that referenced this pull request Oct 11, 2023
ssbarnea added a commit that referenced this pull request Oct 11, 2023
@ssbarnea ssbarnea temporarily deployed to ack October 30, 2023 09:52 — with GitHub Actions Inactive
@pre-commit-ci pre-commit-ci bot temporarily deployed to ack October 30, 2023 09:53 Inactive
@ssbarnea ssbarnea temporarily deployed to ack October 30, 2023 10:10 — with GitHub Actions Inactive
@ssbarnea ssbarnea temporarily deployed to ack October 30, 2023 10:35 — with GitHub Actions Inactive
@ssbarnea ssbarnea self-assigned this Oct 30, 2023
@ssbarnea ssbarnea marked this pull request as ready for review October 30, 2023 10:35
@ssbarnea ssbarnea requested a review from a team as a code owner October 30, 2023 10:35
@ssbarnea ssbarnea requested review from a team, audgirka and priyamsahoo and removed request for a team October 30, 2023 10:35
@ssbarnea ssbarnea temporarily deployed to ack October 30, 2023 10:53 — with GitHub Actions Inactive
@ssbarnea ssbarnea temporarily deployed to ack October 30, 2023 11:07 — with GitHub Actions Inactive
@ssbarnea ssbarnea temporarily deployed to ack October 30, 2023 11:29 — with GitHub Actions Inactive
@ssbarnea ssbarnea requested review from shatakshiiii, cidrblock and Qalthos and removed request for audgirka and priyamsahoo October 30, 2023 11:29
test/test_yaml_utils.py Show resolved Hide resolved
src/ansiblelint/yaml_utils.py Outdated Show resolved Hide resolved
src/ansiblelint/yaml_utils.py Outdated Show resolved Hide resolved
src/ansiblelint/yaml_utils.py Outdated Show resolved Hide resolved
src/ansiblelint/yaml_utils.py Show resolved Hide resolved
src/ansiblelint/transformer.py Outdated Show resolved Hide resolved
yaml = FormattedYAML()
yaml = FormattedYAML(
# Ansible only uses YAML 1.1, but others files should use newer 1.2 (ruamel.yaml defaults to 1.2)
version=(1, 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might just be me, but the conditional in here feels odd, it might be longer but can it go outside?

if file.is_owned_by_ansible():
  yaml = FormattedYAML(version=(1,1))
else:
  yaml = FormattedYAML(version=(1,2))

(somthing like that?)

or

if file.is_owned_by_ansible():
  version=(1,1)
else:
  version=(1,2)
yaml=FormattedYAML(version=version)

or

version = (1,1) if file.is_owned_by_ansible() else (1,2)
yaml=FormattedYAML(version=version)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There reason why I wanted to fallback to null was because that would make it make it future-proof for when ruamel.yaml will switch to versions such 1.3 or more.

What we want is to override the default for ansible file, and not touch it for others.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. We should stick with None as the default value.

Copy link
Contributor

@cognifloyd cognifloyd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The purpose of relying on the presence/absence of the _yaml_version* attributes was not clear to me. What about using a separate _strip_yaml_version_directive attribute to define that explicitly instead of implying it using hasattr?

Comment on lines +821 to +826
if version:
if isinstance(version, str):
x, y = version.split(".", maxsplit=1)
version = (int(x), int(y))
self._yaml_version_default: VersionType = version
self._yaml_version: VersionType = self._yaml_version_default
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if version:
if isinstance(version, str):
x, y = version.split(".", maxsplit=1)
version = (int(x), int(y))
self._yaml_version_default: VersionType = version
self._yaml_version: VersionType = self._yaml_version_default
if isinstance(version, str):
x, y = version.split(".", maxsplit=1)
version = (int(x), int(y))
self._yaml_version_default: VersionType | None = version
self._yaml_version: VersionType | None = self._yaml_version_default
self._strip_yaml_version_directive = version is not None

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not want to touch this right now, especially as I know that a newer patch of the library introduces some data that we might want to use, look at 0.18.4 changelog entries https://pypi.org/project/ruamel.yaml/

We can do extra polishing in follow-up, but I prefer to use whatever they have instead of setting new properties that have meaning only for our subclass. I need to play with it bit more too see what data I have available.

Comment on lines +936 to +938
if hasattr(self, "_yaml_version"):
return self._yaml_version
return None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if hasattr(self, "_yaml_version"):
return self._yaml_version
return None
return self._yaml_version

src/ansiblelint/yaml_utils.py Show resolved Hide resolved
Comment on lines +990 to +994
strip_version_directive = hasattr(self, "_yaml_version_default")
return self._post_process_yaml(
text,
strip_version_directive=strip_version_directive,
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
strip_version_directive = hasattr(self, "_yaml_version_default")
return self._post_process_yaml(
text,
strip_version_directive=strip_version_directive,
)
return self._post_process_yaml(
text,
strip_version_directive=self._strip_yaml_version_directive,
)

@ssbarnea ssbarnea merged commit 348c385 into main Nov 3, 2023
21 checks passed
@ssbarnea ssbarnea deleted the fix/gh-workflows branch November 3, 2023 12:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Archived in project
Development

Successfully merging this pull request may close these issues.

Make yaml reformatting compatible with github workflows (on keyword)
3 participants