Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cattrs-env

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cattrs-env

A tool for parsing and validating env vars using cattrs

  • 1.0.4
  • PyPI
  • Socket score

Maintainers
1

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)

Keywords

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc