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

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

23 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 

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 

47CLASS_GETITEM_TEMPLATE = """ 

48@classmethod 

49def __class_getitem__(cls, item): 

50 return cls 

51""" 

52 

53 

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

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

56 

57 Match these patterns from stdlib/re.py 

58 ```py 

59 Pattern = type(...) 

60 Match = type(...) 

61 ``` 

62 """ 

63 return ( 

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

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

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

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

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

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

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

71 ) 

72 

73 

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

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

76 

77 For PY39+ add `__class_getitem__`. 

78 """ 

79 class_def = nodes.ClassDef( 

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

81 lineno=node.lineno, 

82 col_offset=node.col_offset, 

83 parent=node.parent, 

84 end_lineno=node.end_lineno, 

85 end_col_offset=node.end_col_offset, 

86 ) 

87 if PY39_PLUS: 

88 func_to_add = _extract_single_node(CLASS_GETITEM_TEMPLATE) 

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

90 return iter([class_def]) 

91 

92 

93def register(manager: AstroidManager) -> None: 

94 register_module_extender(manager, "re", _re_transform) 

95 manager.register_transform( 

96 nodes.Call, inference_tip(infer_pattern_match), _looks_like_pattern_or_match 

97 )