https://github.com/timelessnesses/typed_env
A python module that help you have a type safety on enviroment variable
https://github.com/timelessnesses/typed_env
enviroment environment-variables python3 typing
Last synced: about 1 year ago
JSON representation
A python module that help you have a type safety on enviroment variable
- Host: GitHub
- URL: https://github.com/timelessnesses/typed_env
- Owner: timelessnesses
- License: mit
- Created: 2023-04-29T06:53:16.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-03-01T19:41:20.000Z (over 2 years ago)
- Last Synced: 2025-04-17T01:37:03.380Z (about 1 year ago)
- Topics: enviroment, environment-variables, python3, typing
- Language: Python
- Homepage: https://pypi.org/project/timelessnesses-typed-env
- Size: 26.4 KB
- Stars: 19
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# typed-env
A python module that help you have a type safety on enviroment variable (on runtime)
## Documentation
```python
timelessnesses.TypedEnv
```
A parent class that will help you ensure type safetiness on your enviroment variable.
Usage:
```python
from timelessnesses import typed_env
import datetime
class MyDotEnv(typed_env.TypedEnv):
# define your enviroment variable name and it's type (default value is optional and typing.Optional is also supported)
AMONG_US: bool = True
DISCORD_TOKEN: str
DATETIME: datetime.datetime = datetime.datetime.now()
NICE_DICTIONARY: typing.Dict[str, int] = {"a": 1, "b": 2}
DAMN_LIST: typing.List[int] = [1, 2, 3]
BALLS_KIND: BallsEnum # oh no! TypedEnv doesn't support my custom class!
# don't worry you can implement your own converter!
a = MyDotEnv()
a.add_validator(BallsEnum, lambda x: BallsEnum(int(x)))
a.get_env(typed_env.Method.dotenv, dotenv=".env") # you have options to either get only from dotenv or os.environ or both!
"""
a.get_env(typed_env.Method.all, dotenv="path_to_.env") # this fetch all the variable from both dotenv and os.environ
a.get_env(typed_env.Method.env) # this fetch all the variable from os.environ
a.get_env(typed_env.Method.dotenv, dotenv="path_to_.env") # this fetch all the variable from dotenv
NOTE: for dotenv/all method you have to supply dotenv argument
"""
a.raise_error_on_unknown_env(False) # if this set to true any excessive enviroment variable will raise an error (default is True)
a.load() # let it do the work!
```
`TypedEnv` supports normal types like `str` or `int` and also `typing.Dict` and `typing.List` etc. But it also supports custom type by adding a validator, you are also allowed to overwrite the default validator by using `TypedEnv.add_validator` method.
Check out more examples at `tests` folder!