headsrooms / configclasses

Like dataclasses but for config.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

All the factory classmethods *write* to environment

howardjones opened this issue · comments

I was just working on writing tests for a new feature (Required Fields) and I noticed that my tests were acting strange. Then I noticed that even if I load from a string or a JSON file, configclasses actually writes to the OS environment as well as to the configclass.

This makes the tests non-deterministic. Also, as a result, even if you load from a file, but have some of those same fields defined in your environment (e.g. USER) then the config object you end up with has a combination of the file contents and the environment contents.

All the loader functions should return a dict, and the from_environ should be the ONLY factory function that looks at os.environ...

This is the planned behaviour following the twelve-factors' principles: https://12factor.net/config

However, your use case makes sense. I'll see if I can do something about it.

Another option to you is to use the parameter "prefix", then only env vars with the specified prefix will be loaded: https://github.com/kingoodie/configclasses/blob/e9029c967105c192dc79c81913a3f1269353f763/tests/test_configclasses.py#L61

Hmm, really? I always read that as deliver the config to the app in the environment (like, e.g. docker would), so the app doesn't need to care where it came from. Not load all config inside the app into the environment. What's the config object for, if not that? I think I'll probably write a similar class.

Well, I think using the config object is good practice, but sometimes there are situations where it might be more appropriate or cheaper to use the env var, such as when using threads or multiprocessing.