typed_envs package

Submodules

typed_envs.ENVIRONMENT_VARIABLES module

typed_envs.factory module

class typed_envs.factory.EnvVarFactory[source]

Bases: object

Factory for creating EnvironmentVariable instances with optional prefix.

__init__(env_var_prefix=None)[source]

Initializes the EnvVarFactory with an optional prefix for environment variables.

Parameters:

env_var_prefix (str | None) – An optional string prefix to be added to environment variable names.

Return type:

None

create_env(env_var_name, env_var_type, default, *init_args, string_converter=None, verbose=True, **init_kwargs)[source]

Creates a new EnvironmentVariable object with the specified parameters.

Parameters:
  • env_var_name (str) – The name of the environment variable.

  • env_var_type (Type[T]) – The type of the environment variable.

  • default (Any) – The default value for the environment variable.

  • *init_args – Additional positional arguments for initialization.

  • string_converter (Callable[[str], Any] | None) – An optional callable to convert the string value from the environment.

  • verbose (bool) – If True, logs the environment variable details.

  • **init_kwargs (Any) – Additional keyword arguments for initialization.

Returns:

An instance of EnvironmentVariable with the specified type and value.

Return type:

EnvironmentVariable[T]

Example

Create an environment variable with an integer type using an EnvVarFactory instance:

```python from typed_envs.factory import EnvVarFactory factory = EnvVarFactory() some_var = factory.create_env(“SET_WITH_THIS_ENV”, int, 10)

>>> isinstance(some_var, int)
True
>>> isinstance(some_var, EnvironmentVariable)
True
```

Differences between some_var and int(10): - some_var will type check as both int and EnvironmentVariable. - some_var.__repr__() includes contextual information about the EnvironmentVariable.

`python >>> some_var <EnvironmentVariable[name=`SET_WITH_THIS_ENV`, type=int, default_value=10, current_value=10, using_default=True]> >>> str(some_var) "10" >>> some_var + 5 15 >>> 20 / some_var 2 `

See also

register_string_converter(register_for, converter)[source]
Parameters:
Return type:

None

__dict__ = mappingproxy({'__module__': 'typed_envs.factory', '__doc__': 'Factory for creating :class:`EnvironmentVariable` instances with optional prefix.', '__init__': <function EnvVarFactory.__init__>, 'default_string_converters': <property object>, 'use_prefix': <property object>, 'create_env': <function EnvVarFactory.create_env>, 'register_string_converter': <function EnvVarFactory.register_string_converter>, '__dict__': <attribute '__dict__' of 'EnvVarFactory' objects>, '__weakref__': <attribute '__weakref__' of 'EnvVarFactory' objects>, '__annotations__': {'prefix': 'Final', '__use_prefix': 'Final', '__default_string_converters': 'Final[Dict[Type, StringConverter]]'}})
__module__ = 'typed_envs.factory'
__weakref__

list of weak references to the object (if defined)

property default_string_converters: Dict[Type, Callable[[str], Any]]
property use_prefix: bool

typed_envs.registry module

typed_envs.typing module

Module contents

class typed_envs.EnvVarFactory[source]

Bases: object

Factory for creating EnvironmentVariable instances with optional prefix.

__init__(env_var_prefix=None)[source]

Initializes the EnvVarFactory with an optional prefix for environment variables.

Parameters:

env_var_prefix (str | None) – An optional string prefix to be added to environment variable names.

Return type:

None

create_env(env_var_name, env_var_type, default, *init_args, string_converter=None, verbose=True, **init_kwargs)[source]

Creates a new EnvironmentVariable object with the specified parameters.

Parameters:
  • env_var_name (str) – The name of the environment variable.

  • env_var_type (Type[T]) – The type of the environment variable.

  • default (Any) – The default value for the environment variable.

  • *init_args – Additional positional arguments for initialization.

  • string_converter (Callable[[str], Any] | None) – An optional callable to convert the string value from the environment.

  • verbose (bool) – If True, logs the environment variable details.

  • **init_kwargs (Any) – Additional keyword arguments for initialization.

Returns:

An instance of EnvironmentVariable with the specified type and value.

Return type:

EnvironmentVariable[T]

Example

Create an environment variable with an integer type using an EnvVarFactory instance:

```python from typed_envs.factory import EnvVarFactory factory = EnvVarFactory() some_var = factory.create_env(“SET_WITH_THIS_ENV”, int, 10)

>>> isinstance(some_var, int)
True
>>> isinstance(some_var, EnvironmentVariable)
True
```

Differences between some_var and int(10): - some_var will type check as both int and EnvironmentVariable. - some_var.__repr__() includes contextual information about the EnvironmentVariable.

`python >>> some_var <EnvironmentVariable[name=`SET_WITH_THIS_ENV`, type=int, default_value=10, current_value=10, using_default=True]> >>> str(some_var) "10" >>> some_var + 5 15 >>> 20 / some_var 2 `

See also

register_string_converter(register_for, converter)[source]
Parameters:
Return type:

None

__annotations__ = {'__default_string_converters': 'Final[Dict[Type, StringConverter]]', '__use_prefix': 'Final', 'prefix': 'Final'}
__dict__ = mappingproxy({'__module__': 'typed_envs.factory', '__doc__': 'Factory for creating :class:`EnvironmentVariable` instances with optional prefix.', '__init__': <function EnvVarFactory.__init__>, 'default_string_converters': <property object>, 'use_prefix': <property object>, 'create_env': <function EnvVarFactory.create_env>, 'register_string_converter': <function EnvVarFactory.register_string_converter>, '__dict__': <attribute '__dict__' of 'EnvVarFactory' objects>, '__weakref__': <attribute '__weakref__' of 'EnvVarFactory' objects>, '__annotations__': {'prefix': 'Final', '__use_prefix': 'Final', '__default_string_converters': 'Final[Dict[Type, StringConverter]]'}})
__module__ = 'typed_envs.factory'
__weakref__

list of weak references to the object (if defined)

property default_string_converters: Dict[Type, Callable[[str], Any]]
prefix: Final
property use_prefix: bool
typed_envs.create_env()