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

29 statements  

« prev     ^ index     » next       coverage.py v7.3.4, created at 2023-12-20 14:17 -0500

1from pydantic import BaseModel 

2 

3 

4class Attr(BaseModel): 

5 

6 @staticmethod 

7 def create(path, field_name, attribute): 

8 return Attr( 

9 path=path, 

10 field_name=field_name, 

11 type_str=type(attribute).__name__, 

12 value_repr=repr(attribute) 

13 ) 

14 

15 path: str 

16 field_name: str 

17 value_repr: str 

18 type_str: str 

19 attributes: list['Attr'] = [] 

20 

21 

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

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

24 for child_attr_name in dir(attr): 

25 if child_attr_name.startswith('__'): 

26 continue 

27 child_attr = getattr(attr, child_attr_name) 

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

29 continue 

30 child_attr = Attr.create(f'{path}.{child_attr_name}', child_attr_name, attribute=child_attr) 

31 attr_info.attributes.append(child_attr) 

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

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

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

35 attr_info.attributes.append(child_attr) 

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

37 for k, v in enumerate(attr): 

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

39 attr_info.attributes.append(child_attr) 

40 return attr_info