Creates a class used to query environmental variables with typehinting a conversion to basic Python types.
Project description
EnvProxy
EnvProxy is a Python package that provides a convenient proxy for accessing environment variables with type hints,
type conversion, and customizable options for key formatting. It alos includes EnvConfig, which lets you define
configuration classes that map directly to environment variables. With EnvConfig, you can declaratively
describe your environment-based configuration, including defaults, type hints,
and optional sample .env file generation.
Installation
To install EnvProxy, use standard package management tools for Python:
# Using pip
pip install env-proxy
# Using poetry
poetry add env-proxy
Usage
Basic Usage with EnvProxy
Start by creating an EnvProxy instance with optional configuration for environment variable key transformations:
from env_proxy import EnvProxy
proxy = EnvProxy(prefix="MYAPP")
The prefix option adds a prefix to all keys, allowing you to group related variables under a common namespace.
For example, with prefix="MYAPP", proxy.get_any("var") will look for the environment variable MYAPP_VAR.
See the Configuration Options for EnvProxy section for more options.
Retrieving Environment Variables
Each method returns the value of an environment variable, converting it to the specified type. If the variable is missing, it either raises an error or returns the provided default.
Methods
get_any
Retrieve the raw value of a variable as Any. If the key does not exist, ValueError is raised
unless a default is provided.
# export MYAPP_VAR="value"
value = proxy.get_any("var") # returns "value"
get_bool
Retrieve a boolean variable. The following values are considered truthy (case-insensitive):
yes, true, 1, on, enable, enabled, allow
Similarly, common falsy values are handled:
no, false, 0, off, disable, disabled, disallow, deny
# export MYAPP_ENABLED="true"
value = proxy.get_bool("enabled") # returns True
get_str
Retrieve a string variable.
# export MYAPP_NAME="example"
name = proxy.get_str("name") # returns "example"
get_int
Retrieve an integer variable.
# export MYAPP_COUNT="42"
count = proxy.get_int("count") # returns 42
get_float
Retrieve a floating-point variable.
# export MYAPP_RATIO="3.14"
ratio = proxy.get_float("ratio") # returns 3.14
get_list
Retrieve a list of strings by splitting the variable’s value based on a separator (default is ,).
# export MYAPP_ITEMS="a,b,c ,d"
items = proxy.get_list("items") # returns ["a", "b", "c", "d"]
get_json
Parse a JSON string from the environment.
# export MYAPP_CONFIG='{"key": "value"}'
config = proxy.get_json("config") # returns {"key": "value"}
EnvConfig – Declarative Configuration with Fields
The new EnvConfig class allows you to define environment-based configuration with type hints, descriptions,
and defaults. It automatically connects fields to environment variables using a declarative approach, and can
even generate a sample .env file for easy setup.
Defining Configuration Classes with EnvConfig
Define your configuration by subclassing EnvConfig and using Field factory to describe each variable.
The Field function supports attributes like description, default, and type_hint
(see Field Options).
from env_proxy import EnvConfig, Field, EnvProxy
class MyConfig(EnvConfig):
env_proxy = EnvProxy(prefix="MYAPP") # common prefix for all fields
debug: bool = Field(description="Enable debug mode", default=False)
database_url: str = Field(description="Database connection URL")
max_connections: int = Field(description="Maximum DB connections", default=10)
cache_backends: list[str] = Field(description="Cache backends", type_hint="list")
Accessing Config Values
Once defined, MyConfig provides easy access to each environment variable with the specified type conversions.
config = MyConfig()
# Access configuration values
debug = config.debug # Looks for MYAPP_DEBUG in the environment
database_url = config.database_url # Raises ValueError if not found
Overriding Values per Instance
EnvConfig accepts keyword arguments to override individual fields on a per-instance basis.
Overrides take precedence over the environment, letting you layer the env-derived config with
values from any other source — a config file, CLI arguments, programmatic wiring, fixtures —
without touching os.environ.
class AppConfig(EnvConfig):
env_proxy = EnvProxy(prefix="APP")
timeout: int = Field(default=30)
services: list[str] = Field(default=[])
# Layer env with values loaded from a config file:
file_config = load_yaml("app.yaml") # {"timeout": 5, "services": ["redis", "rabbitmq"]}
cfg = AppConfig(**file_config)
assert cfg.timeout == 5
assert cfg.services == ["redis", "rabbitmq"]
Semantics:
- Keys are Python field names (not env-var keys), regardless of any
aliasorenv_prefix. - Values are used as-is — no string parsing or type conversion. Pass real
int,list,dict, etc. - Overrides shadow the environment for reads on that instance only; other instances and direct
os.environaccess are unaffected. - Unknown override keys raise
ValueError, listing the valid field names — typo-proof. - Fields with
allow_set=Falsecan be initialized via override but cannot be reassigned afterwards; theallow_setcontract is unchanged. - For fields with
allow_set=True, assignment after construction updates both the override entry andos.environ(preserving the existing side-effect contract).
Overrides are statically type-checked. EnvConfig is decorated with PEP 681's dataclass_transform,
so mypy and Pyright/Pylance synthesize a typed __init__ from each subclass's annotated fields:
typos (AppConfig(timout=5)) and wrong value types (AppConfig(timeout="bad")) are flagged at
type-check time, and IDEs autocomplete field names with their declared types.
Generating a Sample .env File
You can export a sample .env file from your EnvConfig class, which documents all fields with their
descriptions, types, and default values.
MyConfig.export_env("sample.env", include_defaults=True)
This would produce a file like:
# debug (bool) [optional]
# Enable debug mode
MYAPP_DEBUG=False
# database_url (str) [required]
# Database connection URL
MYAPP_DATABASE_URL=
# max_connections (int) [optional]
# Maximum DB connections
MYAPP_MAX_CONNECTIONS=10
# cache_backends (list) [required]
# Cache backends
MYAPP_CACHE_BACKENDS=
Field Options
Each Field can be customized with the following options:
alias: Custom name in the environment. Defaults to the field name.description: Description of the variable.default: Default value if the variable is missing. If UNSET, the variable is required.type_hint: Specify the type explicitly (e.g., json for JSON objects).env_prefix: Override the env_prefix set on the EnvConfig class for a specific field.allow_set: Allow modification of the environment variable value at runtime.
Field Type Hints
The following type_hint values are supported:
anyboolfloatintstrlistjson
Example of using type_hint:
class AdvancedConfig(EnvConfig):
settings: dict[str, Any] = Field(type_hint="json", description="Complex JSON settings")
Example Usage with EnvConfig
import os
from env_proxy import EnvConfig, Field
# Set environment variables
os.environ["MYAPP_DEBUG"] = "true"
os.environ["MYAPP_DATABASE_URL"] = "sqlite:///data.db"
os.environ["MYAPP_CACHE_BACKENDS"] = "redis,memcached"
class MyConfig(EnvConfig):
env_prefix: str = "MYAPP"
debug: bool = Field(description="Enable debug mode", default=False)
database_url: str = Field(description="Database connection URL")
cache_backends: list[str] = Field(description="Cache backends", type_hint="list")
config = MyConfig()
# Access configuration values
print(config.debug) # True
print(config.database_url) # "sqlite:///data.db"
print(config.cache_backends) # ["redis", "memcached"]
# Export a sample .env file
MyConfig.export_env("sample.env", include_defaults=True)
Configuration Options for EnvProxy
You can control how keys are transformed when retrieving variables in EnvProxy:
prefix: Adds a prefix to all keys.uppercase: Converts keys to uppercase.underscored: Replaces hyphens with underscores.
proxy = EnvProxy(prefix="myapp", uppercase=True, underscored=False)
proxy.get_any("var") # Looks for "MYAPP_VAR"
Error Handling
If a variable is not found, and no default value is provided, a ValueError will be raised.
Each method also raises a ValueError for invalid conversions.
try:
missing_value = proxy.get_int("missing_key")
except ValueError as e:
print(e) # Output: No value found for key 'missing_key' in the environment.
License
EnvProxy is open-source and distributed under the MIT License. See LICENSE.md for more information.
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 env_proxy-1.3.0.tar.gz.
File metadata
- Download URL: env_proxy-1.3.0.tar.gz
- Upload date:
- Size: 14.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.4.1 CPython/3.14.4 Linux/6.17.0-1010-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2e2a7a45e867e21b82f140afa9cc0e033a6ca1052f2600e0fbd90e243e6dfc57
|
|
| MD5 |
01a5d0e00fbf2d04bf45e065f9588640
|
|
| BLAKE2b-256 |
c66337b2256688c8acb972ab9b92d7bf77ff7b8cf84c86652d9cde489a6efed0
|
File details
Details for the file env_proxy-1.3.0-py3-none-any.whl.
File metadata
- Download URL: env_proxy-1.3.0-py3-none-any.whl
- Upload date:
- Size: 12.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.4.1 CPython/3.14.4 Linux/6.17.0-1010-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a06557b13df36ffc69086a0004781f77af08d617e4e777dd24b04495115b8bd5
|
|
| MD5 |
ec630e3cf5198796e165e9e4ee21d8b3
|
|
| BLAKE2b-256 |
ea5edc1f35ab778cdef26f72f019078306e1176215b9c893fb41e4e0baac2719
|