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

Support legacy ruamel.yaml loaders #1060

Merged
merged 2 commits into from
Jun 26, 2021
Merged

Conversation

ischoegl
Copy link
Member

@ischoegl ischoegl commented Jun 14, 2021

Changes proposed in this pull request

A recent change in #1049 replaces a deprecated YAML loader. However, the replacement loader was only implemented for the most recent ruamel.yaml versions.

  • Support ruamel.yaml prior to 0.17.0

If applicable, fill in the issue number this pull request is fixing

Fixes #1059

If applicable, provide an example illustrating new features this pull request is introducing

Checklist

  • The pull request includes a clear description of this code change
  • Commit messages have short titles and reference relevant issues
  • Build passes (scons build & scons test) and unit tests address code coverage
  • Style & formatting of contributed code follows contributing guidelines
  • The pull request is ready for review

Comment on lines 1378 to 1386
try:
yaml_ = yaml.YAML(typ="rt")
with open(path, 'rt', encoding="utf-8") as stream:
yml = yaml_.load(stream)
except yaml.constructor.ConstructorError:
with open(path, "rt", encoding="utf-8") as stream:
# Ensure that the loader remains backward-compatible with legacy
# ruamel.yaml versions (prior to 0.17.0).
yaml.round_trip_load(stream)
Copy link
Member

Choose a reason for hiding this comment

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

I'd suggest passing a pathlib.Path instance directly to load(), rather than using the context manager. I'm not sure what version started supporting that, but hopefully something that's recent enough...

Also, in the last line of the except clause, you don't assign the return value to a variable.

Copy link
Member Author

Choose a reason for hiding this comment

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

whoops - fixed the assignment. Regarding pathlib.Path, I don't think that the function receives the correct argument (from what I see it's a string). For the time being, I hope to just fix #1059 as I inadvertently broke the build for older ruamel.yaml versions - getting rid of os in ck2yaml is a slightly larger project (it currently doesn't use pathlib at all).

Comment on lines 21 to 31
def load_yaml(yml_file):
# Load YAML data from file using the "safe" loading option.
try:
yaml_ = yaml.YAML(typ="safe")
with open(yml_file, "rt", encoding="utf-8") as stream:
return yaml_.load(stream)
except yaml.constructor.ConstructorError:
with open(yml_file, "rt", encoding="utf-8") as stream:
# Ensure that the loader remains backward-compatible with legacy
# ruamel.yaml versions (prior to 0.17.0).
yaml.safe_load(stream)
Copy link
Member

Choose a reason for hiding this comment

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

Same 2 comments here.

One additional comment is, why remove the type argument?

Copy link
Member Author

@ischoegl ischoegl Jun 14, 2021

Choose a reason for hiding this comment

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

For simplicity - all of the tests use the safe loader at the moment.

Upstream ruamel.yaml changes loaders in 0.17.0, with earlier loaders being
deprecated. However, the replacement loaders were introduced with the same
version. This update ensures that both loaders are being supported.
@ischoegl
Copy link
Member Author

ischoegl commented Jun 23, 2021

@bryanwweber … any further comments? As mentioned, I’d prefer to not start switching to pathlib as this should be a simple fix. Regarding getting rid of the keyword parameter, it wasn’t used and I didn’t want to introduce an if-else tree for an unused feature that is deprecated.

@bryanwweber bryanwweber merged commit 74293ee into Cantera:main Jun 26, 2021
@ischoegl ischoegl deleted the fix-dated-ruamel branch June 26, 2021 17:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ruamel.yaml new API implementation broke
2 participants