Using a dataclass to specify a set of environment variables and am trying to keep the dataclass separate from a class that modifies the environment. Passing in an instance of the dataclass and have a get_env
that determines if those variables are set in the environment. When os.getenv(field.name.upper(), None)
is called I want None
to be type as specified in the dataclass. e.g. if field.type
is a str
I want something like os.getenv(field.name.upper(), field.type.__new__(field.type))
This seems to work but would like to confirm this makes sense or what alternatives to consider?
class Env:
def __init__(self, config: Config):
self.config = config
def get_env_dict(self):
return self.config.to_dict()
def get_env(self):
env_dict = self.get_env_dict()
for field in fields(self.config):
env_dict[field.name.upper()] = os.getenv(field.name.upper(), None)