Coverage for configlayer/types.py: 100%
36 statements
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-08 11:36 +0000
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-08 11:36 +0000
1"""Config layer type annotations and other types"""
2from __future__ import annotations
3from ast import literal_eval
4from typing import Any, Iterable, Sequence, NamedTuple, TypeVar, Callable, TypeAlias
5from pathlib import Path
6from dataclasses import dataclass
9__all__ = ('path_t', 'state_t', 'holder_t', 'mb_holder_t', 'fields_t', 'on_set_t',
10 'ClsObj', 'ItemError', 'ValidWrong', 'Field')
13T = TypeVar('T')
15path_t = str | Path # Generic path type
16state_t = bool | None # State is bool or None
17holder_t = Iterable[T] | Sequence[T] # Any objects holder type (__iter__ or __getitem__)
19# bug mypy #14824: TypeAlias needed (Type variable "T" is invalid as target for type alias [misc])
20mb_holder_t: TypeAlias = T | Iterable[T] | Sequence[T] # Maybe holder_t, as_holder() possible type
22fields_t = dict[str, T] # Fields holder type
23on_set_t = tuple[mb_holder_t[str] | None, bool, Callable[[str, Any, Any], None]]
26class ClsObj(NamedTuple):
27 cls: type | None
28 obj: object | None
31class ItemError(NamedTuple):
32 key: Any
33 value: Any
34 result: Any = ''
37class ValidWrong(tuple):
38 valid: Any
39 wrong: Any
40 errors: Iterable[ItemError]
42 def __new__(cls, valid, wrong, errors: Iterable[ItemError] = ()):
43 self = super().__new__(cls, (valid, wrong))
44 self.valid = valid
45 self.wrong = wrong
46 self.errors = errors
47 return self
50@dataclass(slots=True)
51class Field:
52 """Field additional descriptor's holder
53 Used if custom export/import functions needed at value declaration of inherited ConfigBase
54 Also used internally in ConfigBase"""
55 default: Any
56 export_func: Callable = repr
57 import_func: Callable = literal_eval
58 type: type = object # internal usage, filled at ConfigBase init