Coverage for src/typedconfig/core.py: 100%
142 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-13 19:27 +0200
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-13 19:27 +0200
1"""
2Contains most of the loading logic.
3"""
5import types
6import typing
7import warnings
8from collections import ChainMap
9from dataclasses import is_dataclass
10from pathlib import Path
12from typeguard import TypeCheckError
13from typeguard import check_type as _check_type
15from . import loaders
16from .errors import ConfigErrorInvalidType, ConfigErrorMissingKey
17from .helpers import camel_to_snake
19# T is a reusable typevar
20T = typing.TypeVar("T")
21# t_typelike is anything that can be type hinted
22T_typelike: typing.TypeAlias = type | types.UnionType # | typing.Union
23# t_data is anything that can be fed to _load_data
24T_data = str | Path | dict[str, typing.Any]
25# c = a config class instance, can be any (user-defined) class
26C = typing.TypeVar("C")
27# type c is a config class
28Type_C = typing.Type[C]
31def _data_for_nested_key(key: str, raw: dict[str, typing.Any]) -> dict[str, typing.Any]:
32 """
33 If a key contains a dot, traverse the raw dict until the right key was found.
35 Example:
36 key = some.nested.key
37 raw = {"some": {"nested": {"key": {"with": "data"}}}}
38 -> {"with": "data"}
39 """
40 parts = key.split(".")
41 while parts:
42 raw = raw[parts.pop(0)]
44 return raw
47def _guess_key(clsname: str) -> str:
48 """
49 If no key is manually defined for `load_into`, \
50 the class' name is converted to snake_case to use as the default key.
51 """
52 return camel_to_snake(clsname)
55def _load_data(data: T_data, key: str = None, classname: str = None) -> dict[str, typing.Any]:
56 """
57 Tries to load the right data from a filename/path or dict, based on a manual key or a classname.
59 E.g. class Tool will be mapped to key tool.
60 It also deals with nested keys (tool.extra -> {"tool": {"extra": ...}}
61 """
62 if isinstance(data, str):
63 data = Path(data)
64 if isinstance(data, Path):
65 # todo: more than toml
66 with data.open("rb") as f:
67 data = loaders.toml(f)
69 if not data:
70 return {}
72 if key is None:
73 # try to guess key by grabbing the first one or using the class name
74 if len(data) == 1:
75 key = list(data.keys())[0]
76 elif classname is not None:
77 key = _guess_key(classname)
79 if key:
80 return _data_for_nested_key(key, data)
81 else:
82 # no key found, just return all data
83 return data
86def check_type(value: typing.Any, expected_type: T_typelike) -> bool:
87 """
88 Given a variable, check if it matches 'expected_type' (which can be a Union, parameterized generic etc.).
90 Based on typeguard but this returns a boolean instead of returning the value or throwing a TypeCheckError
91 """
92 try:
93 _check_type(value, expected_type)
94 return True
95 except TypeCheckError:
96 return False
99def ensure_types(data: dict[str, T], annotations: dict[str, type]) -> dict[str, T | None]:
100 """
101 Make sure all values in 'data' are in line with the ones stored in 'annotations'.
103 If an annotated key in missing from data, it will be filled with None for convenience.
104 """
105 # custom object to use instead of None, since typing.Optional can be None!
106 # cast to T to make mypy happy
107 notfound = typing.cast(T, object())
109 final: dict[str, T | None] = {}
110 for key, _type in annotations.items():
111 compare = data.get(key, notfound)
112 if compare is notfound: # pragma: nocover
113 warnings.warn(
114 "This should not happen since " "`load_recursive` already fills `data` " "based on `annotations`"
115 )
116 # skip!
117 continue
118 if not check_type(compare, _type):
119 raise ConfigErrorInvalidType(key, value=compare, expected_type=_type)
121 final[key] = compare
122 return final
125def convert_config(items: dict[str, T]) -> dict[str, T]:
126 """
127 Converts the config dict (from toml) or 'overwrites' dict in two ways.
129 1. removes any items where the value is None, since in that case the default should be used;
130 2. replaces '-' and '.' in keys with '_' so it can be mapped to the Config properties.
131 """
132 return {k.replace("-", "_").replace(".", "_"): v for k, v in items.items() if v is not None}
135Type = typing.Type[typing.Any]
136T_Type = typing.TypeVar("T_Type", bound=Type)
139def is_builtin_type(_type: Type) -> bool:
140 """
141 Returns whether _type is one of the builtin types.
142 """
143 return _type.__module__ in ("__builtin__", "builtins")
146# def is_builtin_class_instance(obj: typing.Any) -> bool:
147# return is_builtin_type(obj.__class__)
150def is_from_types_or_typing(_type: Type) -> bool:
151 """
152 Returns whether _type is one of the stlib typing/types types.
154 e.g. types.UnionType or typing.Union
155 """
156 return _type.__module__ in ("types", "typing")
159def is_from_other_toml_supported_module(_type: Type) -> bool:
160 """
161 Besides builtins, toml also supports 'datetime' and 'math' types, \
162 so this returns whether _type is a type from these stdlib modules.
163 """
164 return _type.__module__ in ("datetime", "math")
167def is_parameterized(_type: Type) -> bool:
168 """
169 Returns whether _type is a parameterized type.
171 Examples:
172 list[str] -> True
173 str -> False
174 """
175 return typing.get_origin(_type) is not None
178def is_custom_class(_type: Type) -> bool:
179 """
180 Tries to guess if _type is a builtin or a custom (user-defined) class.
182 Other logic in this module depends on knowing that.
183 """
184 return (
185 type(_type) is type
186 and not is_builtin_type(_type)
187 and not is_from_other_toml_supported_module(_type)
188 and not is_from_types_or_typing(_type)
189 )
192def is_optional(_type: Type | None) -> bool:
193 """
194 Tries to guess if _type could be optional.
196 Examples:
197 None -> True
198 NoneType -> True
199 typing.Union[str, None] -> True
200 str | None -> True
201 list[str | None] -> False
202 list[str] -> False
203 """
204 return (
205 _type is None
206 or issubclass(types.NoneType, _type)
207 or issubclass(types.NoneType, type(_type)) # no type # Nonetype
208 or type(None) in typing.get_args(_type) # union with Nonetype
209 )
212def load_recursive(cls: Type, data: dict[str, T], annotations: dict[str, Type]) -> dict[str, T]:
213 """
214 For all annotations (recursively gathered from parents with `all_annotations`), \
215 try to resolve the tree of annotations.
217 Uses `load_into_recurse`, not itself directly.
219 Example:
220 class First:
221 key: str
223 class Second:
224 other: First
226 # step 1
227 cls = Second
228 data = {"second": {"other": {"key": "anything"}}}
229 annotations: {"other": First}
231 # step 1.5
232 data = {"other": {"key": "anything"}
233 annotations: {"other": First}
235 # step 2
236 cls = First
237 data = {"key": "anything"}
238 annotations: {"key": str}
240 """
241 updated = {}
242 for _key, _type in annotations.items():
243 if _key in data:
244 value: typing.Any = data[_key] # value can change so define it as any instead of T
245 if is_parameterized(_type):
246 origin = typing.get_origin(_type)
247 arguments = typing.get_args(_type)
248 if origin is list and arguments and is_custom_class(arguments[0]):
249 subtype = arguments[0]
250 value = [load_into_recurse(subtype, subvalue) for subvalue in value]
252 elif origin is dict and arguments and is_custom_class(arguments[1]):
253 # e.g. dict[str, Point]
254 subkeytype, subvaluetype = arguments
255 # subkey(type) is not a custom class, so don't try to convert it:
256 value = {subkey: load_into_recurse(subvaluetype, subvalue) for subkey, subvalue in value.items()}
257 # elif origin is dict:
258 # keep data the same
259 elif origin is typing.Union and arguments:
260 for arg in arguments:
261 if is_custom_class(arg):
262 value = load_into_recurse(arg, value)
263 else:
264 # print(_type, arg, value)
265 ...
267 # todo: other parameterized/unions/typing.Optional
269 elif is_custom_class(_type):
270 # type must be C (custom class) at this point
271 value = load_into_recurse(
272 # make mypy and pycharm happy by telling it _type is of type C...
273 # actually just passing _type as first arg!
274 typing.cast(Type_C[typing.Any], _type),
275 value,
276 )
278 elif _key in cls.__dict__:
279 # property has default, use that instead.
280 value = cls.__dict__[_key]
281 elif is_optional(_type):
282 # type is optional and not found in __dict__ -> default is None
283 value = None
284 else:
285 # todo: exception group?
286 raise ConfigErrorMissingKey(_key, cls, _type)
288 updated[_key] = value
290 return updated
293def _all_annotations(cls: Type) -> ChainMap[str, Type]:
294 """
295 Returns a dictionary-like ChainMap that includes annotations for all \
296 attributes defined in cls or inherited from superclasses.
297 """
298 return ChainMap(*(c.__annotations__ for c in getattr(cls, "__mro__", []) if "__annotations__" in c.__dict__))
301def all_annotations(cls: Type, _except: typing.Iterable[str]) -> dict[str, Type]:
302 """
303 Wrapper around `_all_annotations` that filters away any keys in _except.
305 It also flattens the ChainMap to a regular dict.
306 """
307 _all = _all_annotations(cls)
308 return {k: v for k, v in _all.items() if k not in _except}
311def _check_and_convert_data(
312 cls: typing.Type[C],
313 data: dict[str, typing.Any],
314 _except: typing.Iterable[str],
315) -> dict[str, typing.Any]:
316 """
317 Based on class annotations, this prepares the data for `load_into_recurse`.
319 1. convert config-keys to python compatible config_keys
320 2. loads custom class type annotations with the same logic (see also `load_recursive`)
321 3. ensures the annotated types match the actual types after loading the config file.
322 """
323 annotations = all_annotations(cls, _except=_except)
325 to_load = convert_config(data)
326 to_load = load_recursive(cls, to_load, annotations)
327 to_load = ensure_types(to_load, annotations)
328 return to_load
331def load_into_recurse(
332 cls: typing.Type[C],
333 data: dict[str, typing.Any],
334 init: dict[str, typing.Any] = None,
335) -> C:
336 """
337 Loads an instance of `cls` filled with `data`.
339 Uses `load_recursive` to load any fillable annotated properties (see that method for an example).
340 `init` can be used to optionally pass extra __init__ arguments. \
341 NOTE: This will overwrite a config key with the same name!
342 """
343 if init is None:
344 init = {}
346 # fixme: cls.__init__ can set other keys than the name is in kwargs!!
348 if is_dataclass(cls):
349 to_load = _check_and_convert_data(cls, data, init.keys())
350 to_load |= init # add extra init variables (should not happen for a dataclass but whatev)
352 # ensure mypy inst is an instance of the cls type (and not a fictuous `DataclassInstance`)
353 inst = typing.cast(C, cls(**to_load))
354 else:
355 inst = cls(**init)
356 to_load = _check_and_convert_data(cls, data, inst.__dict__.keys())
357 inst.__dict__.update(**to_load)
359 return inst
362def load_into_existing(
363 inst: C,
364 cls: typing.Type[C],
365 data: dict[str, typing.Any],
366 init: dict[str, typing.Any] = None,
367) -> C:
368 """
369 Similar to `load_into_recurse` but uses an existing instance of a class (so after __init__) \
370 and thus does not support init.
372 """
373 if init is not None:
374 raise ValueError("Can not init an existing instance!")
376 existing_data = inst.__dict__
378 annotations = all_annotations(cls, _except=existing_data.keys())
379 to_load = convert_config(data)
380 to_load = load_recursive(cls, to_load, annotations)
381 to_load = ensure_types(to_load, annotations)
383 inst.__dict__.update(**to_load)
385 return inst
388def load_into_class(
389 cls: typing.Type[C],
390 data: T_data,
391 /,
392 key: str = None,
393 init: dict[str, typing.Any] = None,
394) -> C:
395 """
396 Shortcut for _load_data + load_into_recurse.
397 """
398 to_load = _load_data(data, key, cls.__name__)
399 return load_into_recurse(cls, to_load, init=init)
402def load_into_instance(
403 inst: C,
404 data: T_data,
405 /,
406 key: str = None,
407 init: dict[str, typing.Any] = None,
408) -> C:
409 """
410 Shortcut for _load_data + load_into_existing.
411 """
412 cls = inst.__class__
413 to_load = _load_data(data, key, cls.__name__)
414 return load_into_existing(inst, cls, to_load, init=init)
417def load_into(
418 cls: typing.Type[C] | C,
419 data: T_data,
420 /,
421 key: str = None,
422 init: dict[str, typing.Any] = None,
423) -> C:
424 """
425 Load your config into a class (instance).
427 Args:
428 cls: either a class or an existing instance of that class.
429 data: can be a dictionary or a path to a file to load (as pathlib.Path or str)
430 key: optional (nested) dictionary key to load data from (e.g. 'tool.su6.specific')
431 init: optional data to pass to your cls' __init__ method (only if cls is not an instance already)
433 """
434 if not isinstance(cls, type):
435 return load_into_instance(cls, data, key=key, init=init)
437 # make mypy and pycharm happy by telling it cls is of type C and not just 'type'
438 _cls = typing.cast(typing.Type[C], cls)
439 return load_into_class(_cls, data, key=key, init=init)