-
Notifications
You must be signed in to change notification settings - Fork 121
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
Allow resolvers access to the config #266
Comments
Hi, want help? |
Yes. Backward compatibility can be tricky here: |
There is also the question of what to pass in?
|
Am I missing something maybe? |
I didn't test that code sample obviously :)
In [1]: from omegaconf import OmegaConf
In [2]: len(OmegaConf.create({"a": 10, "b": 20}))
Out[2]: 2
In [3]: len(OmegaConf.create([1,2,3]))
Out[3]: 3 |
This commit also fixes omry#266.
I needed this in #321 so I took a stab at it, see 9bb63d2 (look at the changes to Note that I got rid of the ResolverX protocol classes for typing, which didn't seem actually useful to me (and broke typing checks with the new signature of |
Hard to jump into that diff without properly reviewing the whole thing (which I did not do yet). ResolverX no longer make sense once we start supporting passing non-strings into resolver functions. |
Yeah it's a bit mixed up with other stuff, but for this issue you really only need to look at the changes to OmegaConf.register_resolver("len", lambda value, *, root: len(OmegaConf.select(root, value)), config_arg="root") (I'll add it to the tests) |
okay, that's pretty nice. I didn't think about using optional named arguments for this. |
Done in 70d6f4b (I used the parent instead of the node, because it was already available and I assume a common use case will be to access other options from the parent) Doing this made me realize that allowing access to the config doesn't play way with the idea of cached outputs. So I added a mechanism to disable the cache in f7afe4b, and used it to disable the cache for resolvers that use these new options (to access the config or the parent). In particular I disabled the cache for Initially I tried to make it possible to use the cache by using the whole config as key, however this doesn't work well because although configs are hashable, using them as dictionary keys triggers some equality comparisons between configs. And this leads to infinite recursion, presumably because it is trying to resolve interpolations to perform this equality (I haven't looked too deeply into it, as I don't want to stray away too far). |
The intention with the cache is to protect against a function call returning something unstable. This works as long as the function is stateless and does not depend on the config. Hashing the config might offer a way out, but it also sounds pretty expensive to do that on the call (even if we did solve the recursion issue). |
That's also what I was thinking -- taking note to create a follow-up issue once the PR is merged |
This commit also fixes omry#266.
Following up on this, in the end the version I originally wrote in #321 won't make it to master, so most of the comments above are now irrelevant. If anyone wants to pick up this issue, a good starting point is this comment, suggesting to add support for |
I think your changes are mostly addressing this via support for nested interpolations. |
Yes, nested interpolations definitely address most use cases. One potential reason to want access to the node rather than its value could be to resolve interpolations within the resolver itself (that's what my original re-implementation of Something that'd be easy to do would be to add a syntax to access the node object instead of its value. For instance, |
Value nodes are internal API, and container nodes are already accessible through normal interpolation. Generally, an advantage of supporting it at the API level as opposed to the input level is that the input is determined by the user of the resolver. not by the resolver author. at the same time I think nested interpolations will cover the important use cases. |
In progress in #445. |
* Add a grammar to parse interpolations * Add support for nested interpolations * Deprecate register_resolver() and introduce new_register_resolver() (that allows resolvers to use non-string arguments, and to decide whether or not to use the cache) * The `env` resolver now parses environment variables in a way that is consistent with the new interpolation grammar Fixes #100 #230 #266 #318
This is implemented in #599. See the docs for an example. |
Currently resolvers are not getting access to the config structure.
It will be helpful to allow it.
Example use case:
The text was updated successfully, but these errors were encountered: