You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Thank you for the proposal! It is an interesting idea. I need to think about it to see if it could have unexpected consequences. One small comment for now. One thing is that when parsing, an import path resolves to its corresponding value. This might be relatively easy to implement.
A more complex behavior that people could ask for could be that additionally when serializing (dump, save, print_config), the import path is preserved. This would be considerably complex an not be implemented any time soon.
I am not sure if it justifies investing effort in implementing this. If the feature request includes being able to serialize back into the variable path, then it is rather complex. And if it is only for parsing, then the use is quite limited since there aren't many predefined constants to choose from. And just thinking of pi I would imagine very likely people would also want fractions of pi.
One option which is more general could be to use an OmegaConf resolver, that is:
fromjsonargparseimportArgumentParserfromomegaconfimportOmegaConfimportmath# define a custom resolverdefconst(expr: str):
allowed= {"pi": math.pi, "e": math.e}
# evaluate expression in a safe namespacereturneval(expr, {"__builtins__": None}, allowed)
OmegaConf.register_new_resolver("const", const)
parser=ArgumentParser(parser_mode="omegaconf")
parser.add_argument("--config", action="config")
parser.add_argument("--value", type=float)
print(parser.parse_args())
With this, you could have in a config file value: ${const:pi} and also fractions value: ${const:3*pi/4}. Though, this only works in config files, not when given through argument --value.
If you want actual import paths to variables, you could also implement it with an omegaconf resolver. So I will be marking this issue to be resolved by #765.
Activity
mauvilsa commentedon Jan 29, 2024
Thank you for the proposal! It is an interesting idea. I need to think about it to see if it could have unexpected consequences. One small comment for now. One thing is that when parsing, an import path resolves to its corresponding value. This might be relatively easy to implement.
A more complex behavior that people could ask for could be that additionally when serializing (
dump,save,print_config), the import path is preserved. This would be considerably complex an not be implemented any time soon.EthanMarx commentedon May 28, 2025
@mauvilsa is there any update on this? Or, is there a simple way to specify e.g.
math.piin a jsonargparse yaml config that will be parsed as a float?mauvilsa commentedon Jul 26, 2025
I am not sure if it justifies investing effort in implementing this. If the feature request includes being able to serialize back into the variable path, then it is rather complex. And if it is only for parsing, then the use is quite limited since there aren't many predefined constants to choose from. And just thinking of
piI would imagine very likely people would also want fractions ofpi.One option which is more general could be to use an OmegaConf resolver, that is:
With this, you could have in a config file
value: ${const:pi}and also fractionsvalue: ${const:3*pi/4}. Though, this only works in config files, not when given through argument--value.mauvilsa commentedon Aug 29, 2025
In pull request #765 there is a new experimental implementation
omegaconf+that supports interpolation across config files and command line arguments.mauvilsa commentedon Aug 29, 2025
If you want actual import paths to variables, you could also implement it with an omegaconf resolver. So I will be marking this issue to be resolved by #765.