Coverage for src / renaissance / integrations / python / ast / ast_node.py: 80%

44 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-09-09 14:04 +0000

1"""implementation that patches the native ast using 'traits' mechanism, 

2require minimum amount of code to make the matcher work. 

3 

4""" 

5 

6import ast 

7 

8from renaissance.integrations.types import KIND_MAP, BogusType 

9 

10 

11class ASTExtension: 

12 @staticmethod 

13 def load_from_ast(text, file): 

14 root = ast.parse(text, file) 

15 return root 

16 

17 @staticmethod 

18 @property 

19 def ast_node(self): 

20 return self 

21 

22 @staticmethod 

23 @property 

24 def ast_kind(self): 

25 return KIND_MAP.get(type(self).__name__, BogusType).__name__ 

26 

27 @staticmethod 

28 @property 

29 def ast_type(self): 

30 return KIND_MAP.get(type(self).__name__, BogusType) 

31 

32 @staticmethod 

33 @property 

34 def ast_properties(self): 

35 return {field: getattr(self, field) for field in self._fields if not isinstance(getattr(self, field), ast.AST)} 

36 

37 @staticmethod 

38 @property 

39 def ast_children(self): 

40 children = [getattr(self, field) for field in self._fields if isinstance(getattr(self, field), (ast.AST))] 

41 [children.extend(getattr(self, field)) for field in self._fields if isinstance(getattr(self, field), (list))] 

42 return children 

43 

44 @staticmethod 

45 @property 

46 def ast_signature(self): 

47 return ast.unparse(self) 

48 

49 @staticmethod 

50 @property 

51 def ast_name(self): 

52 if isinstance(self, ast.arg): 

53 signature = self.arg 

54 elif isinstance(self, ast.Name): 

55 signature = self.id 

56 elif isinstance(self, ast.Expr) and isinstance(self.value, ast.Name): 

57 signature = self.value.id 

58 else: 

59 signature = str(self) 

60 return signature