Hi Team! :kedro: What is the correct way to do ke...
# questions
a
Hi Team! K What is the correct way to do key interpolation when the key is an integer?
Copy code
variants:

  1:
    - f1
    - f2
  
  5: 
    - f7
    - 78


global: ${variants.1}
Referencing it as
${variants.1}
fails with the following error:
Copy code
InterpolationKeyError: Interpolation key 'variants.1' not found
    full_key: global
    object_type=dict
Changing 1 and 5 to strings solves it but explicitly want the keys to be integer. Note that specifying it as integer works fine, it's just when I reference the key somewhere, it errors out
Copy code
variants:

  "1":
    - f1
    - f2
  
  "5": 
    - f7
    - 78


global: ${variants.1}
Does anybody have solution? 🙂
j
Hi Abhishek, Thank you for asking your question here. I'm currently looking into the best way to handle this situation correctly. At the moment, I don't have a clear answer, but I will update this thread as soon as I have more information. Thank you for your patience.
a
Hey Abhishek, seems like this is not supported by
omegaconf
yet - https://github.com/omry/omegaconf/issues/864 The workaround is just what you mentioned, convert the keys to string!
👍 1
j
Hi Abhishek, As @Ankita Katiyar mentioned Unfortunately, OmegaConf does not natively support interpolation with integer keys (see this issue). As a workaround, you can use a custom resolver. Add the following to your `settings.py`:
Copy code
from omegaconf import OmegaConf

# <https://github.com/omry/omegaconf/blob/master/omegaconf/_impl.py#L62>
def select2(key: str, secondary_key, default=None, *, _parent_):
  return OmegaConf.to_container(_parent_[key], resolve=True)[secondary_key]

OmegaConf.register_new_resolver("select2", select2, replace=True)
Then, use this resolver in your YAML:
Copy code
variants:
 1:
  - f1
  - f2
 5:
  - f7
  - 78

global: ${select2:variants, 1}
Note: it's using
_parent_
which is a special OmegaConf internal that allow you to access the config tree within the resolver.
a
Thanks @Jitendra Gundaniya for explaining
_parent_
and for referencing the issue ✌️