Coverage for src/zapy/store/attr.py: 78%

30 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-02-10 19:35 +0000

1from typing import Any 

2 

3from pydantic import BaseModel 

4 

5 

6class Attr(BaseModel): 

7 

8 @staticmethod 

9 def create(path: str, field_name: str, attribute: Any) -> "Attr": 

10 return Attr(path=path, field_name=field_name, type_str=type(attribute).__name__, value_repr=repr(attribute)) 

11 

12 path: str 

13 field_name: str 

14 value_repr: str 

15 type_str: str 

16 attributes: list["Attr"] = [] 

17 

18 

19def build_attr_info(attr: Any, path: str) -> Attr: 

20 attr_info = Attr.create(path, path, attribute=attr) 

21 for child_attr_name in dir(attr): 

22 if child_attr_name.startswith("__"): 

23 continue 

24 child_attr = getattr(attr, child_attr_name) 

25 if callable(child_attr): 25 ↛ 27line 25 didn't jump to line 27, because the condition on line 25 was never false

26 continue 

27 child_attr = Attr.create(f"{path}.{child_attr_name}", child_attr_name, attribute=child_attr) 

28 attr_info.attributes.append(child_attr) 

29 if isinstance(attr, dict): 29 ↛ 33line 29 didn't jump to line 33, because the condition on line 29 was never false

30 for k, v in attr.items(): 

31 child_attr = Attr.create(f"{path}['{k}']", f"'{k}'", attribute=v) 

32 attr_info.attributes.append(child_attr) 

33 if isinstance(attr, (list, tuple)): 33 ↛ 34line 33 didn't jump to line 34, because the condition on line 33 was never true

34 for k, v in enumerate(attr): 

35 child_attr = Attr.create(f"{path}[{k}]", str(k), attribute=v) 

36 attr_info.attributes.append(child_attr) 

37 return attr_info