Principle:Confident ai Deepeval Package Constants Management
| Knowledge Sources | |
|---|---|
| Domains | Configuration, Software_Architecture |
| Last Updated | 2026-02-14 09:30 GMT |
Overview
Principle of centralizing all package-wide constants, enumerated types, and configuration variable names in a single module to eliminate magic strings and ensure consistency across a codebase.
Description
Package Constants Management is the practice of defining all shared string literals, file paths, environment variable names, and enumerated types in a dedicated constants module rather than scattering them across the codebase. This principle addresses several software engineering concerns:
- Elimination of magic strings: Provider names, environment variable keys, and file paths are defined once and referenced by symbol, preventing typos and inconsistencies.
- Type-safe enumeration: Using `str`-`Enum` hybrids provides both human-readable string values and IDE-level type checking, ensuring only valid values are used.
- Centralized refactoring: Renaming a constant or adding a new provider requires changes in exactly one location.
- O(1) membership testing: Frozen sets of valid values enable efficient validation without repeated list construction.
The pattern of inheriting from both `str` and `Enum` is particularly useful in Python because enum members can be used directly as strings (in comparisons, dictionary keys, f-strings) while still providing `.name` and `.value` attributes for programmatic access.
Usage
Apply this principle in any Python package where multiple modules need to reference the same string constants, environment variable names, or enumerated types. It is especially valuable when the set of valid values (such as provider names) may change over time.
Theoretical Basis
The core pattern follows the constant extraction refactoring:
# Abstract pattern (NOT real implementation)
# BEFORE: magic strings scattered across modules
if provider == "openai": # in module A
...
key = os.getenv("CONFIDENT_TRACING_ENABLED") # in module B
# AFTER: centralized constants
class ProviderSlug(str, Enum):
OPENAI = "openai"
CONFIDENT_TRACING_ENABLED = "CONFIDENT_TRACING_ENABLED"
# Usage: import and reference by symbol
if provider == ProviderSlug.OPENAI: # type-safe
...
key = os.getenv(CONFIDENT_TRACING_ENABLED) # refactor-safe
The `str`-`Enum` hybrid pattern leverages Python's multiple inheritance: `ProviderSlug.OPENAI == "openai"` evaluates to `True`, enabling seamless interoperability with string-based APIs.