Coverage for src/typedconfig/core.py: 100%

1"""

2 Contains most of the loading logic.

3"""

4

5 import types

6 import typing

7 import warnings

8 from collections import ChainMap

9 from dataclasses import is_dataclass

10 from pathlib import Path

11

12 from typeguard import TypeCheckError

13 from typeguard import check_type as _check_type

14

15 from . import loaders

16 from .errors import ConfigErrorInvalidType, ConfigErrorMissingKey

17 from .helpers import camel_to_snake

18

19# T is a reusable typevar

20 T = typing.TypeVar("T")

21# t_typelike is anything that can be type hinted

22 T_typelike: typing.TypeAlias = type | types.UnionType # | typing.Union

23# t_data is anything that can be fed to _load_data

24 T_data = str | Path | dict[str, typing.Any]

25# c = a config class instance, can be any (user-defined) class

26 C = typing.TypeVar("C")

27# type c is a config class

28 Type_C = typing.Type[C]

29

30

31 def _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.

34

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

43

44 return raw

45

46

47 def _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)

53

54

55 def _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.

58

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)

68

69 if not data:

70 return {}

71

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)

78

79 if key:

80 return _data_for_nested_key(key, data)

81 else:

82 # no key found, just return all data

83 return data

84

85

86 def 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.).

89

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

97

98

99 def 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'.

102

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

108

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)

120

121 final[key] = compare

122 return final

123

124

125 def convert_config(items: dict[str, T]) -> dict[str, T]:

126 """

127 Converts the config dict (from toml) or 'overwrites' dict in two ways.

128

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}

133

134

135 Type = typing.Type[typing.Any]

136 T_Type = typing.TypeVar("T_Type", bound=Type)

137

138

139 def 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")

144

145

146# def is_builtin_class_instance(obj: typing.Any) -> bool:

147# return is_builtin_type(obj.__class__)

148

149

150 def is_from_types_or_typing(_type: Type) -> bool:

151 """

152 Returns whether _type is one of the stlib typing/types types.

153

154 e.g. types.UnionType or typing.Union

155 """

156 return _type.__module__ in ("types", "typing")

157

158

159 def 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")

165

166

167 def is_parameterized(_type: Type) -> bool:

168 """

169 Returns whether _type is a parameterized type.

170

171 Examples:

172 list[str] -> True

173 str -> False

174 """

175 return typing.get_origin(_type) is not None

176

177

178 def is_custom_class(_type: Type) -> bool:

179 """

180 Tries to guess if _type is a builtin or a custom (user-defined) class.

181

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 )

190

191

192 def is_optional(_type: Type | None) -> bool:

193 """

194 Tries to guess if _type could be optional.

195

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 )

210

211

212 def 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.

216

217 Uses `load_into_recurse`, not itself directly.

218

219 Example:

220 class First:

221 key: str

222

223 class Second:

224 other: First

225

226 # step 1

227 cls = Second

228 data = {"second": {"other": {"key": "anything"}}}

229 annotations: {"other": First}

230

231 # step 1.5

232 data = {"other": {"key": "anything"}

233 annotations: {"other": First}

234

235 # step 2

236 cls = First

237 data = {"key": "anything"}

238 annotations: {"key": str}

239

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]

251

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

266

267 # todo: other parameterized/unions/typing.Optional

268

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 )

277

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)

287

288 updated[_key] = value

289

290 return updated

291

292

293 def _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__))

299

300

301 def all_annotations(cls: Type, _except: typing.Iterable[str]) -> dict[str, Type]:

302 """

303 Wrapper around `_all_annotations` that filters away any keys in _except.

304

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}

309

310

311 def _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`.

318

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)

324

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

329

330

331 def 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`.

338

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 = {}

345

346 # fixme: cls.__init__ can set other keys than the name is in kwargs!!

347

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)

351

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)

358

359 return inst

360

361

362 def 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.

371

372 """

373 if init is not None:

374 raise ValueError("Can not init an existing instance!")

375

376 existing_data = inst.__dict__

377

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)

382

383 inst.__dict__.update(**to_load)

384

385 return inst

386

387

388 def 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)

400

401

402 def 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)

415

416

417 def 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).

426

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)

432

433 """

434 if not isinstance(cls, type):

435 return load_into_instance(cls, data, key=key, init=init)

436

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)