Skip to main content

cattrs-env

Tests + Linting pypi License Downloads

cattrs-env is an Environment Variable parser/validator which utilizes the cattrs library.

cattrs-env parses Environment Variables from os.environ and structures them into a cattrs compatible dataclass. Providing you with easy and type safe environment variables in your project.

Because cattrs-env gets the Environment Variables from os.environ, it is fully compatible with any Environment Variable loading library such as python-dotenv.

from dataclasses import dataclass
from typing import List, Optional
from cattrs_env import CattrsEnv

@dataclass
class Env(CattrsEnv):
    A: int
    C: List[str]
    B: Optional[float] = None

env = Env.load()

env.A
env.C
env.B

Examples

dataclass example

import os
from dataclasses import dataclass
from typing import List, Optional

from cattrs_env import CattrsEnv


@dataclass
class Env(CattrsEnv):
    A: int
    C: List[str]
    B: Optional[float] = None


if __name__ == "__main__":
    os.environ.update(
        {
            "A": "99",
            "B": "9.9",
            "C": '["a","b","c", 1]',
        }
    )
    env = Env.load()
    print(env)

attrs example

import os
from typing import List, Optional
import attrs
from cattrs_env import CattrsEnv


@attrs.define
class Config:
    E: str
    F: Optional[int] = None


@attrs.define
class Env(CattrsEnv):
    A: int
    C: List[str]
    D: Config
    B: Optional[float] = None


if __name__ == "__main__":
    os.environ.update(
        {
            "A": "99",
            "B": "9.9",
            "C": '["a","b","c", 1]',
            "D": """{'E':"abcdef"}""",
        }
    )
    env = Env.load()

python-dotenv example

from dataclasses import dataclass
from typing import List, Optional
import dotenv
from cattrs_env import CattrsEnv


@dataclass
class Env(CattrsEnv):
    A: int
    C: List[str]
    B: Optional[float] = None


ENV_FILE_CONTENTS = """
A=1
B=99.9
C="['foo', 'bar']"
"""

if __name__ == "__main__":
    with open(".env", "w") as env_file:
        env_file.write(ENV_FILE_CONTENTS)
    dotenv.load_dotenv()
    env = Env.load()
    print(env)

Release files for cattrs-env 1.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cattrs-env 1.0.4
File Size Uploaded
cattrs-env-1.0.4.tar.gz 4.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cattrs-env 1.0.4
File Interpreter ABI Platform
cattrs_env-1.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 7.9 kB

Release files / cattrs-env-1.0.4.tar.gz

Download URL cattrs-env-1.0.4.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
518106ae678d697e759a03db630296232e6134fc71aaa5ba6932668c5e722c3d
BLAKE2b-256 checksum
How to use checksums
c7f25cc47839a8dc804da9eebd80b14be66f2f0dfb7ee03a78d6f129066e75c6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release files / cattrs_env-1.0.4-py3-none-any.whl

Download URL cattrs_env-1.0.4-py3-none-any.whl
Size 3.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5084d5eae27a6178fec0d7d4c3921a7232d2b3a83d3f046e1ba6917b5ccd60d3
BLAKE2b-256 checksum
How to use checksums
03bdeb39545f271b32a111b51725995fccdf6140e033fab0a311bcf1ced345e3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release history Release notifications | RSS feed

This release

1.0.4 This release

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page