### Mypy Plugin Configuration for typed-envs Source: https://context7.com/bobthebuidler/typed-envs/llms.txt Provides configuration examples for enabling the typed-envs mypy plugin in both `mypy.ini` and `pyproject.toml` files. This plugin enhances mypy's type inference capabilities for `EnvironmentVariable[T]` types. ```ini # mypy.ini configuration [mypy] plugins = typed_envs.mypy_plugin ``` ```toml # pyproject.toml configuration [tool.mypy] plugins = ["typed_envs.mypy_plugin"] ``` -------------------------------- ### Register Custom Type Converters with EnvVarFactory Source: https://context7.com/bobthebuidler/typed-envs/llms.txt Explains how to use `register_string_converter` on `EnvVarFactory` to define default conversion logic for custom types. Once registered, these converters are automatically applied when creating environment variables of the specified type, eliminating the need to repeatedly provide `string_converter` arguments. Examples include registering converters for `Decimal` and `Path`. ```python from typed_envs import EnvVarFactory from decimal import Decimal from pathlib import Path factory = EnvVarFactory("APP") # Register a converter for Decimal type factory.register_string_converter(Decimal, Decimal) # Register a converter for Path type factory.register_string_converter(Path, Path) # Now create env vars without specifying string_converter each time price_limit = factory.create_env("PRICE_LIMIT", Decimal, Decimal("99.99")) # If APP_PRICE_LIMIT="150.50" in environment, price_limit will be Decimal("150.50") config_path = factory.create_env("CONFIG_PATH", Path, Path("/etc/app/config")) # If APP_CONFIG_PATH="/home/user/config" in environment, config_path will be Path("/home/user/config") # Check registered converters print(factory.default_string_converters) ``` -------------------------------- ### Configure Mypy Plugin for Typed Envs (TOML) Source: https://github.com/bobthebuidler/typed-envs/blob/master/README.md Demonstrates how to enable the typed_envs Mypy plugin using a pyproject.toml file. This configuration is common in modern Python projects for managing build and tool settings. ```toml [tool.mypy] plugins = ["typed_envs.mypy_plugin"] ``` -------------------------------- ### Configure Mypy Plugin for Typed Envs (INI) Source: https://github.com/bobthebuidler/typed-envs/blob/master/README.md Shows how to enable the typed_envs Mypy plugin in a standard INI configuration file. This plugin helps Mypy understand the dynamic subclassing performed by typed_envs. ```ini [mypy] plugins = typed_envs.mypy_plugin ``` -------------------------------- ### Create Typed Environment Variables with typed-envs Source: https://context7.com/bobthebuidler/typed-envs/llms.txt Demonstrates the core `create_env` function for defining typed environment variables. It shows how to create variables for integers, strings, and booleans, including setting default values and using custom string converters for complex types like `asyncio.Semaphore`. The variables behave like their underlying types while providing enhanced debugging information. ```python import typed_envs from typed_envs import EnvironmentVariable import asyncio # Create an integer environment variable with default value max_connections = typed_envs.create_env("MAX_CONNECTIONS", int, 10) # The variable works exactly like an int print(max_connections + 5) # Output: 15 print(max_connections * 2) # Output: 20 print(isinstance(max_connections, int)) # Output: True print(isinstance(max_connections, EnvironmentVariable)) # Output: True # Enhanced repr shows contextual information print(repr(max_connections)) # Output: EnvironmentVariable[int](name=`MAX_CONNECTIONS`, default_value=10, using_default=True) # Create a string environment variable api_url = typed_envs.create_env("API_URL", str, "https://api.example.com") print(api_url.upper()) # Output: HTTPS://API.EXAMPLE.COM print(api_url.startswith("https")) # Output: True # Create a boolean environment variable # Recognizes "0", "false", "False", "FALSE" as False; anything else is True debug_mode = typed_envs.create_env("DEBUG_MODE", bool, False) if debug_mode: print("Debug mode enabled") # Use string_converter for complex types semaphore = typed_envs.create_env( "SEMAPHORE_LIMIT", asyncio.Semaphore, default=10, string_converter=int # Converts env string to int before passing to Semaphore ) print(hasattr(semaphore, "acquire")) # Output: True ``` -------------------------------- ### Create Prefixed Environment Variables with EnvVarFactory Source: https://context7.com/bobthebuidler/typed-envs/llms.txt Illustrates the use of `EnvVarFactory` to create environment variables with a common prefix, simplifying configuration for related settings. It demonstrates creating a factory with a prefix and then using it to define variables, showing how the prefix is automatically prepended to the variable names. It also shows creating a factory without a prefix, which behaves identically to `typed_envs.create_env`. ```python from typed_envs import EnvVarFactory, EnvironmentVariable # Create a factory with prefix "MYAPP" factory = EnvVarFactory("MYAPP") # Creates env var named "MYAPP_DATABASE_HOST" db_host = factory.create_env("DATABASE_HOST", str, "localhost") print(repr(db_host)) # Output: EnvironmentVariable[str](name=`MYAPP_DATABASE_HOST`, default_value=localhost, using_default=True) # Creates env var named "MYAPP_DATABASE_PORT" db_port = factory.create_env("DATABASE_PORT", int, 5432) print(db_port + 1) # Output: 5433 # Creates env var named "MYAPP_POOL_SIZE" pool_size = factory.create_env("POOL_SIZE", int, 5) # Factory without prefix (same as using typed_envs.create_env directly) no_prefix_factory = EnvVarFactory() timeout = no_prefix_factory.create_env("REQUEST_TIMEOUT", int, 30) print(repr(timeout)) # Output: EnvironmentVariable[int](name=`REQUEST_TIMEOUT`, default_value=30, using_default=True) ``` -------------------------------- ### Tracking Environment Variables with Environment Registry Source: https://context7.com/bobthebuidler/typed-envs/llms.txt Illustrates how typed-envs maintains registries of all created environment variables. It shows how to access and inspect these registries to differentiate between user-set variables and those using defaults, which is useful for debugging and validation. ```python import typed_envs from typed_envs import ( ENVIRONMENT, _ENVIRONMENT_VARIABLES_SET_BY_USER, _ENVIRONMENT_VARIABLES_USING_DEFAULTS, ) # Create some environment variables api_key = typed_envs.create_env("API_KEY", str, "default-key") max_retries = typed_envs.create_env("MAX_RETRIES", int, 3) timeout = typed_envs.create_env("TIMEOUT", int, 30) # ENVIRONMENT contains all registered environment variables print("All environment variables:") for name, var in ENVIRONMENT.items(): print(f" {name}: {var}") # _ENVIRONMENT_VARIABLES_USING_DEFAULTS contains vars not set in environment print("\nVariables using defaults:") for name in _ENVIRONMENT_VARIABLES_USING_DEFAULTS: print(f" {name}") # _ENVIRONMENT_VARIABLES_SET_BY_USER contains vars explicitly set in environment print("\nVariables set by user:") for name in _ENVIRONMENT_VARIABLES_SET_BY_USER: print(f" {name}") ``` -------------------------------- ### Registering String Converters with EnvVarFactory Source: https://context7.com/bobthebuidler/typed-envs/llms.txt Demonstrates how to register custom string converters for specific types using EnvVarFactory. It also shows the error handling when attempting to register a converter for a type that already has one. ```python from decimal import Decimal from typed_envs import EnvVarFactory factory = EnvVarFactory() # Output: {: , : } print(factory._type_to_string_converter) # Cannot register a converter for a type that already has one try: factory.register_string_converter(Decimal, str) except ValueError as e: print(e) # Output: There is already a string converter registered for ``` -------------------------------- ### Create Environment Variable with Typed Envs (Python) Source: https://github.com/bobthebuidler/typed-envs/blob/master/README.md Demonstrates how to create a specialized EnvironmentVariable object for an integer type using typed_envs. This object can be used like a standard integer but also type checks as an EnvironmentVariable. ```python import typed_envs some_var = typed_envs.create_env("SET_WITH_THIS_ENV", int, 10) print(isinstance(some_var, int)) print(isinstance(some_var, typed_envs.EnvironmentVariable)) print(some_var) print(str(some_var)) print(some_var + 5) print(20 / some_var) ``` -------------------------------- ### Mypy Plugin Type Checking with Environment Variables Source: https://context7.com/bobthebuidler/typed-envs/llms.txt Demonstrates how the typed-envs mypy plugin enables proper type checking for `EnvironmentVariable[T]`. It shows that variables are correctly recognized as both `EnvironmentVariable` and their underlying type, allowing methods from both to be used without type errors. ```python import typed_envs from typed_envs import EnvironmentVariable # With the plugin enabled, mypy understands the dual inheritance port: EnvironmentVariable[int] = typed_envs.create_env("PORT", int, 8080) # mypy knows port has int methods result = port + 1000 # No type error hex_port = hex(port) # No type error # mypy also knows port is an EnvironmentVariable name = port._env_name # No type error # Works with EnvVarFactory too from typed_envs import EnvVarFactory factory = EnvVarFactory("WEB") host: EnvironmentVariable[str] = factory.create_env("HOST", str, "0.0.0.0") upper_host = host.upper() # No type error ``` -------------------------------- ### Control Verbose Logging for Environment Variables (Python) Source: https://context7.com/bobthebuidler/typed-envs/llms.txt Shows how to control the logging behavior of typed-envs. Logging can be disabled globally by setting the TYPEDENVS_SHUTUP environment variable or per-variable using the 'verbose' parameter in create_env. It also demonstrates accessing the logger directly for custom configuration. ```python import os import typed_envs from typed_envs.ENVIRONMENT_VARIABLES import SHUTUP from typed_envs import logger # Disable logging for a specific variable silent_var = typed_envs.create_env("SILENT_VAR", int, 100, verbose=False) # Disable all typed_envs logging globally os.environ["TYPEDENVS_SHUTUP"] = "1" # Now all subsequent create_env calls will be silent # Or access the SHUTUP variable directly print(repr(SHUTUP)) # Output: EnvironmentVariable[bool](name=`TYPEDENVS_SHUTUP`, default_value=False, using_default=True) # Access the logger directly for custom configuration logger.setLevel("DEBUG") # or logger.disabled = True ``` -------------------------------- ### Simulate Environment Variable Set to Various Falsey Values (Python) Source: https://context7.com/bobthebuidler/typed-envs/llms.txt Demonstrates how typed-envs interprets various string representations of 'false' as boolean False. It also shows that any other string value is considered truthy. Note that boolean environment variables are subclasses of int in Python due to limitations, not actual bools. ```python import os import typed_envs # Simulate environment variable set to various falsey values os.environ["FALSEY_TEST"] = "false" falsey1 = typed_envs.create_env("FALSEY_TEST", bool, True) print(bool(falsey1)) # Output: False os.environ["FALSEY_TEST2"] = "0" falsey2 = typed_envs.create_env("FALSEY_TEST2", bool, True) print(bool(falsey2)) # Output: False os.environ["FALSEY_TEST3"] = "FALSE" falsey3 = typed_envs.create_env("FALSEY_TEST3", bool, True) print(bool(falsey3)) # Output: False # Any other value is truthy os.environ["TRUTHY_TEST"] = "yes" truthy = typed_envs.create_env("TRUTHY_TEST", bool, False) print(bool(truthy)) # Output: True # Note: bool env vars are actually int subclasses (Python limitation) # Assuming 'feature_flag' was previously defined as a boolean env var # print(isinstance(feature_flag, int)) # Output: True # print(isinstance(feature_flag, bool)) # Output: False (can't subclass bool) ``` -------------------------------- ### Boolean Environment Variable Conversion Rules Source: https://context7.com/bobthebuidler/typed-envs/llms.txt Details the special conversion rules for boolean environment variables in typed-envs. It explains how string values like '0', 'false', 'False', and 'FALSE' are interpreted as `False`, while any other non-empty string is treated as `True`. ```python import os import typed_envs # Boolean with default False feature_flag = typed_envs.create_env("FEATURE_FLAG", bool, False) print(bool(feature_flag)) # Output: False ``` -------------------------------- ### EnvironmentVariable Class Metadata Access Source: https://context7.com/bobthebuidler/typed-envs/llms.txt Explains the EnvironmentVariable class, the base for all typed environment variables. It demonstrates how to access metadata such as the variable's name, default value, and whether it's currently using its default value. ```python import typed_envs from typed_envs import EnvironmentVariable # Create an environment variable log_level = typed_envs.create_env("LOG_LEVEL", str, "INFO") # Type checking works for both the base type and EnvironmentVariable assert isinstance(log_level, str) assert isinstance(log_level, EnvironmentVariable) assert isinstance(log_level, EnvironmentVariable[str]) # Access metadata through internal attributes print(f"Name: {log_level._env_name}") # Output: Name: LOG_LEVEL print(f"Default: {log_level._default_value}") # Output: Default: INFO print(f"Using default: {log_level._using_default}") # Output: Using default: True print(f"Current value: {log_level._init_arg0}") # Output: Current value: INFO # String conversion uses the underlying type's __str__ print(str(log_level)) # Output: INFO # repr shows full context print(repr(log_level)) # Output: EnvironmentVariable[str](name=`LOG_LEVEL`, default_value=INFO, using_default=True) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.