-
-
Notifications
You must be signed in to change notification settings - Fork 663
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
[Draft] Add support for nested interpolations in Override grammar #1594
[Draft] Add support for nested interpolations in Override grammar #1594
Conversation
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.
Something to discuss
# If it is an interpolation, then OmegaConf will take care of un-escaping | ||
# the `\\`. But if it is not, then we need to do it here. | ||
if not is_interpolation: | ||
ret = ret.replace("\\\\", "\\") |
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 wanted to discuss this point. It looks (and is) a bit clumsy but it's the best I have right now.
Consider the two following override values (here we really have 4 backslashes ending each quoted string, I'm assuming these are the actual characters after any Python un-escaping):
["abc\\\\", "def\\\\"]
["${abc}\\\\", "${def}\\\\"]
In both situations we would like the \\\\
to be turned into \\
(i.e., \\\\
== two consecutive escaped backslashes).
In case (1), if the content of each quoted string is kept unchanged (ex: abc\\\\
) then we'll end up with extra unwanted backslashes at the end of the string (since the intent here is to obtain abc\\
).
In case (2), if Hydra un-escapes the backslahes, then it will feed the string ${abc}\\
to OmegaConf, which will resolve it to <value_of_abc>\
(it will un-escape \\
into \
), so we would end up with only one \
at the end instead of two.
As a result, we need to know whether or not OmegaConf will process the string with its grammar, in order to decide whether or not to un-escape the \\
. I guess we could decide to ignore this and let the user figure it out, but this wouldn't seem intuitive.
@omry how do you feel about this?
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.
Note: this is a side effect of the fact that in OmegaConf, setting a value node to abc\\
will use this string unchanged, while ${abc}\\
will be resolved to <value_of_abc>\
.
One potential approach to resolve this issue would be the one from omry/omegaconf#621 where \\
is only un-escaped when required.
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 think we can merge the change "Quoted values are not valid dictionary keys anymore" independently (just need to add a news item).
Before jumping into the details here, let's answer this question:
Why should the Hydra override grammar be aware of interpolations now that the OmegaConf has an actual grammar than is handling them?
Yeah, don't look at this PR's diff yet, I'm splitting it into multiple independent components (#1599 is the first step) -- I just had this draft up to discuss the specific point about un-escaping
Two reasons:
|
Why?
I think nested interpolations in the override will be extremely rare. |
I'm not sure I understand your question -- it seems obvious to me that not knowing where an interpolation ends is going to cause trouble. For instance in
how would you know that it's a valid list unless you can tell where Here is an example where detecting the end gets trickier:
(I think) this is related to your other comment here: #1600 (comment) Do you believe it would be possible to properly parse the second example above with just regexes for quoted strings & interpolations? I don't see it myself (I mean, obviously it would be possible with this specific example, but more generally, you see the idea) Also, just to make sure it's clear, regarding the specific un-escaping of
|
With the emerging definition, I think you can't have unescaped quotes in the middle of an string. If you want:
You must quote the enclosing string and potentially escape it the inner quotes:
If you do need a comma, e.g a custom resolver:
Not in the current form.
Between the unescaping in the shell, in Hydra and in OmegaConf, no one will actually manage to use a quoted string in a nested interpolation in an override in the shell. It will just be too confusing. |
Currently unquoted strings don't allow
I'm not sure I follow, escaped quotes are for quotes inside quoted strings, but here I don't have any quote inside my quoted string. Based on what you wrote below though, I believe you're saying that I should be adding quotes around the interpolation:
In other words, any interpolation that can't be matched with the basic
I agree that the main goal is to make it possible to pass arbitrary values to OmegaConf, and it's ok if some niche use cases are tricky to get right. I hadn't thought of putting all tricky interpolations in quoted strings until you mentioned it now: I think it's a reasonable trade-off.
With what I had it should work as: 'foo="${concat:"a,b", c}, def"' which isn't that complex, but again, I agree it's ok to make it more complex if it means the underlying grammar/code is simpler. |
Yes.
I was thinking that we might be able to remove the INTERPOLATION token completely, but I think it's indeed conflicting with the dict syntax.
Great. |
What about deprecating interpolations in unquoted strings in 1.1? This way INTERPOLATION could be removed in 1.2, and it would also avoid some potential confusion regarding which interpolations are supported vs which aren't (in unquoted strings). |
Normally I would say yes to this, but because the primary usage is in the shell, it gets a bit confusing: # not a properly quoted interpolation
a='${b}'
# properly quoted interpolation
a='"${b}"'
# or
a='"${b}"' |
Alright -- not a big deal (any kind of interpolation on the command line is probably a niche use case though) |
Since quoted strings are enough to wrap any kind of interpolation, #1601 is all we need => closing this PR |
Still WIP, but I wanted to put it up to discuss something