-
Notifications
You must be signed in to change notification settings - Fork 5
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
Modified recursive dictionary parsing to correctly handle lists in dvc plot templates #134
Conversation
src/dvc_render/vega_templates.py
Outdated
return True | ||
if isinstance(v, list): | ||
return any(dict_find_value(e, value) for e in v) | ||
def dict_find_value(d: Union[dict, str], value: str) -> bool: |
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 generalize: can it be a list
also? E.g. in case of some nested lists.
Thanks for the fix @alexk101 ! It looks reasonable to me, we can keep the name also (it's still trying to find a name). Can we add some tests for this please? |
@alexk101 Thanks for catching this and the changes look good! Since I was also working on this recently (causing this bug), my feeling was that this felt a bit complicated just to check for the anchor, and it feels even more complicated now. An alternative approach would be to search the raw text for dvc-render/src/dvc_render/vega_templates.py Lines 691 to 693 in 635eaff
You could replace that with something like: with _open(template_path, encoding="utf-8") as f:
raw = f.read()
content = json.loads(raw)
template = Template(content, name=template)
anchor = template.anchor("data")
if anchor not in raw:
raise BadTemplateError(
f"Template '{template.name}' "
f"is not using '{anchor}' anchor"
)
return template Then we could drop Let me know if that makes sense. The approach you have also works if we have some better tests to avoid a bug like the one I introduced. |
Extend tests.
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## main #134 +/- ##
==========================================
- Coverage 96.26% 96.03% -0.24%
==========================================
Files 19 19
Lines 777 781 +4
Branches 125 129 +4
==========================================
+ Hits 748 750 +2
- Misses 16 17 +1
- Partials 13 14 +1
☔ View full report in Codecov by Sentry. |
Actually, I think that we should be validating more strictly. |
Previously, the way in which lists were parsed from dvc templates caused the
dict_find_value
function to end early without continuing to parse the rest of the template once it encountered a list. Additionally, the structure of vega-lite plots when using lists allows for the case of a list of strings. Because of this, when called recursively as was done in 635eaff , the function may fail when it encounters something like a list of strings, as it tries to access the values of d, which is no longer a dictionary, but a string. I have fixed this by allowing ford
to be a string, but this may not be desired/sensible since the function is calleddict_find_value
. It may make sense to change the name of the function or to call another function to handle the string case.I include a template which previously would not parse as reference for the changes made.