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

Omnibus 2024-11-27 #663

Open
wants to merge 32 commits into
base: main
Choose a base branch
from

Conversation

maddenp-noaa
Copy link
Contributor

@maddenp-noaa maddenp-noaa commented Nov 27, 2024

Synopsis

  • Update .github such that the Test workflow will be run for PRs or pushes to all branches. This means that tests will run for updates on release branches, which seems like it would have been a good idea to always be doing.
  • Add setuptools to conda build requirements, as it appears not to be automatically installed with Python 3.13 now in CI jobs, but is required for the uwtools build.
  • Some updated documentation about the !include tag.
  • Some trivial doc changes per make docs.
  • Fix a bug whereby Jinja2 expressions referencing tagged YAML values would render to strings including the tag as part of the string. The fix involves detecting tagged values and raising an exception to delay rendering the value until a later iteration of the dereferencing process, when the tagged value will have been converted to its simple type (int, bool, etc.). See below (and the new unit tests) for examples.

Given config.yaml

a: 2
b: 7
foo: !int "{{ a * b }}"
bar: !int "{{ foo }}"

Old behavior:

$ uw config realize -i config.yaml --output-format yaml
a: 2
b: 7
foo: 14
bar: !int '!int 14'

New behavior:

$ uw config realize -i config.yaml --output-format yaml
a: 2
b: 7
foo: 14
bar: 14

Type

  • Bug fix (corrects a known issue)
  • Documentation
  • Tooling (CI, code-quality, packaging, revision-control, etc.)

Impact

  • This is a non-breaking change (existing functionality continues to work as expected)

Checklist

  • I have added myself and any co-authors to the PR's Assignees list.
  • I have reviewed the documentation and have made any updates necessitated by this change.

@maddenp-noaa maddenp-noaa self-assigned this Nov 27, 2024
for k, v in val.items():
if isinstance(v, UWYAMLRemove):
_deref_debug("Removing value at", " > ".join([*keys, k]))
_deref_debug("Removing value at", ".".join([*keys, k]))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Show the keypath using dot notation, for consistency with other places where uwtools shows keypaths.

The rest of the changes in this function are cosmetic.

@@ -266,6 +269,8 @@ def _deref_render(val: str, context: dict, local: Optional[dict] = None) -> str:
context = {**(local or {}), **context}
try:
rendered = _register_filters(env).from_string(val).render(context)
if isinstance(yaml.safe_load(rendered), UWYAMLConvert):
raise UWConfigRealizeError(f"Rendering delayed: {rendered}")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If the rendered value parses to a UWYAMLConvert object (i.e. is prefixed with one of our !bool, !int, etc. tags), raise an exception to keep the original value intact for now; it may be possible to render it later after the value it references has itself been rendered and converted to a basic (bool, int, etc.) value.

print(dedent(config).strip(), file=f)
tools.realize_config(input_config=path_in, output_file=path_out)
with open(path_out, "r", encoding="utf-8") as f:
assert f.read().strip() == dedent(expected).strip()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

To keep the new tests -- see below -- DRY.

"""
loader = yaml.SafeLoader
loader = uw_yaml_loader()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The basic loader, handling tags !bool, !datetime, !float, and !int, has been factored out to uwtools.config.support so that it can be used by code outside YAMLConfig. This method obtains that loader, then adds !include support. It seemed far from straightforward to factor !include support out of YAMLConfig, and I think there are open questions about how the !include tag would interact with other features (e.g. an !include-tagged value that loads values tagged with other tags like !int et al, how these interact with Jinja2 value rendering, etc.) I think we'll have to cross that bridge when we come to it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did a basic test involving tagged values and !include and things seem to work as I'd expect:

numbers.yaml

e_plus_half_pi: !float '{{ values.e + half_pi }}'
half_pi: !float '{{ values.pi / 2 }}'
values: !include [e.yaml, pi.yaml]

e.yaml

e: 2.718

pi.yaml

pi: 3.141

And

$ uw config realize --input-file numbers.yaml --output-format yaml
e_plus_half_pi: 4.2885
half_pi: 1.5705
values:
  e: 2.718
  pi: 3.141

@@ -266,6 +269,9 @@ def _deref_render(val: str, context: dict, local: Optional[dict] = None) -> str:
context = {**(local or {}), **context}
try:
rendered = _register_filters(env).from_string(val).render(context)
if isinstance(yaml.load(rendered, Loader=uw_yaml_loader()), UWYAMLConvert):
_deref_debug("Held", rendered)
raise UWConfigRealizeError()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here's the crux of the bugfix: If the rendered value turns out to be something (e.g. !int '{{ foo }}') that loads as a UWYAMLConvert object, reject the rendered value, and hold the old value, by raising an exception. The value may be rendered by a future iteration of the dereferencing loop when the value it refers to has been reduced to a basic value (a bool, int, etc.).

@maddenp-noaa maddenp-noaa changed the title Omnibus 2024-11-2 Omnibus 2024-11-27 Nov 27, 2024
@maddenp-noaa maddenp-noaa marked this pull request as ready for review November 27, 2024 06:47
for tag_class in (UWYAMLConvert, UWYAMLRemove):
for tag in getattr(tag_class, "TAGS"):
loader.add_constructor(tag, tag_class)
return loader
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here's the loader that handles !bool, !int, etc. UW YAML tags.

docs/sections/user_guide/yaml/tags.rst Outdated Show resolved Hide resolved
Co-authored-by: Emily Carpenter <137525341+elcarpenterNOAA@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants