A simple env loader, made to use type annotations
Project description
Typehintenv - Env loading using type annotations
The idea is simple : in one file (like an env.py), you define your constants, with the same name as your env variables and you give them a type. At the end of this file, you call the loader (as you would do with python-dotenv for example).
After that, all the constants in the env.py file are loaded, and you can import them from other modules, with your IDE knowing they exist, and what their type is.
Example
The most basic one
You have those variables in your environment
A=1.2
B=5
C=toto
D=true
You declare a file like env.py with
from typehintenv import load_env
A : float
B : int
C : str
D : bool
load_env()
And then, from the rest of your code, just use it like this
from .env import A, D
if D:
print(type(A))
# this code will show 'float'
With a dotenv
You can use python-dotenv to load a environment file, just ensure doing it before calling load_env, as you will need the os.environ loaded before the call.
Extending the converters
In the environment, everything is a string. When loading those into python, functions are used to changed from strings to the types you want.
load_env takes as a first argument a dictionary of converters : the keys are the types (T) and the values are functions that convert from strings to T.
This function can also perform validation, as they can raise ValueError if the values are not correct.
There is a small set of converters for the basic types of the language, you can override them.
from typing import Literal
from typehintenv import load_env
A : bool
ENV : Litteral["env", "prod"]
def parse_env(s : string) -> Litteral["env", "prod"]:
if s not in ("env", "prod"):
raise ValueError("Not a correct environment")
return s
load_env({
bool : lambda s: s in ("oui", "vrai"), # can't help wanting my booleans to be in french
Litteral["env", "prod"] : parse_env # now we have more of a validator
})
Acknowledgments
The work from (typenv)[https://pypi.org/project/typenv/] is quite the same, exclusing the fact that it does not use type annotations but rather uses it's own model.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file typehintenv-0.2.0.tar.gz.
File metadata
- Download URL: typehintenv-0.2.0.tar.gz
- Upload date:
- Size: 3.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c292935dcca8dc8f5786dc2dff4a0bc95802ee1020171ebe06dd36e24fe95b8a
|
|
| MD5 |
6de04cbb80a9d6dc516d7fbb6d4e75f7
|
|
| BLAKE2b-256 |
5defce8c4f9376ae105159582f6e7ba4918a7f4cf7020481d06dc3138948dadb
|
File details
Details for the file typehintenv-0.2.0-py3-none-any.whl.
File metadata
- Download URL: typehintenv-0.2.0-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ee5aa107bc36d0ca8ebda15ba5713ae300690741c0d7fc9d000506fbb5c81edb
|
|
| MD5 |
268918a85becfaa08b94ab461c8c0886
|
|
| BLAKE2b-256 |
5d6b0bf2d0e13cd7c6bc1cd480a018031f5cb3be1bd8b1bf4225811596e0fd0b
|