Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.10/site-packages/astroid/_ast.py: 92%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

37 statements  

1# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html 

2# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE 

3# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt 

4 

5from __future__ import annotations 

6 

7import ast 

8from typing import NamedTuple 

9 

10from astroid.const import Context 

11 

12 

13class FunctionType(NamedTuple): 

14 argtypes: list[ast.expr] 

15 returns: ast.expr 

16 

17 

18class ParserModule(NamedTuple): 

19 unary_op_classes: dict[type[ast.unaryop], str] 

20 cmp_op_classes: dict[type[ast.cmpop], str] 

21 bool_op_classes: dict[type[ast.boolop], str] 

22 bin_op_classes: dict[type[ast.operator], str] 

23 context_classes: dict[type[ast.expr_context], Context] 

24 

25 def parse( 

26 self, string: str, type_comments: bool = True, filename: str | None = None 

27 ) -> ast.Module: 

28 if filename: 

29 return ast.parse(string, filename=filename, type_comments=type_comments) 

30 return ast.parse(string, type_comments=type_comments) 

31 

32 

33def parse_function_type_comment(type_comment: str) -> FunctionType | None: 

34 """Given a correct type comment, obtain a FunctionType object.""" 

35 func_type = ast.parse(type_comment, "<type_comment>", "func_type") 

36 return FunctionType(argtypes=func_type.argtypes, returns=func_type.returns) 

37 

38 

39def get_parser_module(type_comments: bool = True) -> ParserModule: 

40 unary_op_classes = _unary_operators_from_module() 

41 cmp_op_classes = _compare_operators_from_module() 

42 bool_op_classes = _bool_operators_from_module() 

43 bin_op_classes = _binary_operators_from_module() 

44 context_classes = _contexts_from_module() 

45 

46 return ParserModule( 

47 unary_op_classes, 

48 cmp_op_classes, 

49 bool_op_classes, 

50 bin_op_classes, 

51 context_classes, 

52 ) 

53 

54 

55def _unary_operators_from_module() -> dict[type[ast.unaryop], str]: 

56 return {ast.UAdd: "+", ast.USub: "-", ast.Not: "not", ast.Invert: "~"} 

57 

58 

59def _binary_operators_from_module() -> dict[type[ast.operator], str]: 

60 return { 

61 ast.Add: "+", 

62 ast.BitAnd: "&", 

63 ast.BitOr: "|", 

64 ast.BitXor: "^", 

65 ast.Div: "/", 

66 ast.FloorDiv: "//", 

67 ast.MatMult: "@", 

68 ast.Mod: "%", 

69 ast.Mult: "*", 

70 ast.Pow: "**", 

71 ast.Sub: "-", 

72 ast.LShift: "<<", 

73 ast.RShift: ">>", 

74 } 

75 

76 

77def _bool_operators_from_module() -> dict[type[ast.boolop], str]: 

78 return {ast.And: "and", ast.Or: "or"} 

79 

80 

81def _compare_operators_from_module() -> dict[type[ast.cmpop], str]: 

82 return { 

83 ast.Eq: "==", 

84 ast.Gt: ">", 

85 ast.GtE: ">=", 

86 ast.In: "in", 

87 ast.Is: "is", 

88 ast.IsNot: "is not", 

89 ast.Lt: "<", 

90 ast.LtE: "<=", 

91 ast.NotEq: "!=", 

92 ast.NotIn: "not in", 

93 } 

94 

95 

96def _contexts_from_module() -> dict[type[ast.expr_context], Context]: 

97 return { 

98 ast.Load: Context.Load, 

99 ast.Store: Context.Store, 

100 ast.Del: Context.Del, 

101 ast.Param: Context.Store, 

102 }