-
Notifications
You must be signed in to change notification settings - Fork 24
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
base: main
Are you sure you want to change the base?
Omnibus 2024-11-27 #663
Conversation
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])) |
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.
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.
src/uwtools/config/jinja2.py
Outdated
@@ -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}") |
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 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() |
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.
To keep the new tests -- see below -- DRY.
""" | ||
loader = yaml.SafeLoader | ||
loader = uw_yaml_loader() |
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 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.
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 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() |
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.
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.).
for tag_class in (UWYAMLConvert, UWYAMLRemove): | ||
for tag in getattr(tag_class, "TAGS"): | ||
loader.add_constructor(tag, tag_class) | ||
return loader |
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.
Here's the loader that handles !bool
, !int
, etc. UW YAML tags.
Co-authored-by: Emily Carpenter <137525341+elcarpenterNOAA@users.noreply.github.com>
Synopsis
.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.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 theuwtools
build.!include
tag.make docs
.int
,bool
, etc.). See below (and the new unit tests) for examples.Given
config.yaml
Old behavior:
New behavior:
Type
Impact
Checklist