python-configuration icon indicating copy to clipboard operation
python-configuration copied to clipboard

Cast a config value to a python type

Open publicmatt opened this issue 1 year ago • 2 comments

It would be nice if there was a interface to cast config values to specific types when they get loaded.

I'm using this library to configure file paths alot and I'm finding I have to nest the config values a lot since they currently only return str, int and float:

from module import config # previously loaded ConfigurationSet
from pathlib import Path

my_func(Path(config.paths.train_features))

If the ConfigurationSet class is immutable, that casting would have to happen when it's created. I tried to interate through the values and cast them myself, but I couldn't figure it out.

publicmatt avatar Apr 09 '24 15:04 publicmatt

I'm having the same trouble with an env value that is a int, but when it's loaded into the config, it's a string. So I think there's a potential need being met by adding a cast to variables loaded in the config.

publicmatt avatar Apr 10 '24 21:04 publicmatt

Do you have an example of this? There are no specific casts forced by the library ; the data type is taken by the first element the ConfigurationSet sees. Is this a particular case of https://github.com/tr11/python-configuration?tab=readme-ov-file#caveats-1?

tr11 avatar Apr 11 '24 00:04 tr11