I understand that and why using eval is generally considered bad practice in most cases (see e.g. here). Related questions on config files and argparse don't use types. (1, 2)I needed to parse a number of command line arguments and decided to store the arguments, defaults and help strings in a .json file. I read the .json and add every argument to an argparse.ArgumentParser. To specify a type I need to pass a callable like float and not the string float. To get from the string to the callable I thought ofusing evalusing a dict to map from string to callableusing a if/else or switchand decided to use to use eval to avoid hard coding all types.I have no security concerns because the argument file is supplied by the user and the program that uses this code is targeted at scientists that will realistically want to change the file to add parameters or change defaults. (Also, it is a university project which will never be run except for grading. I handed in a version using eval.)Is there a smart solution avoiding hardcoding all types and avoiding eval, or did I find a place where eval is a sensible choice? I was only allowed to use the standard library.Minimal args.json:{    dt: {        type: float,        help: Time step size,        default: 0.4},    T: {        type: int,        help: Time steps between outputs,        default: 50},    f: {        type: str,        help: Landscape file name,        default: small.dat}}Runnable code, put args.json above in same directory to run:import jsonimport argparseimport pprintdef setup_parser(arguments, title):    parser = argparse.ArgumentParser(description=title,                                     formatter_class=argparse.ArgumentDefaultsHelpFormatter)    for key, val in arguments.items():        parser.add_argument('-%s' % key,                            type=eval(val[type]),                            help=val[help],                            default=val[default])    return parserdef read_params(parser):    parameters = vars(parser.parse_args())    return parametersdef get_parameters(title=None):    with open(args.json) as data_file:        data = json.load(data_file)    parser = setup_parser(data, title)    parameters = read_params(parser)    return parametersif __name__ == __main__:    params = get_parameters()    pprint.pprint(params)
Using argparse with parameters defined in config file
python;parsing;json
I don't like the eval here anywhere for the usual reasons. I think you have two better options here:Find the right type within all builtin typesSee https://stackoverflow.com/a/3222774/620382 (use builtins for python3)Use the type of the default value from json.Json types are properly translated to python. type(val['default']) will provide this type. You can check with __name__ == val['type'] if you want to keep the redundancy. Of course you then should make sure to use default: 0.0 if the value should be float. 