@@ -62,6 +62,9 @@ class ConfigOption:
6262 supplied, we'll construct this. False disables reading from
6363 environmental variables, None uses the auto generated variable name
6464 and explicitly provided string overwrites the default one.
65+ default: The value we should resolve to when the option is not defined
66+ in any of the sources. When it's None we treat that as there's no
67+ default value.
6568 _root_manager: Reference to the root manager. Used to efficiently
6669 refer to cached config file. Is supplied by the parent
6770 ConfigManager.
@@ -78,6 +81,7 @@ def __init__(
7881 parse_str : Callable [[str ], _T ] | None = None ,
7982 choices : Iterable [Any ] | None = None ,
8083 env_name : str | None | Literal [False ] = None ,
84+ default : Any | None = None ,
8185 _root_manager : ConfigManager | None = None ,
8286 _nest_path : list [str ] | None ,
8387 ) -> None :
@@ -91,6 +95,8 @@ def __init__(
9195 Providing a string will use that environment variable, False disables
9296 reading value from environmental variables and the default None generates
9397 an environmental variable name for it using the _nest_path and name.
98+ default: Default value for the option. Used in case the value is
99+ is not defined in any of the sources.
94100 _root_manager: Reference to the root manager. Should be supplied by
95101 the parent ConfigManager.
96102 _nest_path: The names of the ConfigManagers that this option is
@@ -106,6 +112,7 @@ def __init__(
106112 self ._nest_path = _nest_path + [name ]
107113 self ._root_manager : ConfigManager = _root_manager
108114 self .env_name = env_name
115+ self .default = default
109116
110117 def value (self ) -> Any :
111118 """Retrieve a value of option.
@@ -115,8 +122,15 @@ def value(self) -> Any:
115122 source = "environment variable"
116123 loaded_env , value = self ._get_env ()
117124 if not loaded_env :
118- source = "configuration file"
119- value = self ._get_config ()
125+ try :
126+ value = self ._get_config ()
127+ source = "configuration file"
128+ except MissingConfigOptionError :
129+ if self .default :
130+ source = "default_value"
131+ value = self .default
132+ else :
133+ raise
120134 if self .choices and value not in self .choices :
121135 raise ConfigSourceError (
122136 f"The value of { self .option_name } read from "
@@ -436,6 +450,10 @@ def __getitem__(self, name: str) -> ConfigOption | ConfigManager:
436450 name = "connections" ,
437451 parse_str = tomlkit .parse ,
438452)
453+ CONFIG_MANAGER .add_option (
454+ name = "default_connection_name" ,
455+ default = "default" ,
456+ )
439457
440458
441459def __getattr__ (name ):
0 commit comments