common.configuration.specs.base_configuration
NotResolved Objects
class NotResolved()
Used in type annotations to indicate types that should not be resolved.
is_hint_not_resolvable
def is_hint_not_resolvable(hint: AnyType) -> bool
Checks if hint should NOT be resolved. Final and types annotated like
Annotated[str, NotResolved()]
are not resolved.
configspec
@dataclass_transform(eq_default=False,
field_specifiers=(dataclasses.Field, dataclasses.field))
def configspec(
cls: Optional[Type[Any]] = None,
init: bool = True
) -> Union[Type[TAnyClass], Callable[[Type[TAnyClass]], Type[TAnyClass]]]
Converts (via derivation) any decorated class to a Python dataclass that may be used as a spec to resolve configurations
init method is synthesized by default. init
flag is ignored if the decorated class implements custom init as well as
when any of base classes has no synthesized init
All fields must have default values. This decorator will add None
default values that miss one.
In comparison the Python dataclass, a spec implements full dictionary interface for its attributes, allows instance creation from ie. strings
or other types (parsing, deserialization) and control over configuration resolution process. See BaseConfiguration
and CredentialsConfiguration` for
more information.
BaseConfiguration Objects
@configspec
class BaseConfiguration(MutableMapping[str, Any])
__is_resolved__
True when all config fields were resolved and have a specified value type
__exception__
Holds the exception that prevented the full resolution
__section__
Obligatory section used by config providers when searching for keys, always present in the search path
__config_gen_annotations__
Additional annotations for config generator, currently holds a list of fields of interest that have defaults
__dataclass_fields__
Typing for dataclass fields
from_init_value
@classmethod
def from_init_value(cls: Type[_B], init_value: Any = None) -> _B
Initializes credentials from init_value
Init value may be a native representation of the credentials or a dict. In case of native representation (for example a connection string or JSON with service account credentials)
a parse_native_representation
method will be used to parse it. In case of a dict, the credentials object will be updated with key: values of the dict.
Unexpected values in the dict will be ignored.
Credentials will be marked as resolved if all required fields are set resolve() method is successful
parse_native_representation
def parse_native_representation(native_value: Any) -> None
Initialize the configuration fields by parsing the native_value
which should be a native representation of the configuration
or credentials, for example database connection string or JSON serialized GCP service credentials file.
Arguments:
native_value
Any - A native representation of the configuration
Raises:
NotImplementedError
- This configuration does not have a native representationValueError
- The value provided cannot be parsed as native representation
to_native_representation
def to_native_representation() -> Any
Represents the configuration instance in its native form ie. database connection string or JSON serialized GCP service credentials file.
Raises:
NotImplementedError
- This configuration does not have a native representation
Returns:
Any
- A native representation of the configuration
get_resolvable_fields
@classmethod
def get_resolvable_fields(cls) -> Dict[str, type]
Returns a mapping of fields to their type hints. Dunders should not be resolved and are not returned
is_partial
def is_partial() -> bool
Returns True when any required resolvable field has its value missing.
copy
def copy() -> _B
Returns a deep copy of the configuration instance
__iter__
def __iter__() -> Iterator[str]
Iterator or valid key names
CredentialsConfiguration Objects
@configspec
class CredentialsConfiguration(BaseConfiguration)
Base class for all credentials. Credentials are configurations that may be stored only by providers supporting secrets.
to_native_credentials
def to_native_credentials() -> Any
Returns native credentials object.
By default calls to_native_representation
method.
__str__
def __str__() -> str
Get string representation of credentials to be displayed, with all secret parts removed
CredentialsWithDefault Objects
class CredentialsWithDefault()
A mixin for credentials that can be instantiated from default ie. from well known env variable with credentials
ContainerInjectableContext Objects
@configspec
class ContainerInjectableContext(BaseConfiguration)
Base class for all configurations that may be injected from a Container. Injectable configuration is called a context
can_create_default
If True, Container
is allowed to create default context instance, if none exists
global_affinity
If True, Container
will create context that will be visible in any thread. If False, per thread context is created
in_container
Current container, if None then not injected
extras_added
Tells if extras were already added to this context
add_extras
def add_extras() -> None
Called once after default context was created and added to the container. Benefits mostly the config provider injection context which adds extra providers using the initial ones.
after_add
def after_add() -> None
Called each time after context is added to container
before_remove
def before_remove() -> None
Called each time before context is removed from container