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

35 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:53 +0000

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(self, string: str, type_comments: bool = True) -> ast.Module: 

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

27 

28 

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

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

31 func_type = ast.parse(type_comment, "<type_comment>", "func_type") # type: ignore[attr-defined] 

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

33 

34 

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

36 unary_op_classes = _unary_operators_from_module() 

37 cmp_op_classes = _compare_operators_from_module() 

38 bool_op_classes = _bool_operators_from_module() 

39 bin_op_classes = _binary_operators_from_module() 

40 context_classes = _contexts_from_module() 

41 

42 return ParserModule( 

43 unary_op_classes, 

44 cmp_op_classes, 

45 bool_op_classes, 

46 bin_op_classes, 

47 context_classes, 

48 ) 

49 

50 

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

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

53 

54 

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

56 return { 

57 ast.Add: "+", 

58 ast.BitAnd: "&", 

59 ast.BitOr: "|", 

60 ast.BitXor: "^", 

61 ast.Div: "/", 

62 ast.FloorDiv: "//", 

63 ast.MatMult: "@", 

64 ast.Mod: "%", 

65 ast.Mult: "*", 

66 ast.Pow: "**", 

67 ast.Sub: "-", 

68 ast.LShift: "<<", 

69 ast.RShift: ">>", 

70 } 

71 

72 

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

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

75 

76 

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

78 return { 

79 ast.Eq: "==", 

80 ast.Gt: ">", 

81 ast.GtE: ">=", 

82 ast.In: "in", 

83 ast.Is: "is", 

84 ast.IsNot: "is not", 

85 ast.Lt: "<", 

86 ast.LtE: "<=", 

87 ast.NotEq: "!=", 

88 ast.NotIn: "not in", 

89 } 

90 

91 

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

93 return { 

94 ast.Load: Context.Load, 

95 ast.Store: Context.Store, 

96 ast.Del: Context.Del, 

97 ast.Param: Context.Store, 

98 }