Skip to main content
Version: 1.5.0 (latest)

common.configuration.specs.base_configuration

NotResolved Objects

class NotResolved()

[view_source]

Used in type annotations to indicate types that should not be resolved.

is_hint_not_resolvable

def is_hint_not_resolvable(hint: AnyType) -> bool

[view_source]

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]]]

[view_source]

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])

[view_source]

__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

[view_source]

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

[view_source]

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 representation
  • ValueError - The value provided cannot be parsed as native representation

to_native_representation

def to_native_representation() -> Any

[view_source]

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]

[view_source]

Returns a mapping of fields to their type hints. Dunders should not be resolved and are not returned

is_partial

def is_partial() -> bool

[view_source]

Returns True when any required resolvable field has its value missing.

copy

def copy() -> _B

[view_source]

Returns a deep copy of the configuration instance

__iter__

def __iter__() -> Iterator[str]

[view_source]

Iterator or valid key names

CredentialsConfiguration Objects

@configspec
class CredentialsConfiguration(BaseConfiguration)

[view_source]

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

[view_source]

Returns native credentials object.

By default calls to_native_representation method.

__str__

def __str__() -> str

[view_source]

Get string representation of credentials to be displayed, with all secret parts removed

CredentialsWithDefault Objects

class CredentialsWithDefault()

[view_source]

A mixin for credentials that can be instantiated from default ie. from well known env variable with credentials

ContainerInjectableContext Objects

@configspec
class ContainerInjectableContext(BaseConfiguration)

[view_source]

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

[view_source]

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

[view_source]

Called each time after context is added to container

before_remove

def before_remove() -> None

[view_source]

Called each time before context is removed from container

This demo works on codespaces. Codespaces is a development environment available for free to anyone with a Github account. You'll be asked to fork the demo repository and from there the README guides you with further steps.
The demo uses the Continue VSCode extension.

Off to codespaces!

DHelp

Ask a question

Welcome to "Codex Central", your next-gen help center, driven by OpenAI's GPT-4 model. It's more than just a forum or a FAQ hub – it's a dynamic knowledge base where coders can find AI-assisted solutions to their pressing problems. With GPT-4's powerful comprehension and predictive abilities, Codex Central provides instantaneous issue resolution, insightful debugging, and personalized guidance. Get your code running smoothly with the unparalleled support at Codex Central - coding help reimagined with AI prowess.