Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/astroid/brain/brain_re.py: 59%

22 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 

7from astroid import context, inference_tip, nodes 

8from astroid.brain.helpers import register_module_extender 

9from astroid.builder import _extract_single_node, parse 

10from astroid.const import PY39_PLUS, PY311_PLUS 

11from astroid.manager import AstroidManager 

12 

13 

14def _re_transform() -> nodes.Module: 

15 # The RegexFlag enum exposes all its entries by updating globals() 

16 # In 3.6-3.10 all flags come from sre_compile 

17 # On 3.11+ all flags come from re._compiler 

18 if PY311_PLUS: 

19 import_compiler = "import re._compiler as _compiler" 

20 else: 

21 import_compiler = "import sre_compile as _compiler" 

22 return parse( 

23 f""" 

24 {import_compiler} 

25 NOFLAG = 0 

26 ASCII = _compiler.SRE_FLAG_ASCII 

27 IGNORECASE = _compiler.SRE_FLAG_IGNORECASE 

28 LOCALE = _compiler.SRE_FLAG_LOCALE 

29 UNICODE = _compiler.SRE_FLAG_UNICODE 

30 MULTILINE = _compiler.SRE_FLAG_MULTILINE 

31 DOTALL = _compiler.SRE_FLAG_DOTALL 

32 VERBOSE = _compiler.SRE_FLAG_VERBOSE 

33 TEMPLATE = _compiler.SRE_FLAG_TEMPLATE 

34 DEBUG = _compiler.SRE_FLAG_DEBUG 

35 A = ASCII 

36 I = IGNORECASE 

37 L = LOCALE 

38 U = UNICODE 

39 M = MULTILINE 

40 S = DOTALL 

41 X = VERBOSE 

42 T = TEMPLATE 

43 """ 

44 ) 

45 

46 

47register_module_extender(AstroidManager(), "re", _re_transform) 

48 

49 

50CLASS_GETITEM_TEMPLATE = """ 

51@classmethod 

52def __class_getitem__(cls, item): 

53 return cls 

54""" 

55 

56 

57def _looks_like_pattern_or_match(node: nodes.Call) -> bool: 

58 """Check for re.Pattern or re.Match call in stdlib. 

59 

60 Match these patterns from stdlib/re.py 

61 ```py 

62 Pattern = type(...) 

63 Match = type(...) 

64 ``` 

65 """ 

66 return ( 

67 node.root().name == "re" 

68 and isinstance(node.func, nodes.Name) 

69 and node.func.name == "type" 

70 and isinstance(node.parent, nodes.Assign) 

71 and len(node.parent.targets) == 1 

72 and isinstance(node.parent.targets[0], nodes.AssignName) 

73 and node.parent.targets[0].name in {"Pattern", "Match"} 

74 ) 

75 

76 

77def infer_pattern_match(node: nodes.Call, ctx: context.InferenceContext | None = None): 

78 """Infer re.Pattern and re.Match as classes. 

79 

80 For PY39+ add `__class_getitem__`. 

81 """ 

82 class_def = nodes.ClassDef( 

83 name=node.parent.targets[0].name, 

84 lineno=node.lineno, 

85 col_offset=node.col_offset, 

86 parent=node.parent, 

87 end_lineno=node.end_lineno, 

88 end_col_offset=node.end_col_offset, 

89 ) 

90 if PY39_PLUS: 

91 func_to_add = _extract_single_node(CLASS_GETITEM_TEMPLATE) 

92 class_def.locals["__class_getitem__"] = [func_to_add] 

93 return iter([class_def]) 

94 

95 

96AstroidManager().register_transform( 

97 nodes.Call, inference_tip(infer_pattern_match), _looks_like_pattern_or_match 

98)