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.
Release files for typehintenv 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| typehintenv-0.2.0.tar.gz | 3.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| typehintenv-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 7.9 kB
Release files / typehintenv-0.2.0.tar.gz
| Download URL | typehintenv-0.2.0.tar.gz |
|---|---|
| Size | 3.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c292935dcca8dc8f5786dc2dff4a0bc95802ee1020171ebe06dd36e24fe95b8a
|
|
BLAKE2b-256 checksum How to use checksums |
5defce8c4f9376ae105159582f6e7ba4918a7f4cf7020481d06dc3138948dadb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.7.8
|
Release files / typehintenv-0.2.0-py3-none-any.whl
| Download URL | typehintenv-0.2.0-py3-none-any.whl |
|---|---|
| Size | 4.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ee5aa107bc36d0ca8ebda15ba5713ae300690741c0d7fc9d000506fbb5c81edb
|
|
BLAKE2b-256 checksum How to use checksums |
5d6b0bf2d0e13cd7c6bc1cd480a018031f5cb3be1bd8b1bf4225811596e0fd0b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.7.8
|