-
Notifications
You must be signed in to change notification settings - Fork 667
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
Conversation
a528423
to
19eb58f
Compare
19eb58f
to
31fd04e
Compare
31fd04e
to
189a2c5
Compare
8172781
to
b58bdd5
Compare
b58bdd5
to
3f582b7
Compare
3f582b7
to
f98e611
Compare
f98e611
to
dffaf1b
Compare
adf6614
to
429c83b
Compare
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) |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this 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
?
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
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.
if hasattr(self, "_yaml_version"): | ||
return self._yaml_version | ||
return None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if hasattr(self, "_yaml_version"): | |
return self._yaml_version | |
return None | |
return self._yaml_version |
strip_version_directive = hasattr(self, "_yaml_version_default") | ||
return self._post_process_yaml( | ||
text, | ||
strip_version_directive=strip_version_directive, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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, | |
) |
Fixes: #3790