Skip to content

Passing paths to python variables  #439

Closed

Description

@EthanMarx

🚀 Feature request

Given this simple parser setup, is it possible to parse values that are paths to python variables that resolve the the requested type?

parser = jsonargparse.ArgumentParser()
parser.add_argument("--float", type=float)
parser.parse_args(["--float", "math.pi"])

Motivation

Pitch

Alternatives

Activity

  1. mauvilsa commented on Jan 29, 2024

    @mauvilsa
    Owner

    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.

  2. EthanMarx commented on May 28, 2025

    @EthanMarx
    ContributorAuthor

    @mauvilsa is there any update on this? Or, is there a simple way to specify e.g. math.pi in a jsonargparse yaml config that will be parsed as a float?

  3. mauvilsa commented on Jul 26, 2025

    @mauvilsa
    Owner

    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:

    from jsonargparse import ArgumentParser
    from omegaconf import OmegaConf
    import math
    
    # define a custom resolver
    def const(expr: str):
        allowed = {"pi": math.pi, "e": math.e}
        # evaluate expression in a safe namespace
        return eval(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.

  4. mauvilsa commented on Aug 29, 2025

    @mauvilsa
    Owner

    In pull request #765 there is a new experimental implementation omegaconf+ that supports interpolation across config files and command line arguments.

  5. mauvilsa commented on Aug 29, 2025

    @mauvilsa
    Owner

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions