Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.10/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, nodes 

8from astroid.brain.helpers import register_module_extender 

9from astroid.builder import _extract_single_node, parse 

10from astroid.const import PY311_PLUS 

11from astroid.inference_tip import inference_tip 

12from astroid.manager import AstroidManager 

13 

14 

15def _re_transform() -> nodes.Module: 

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

17 # In 3.6-3.10 all flags come from sre_compile 

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

19 if PY311_PLUS: 

20 import_compiler = "import re._compiler as _compiler" 

21 else: 

22 import_compiler = "import sre_compile as _compiler" 

23 return parse( 

24 f""" 

25 {import_compiler} 

26 NOFLAG = 0 

27 ASCII = _compiler.SRE_FLAG_ASCII 

28 IGNORECASE = _compiler.SRE_FLAG_IGNORECASE 

29 LOCALE = _compiler.SRE_FLAG_LOCALE 

30 UNICODE = _compiler.SRE_FLAG_UNICODE 

31 MULTILINE = _compiler.SRE_FLAG_MULTILINE 

32 DOTALL = _compiler.SRE_FLAG_DOTALL 

33 VERBOSE = _compiler.SRE_FLAG_VERBOSE 

34 TEMPLATE = _compiler.SRE_FLAG_TEMPLATE 

35 DEBUG = _compiler.SRE_FLAG_DEBUG 

36 A = ASCII 

37 I = IGNORECASE 

38 L = LOCALE 

39 U = UNICODE 

40 M = MULTILINE 

41 S = DOTALL 

42 X = VERBOSE 

43 T = TEMPLATE 

44 """ 

45 ) 

46 

47 

48CLASS_GETITEM_TEMPLATE = """ 

49@classmethod 

50def __class_getitem__(cls, item): 

51 return cls 

52""" 

53 

54 

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

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

57 

58 Match these patterns from stdlib/re.py 

59 ```py 

60 Pattern = type(...) 

61 Match = type(...) 

62 ``` 

63 """ 

64 return ( 

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

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

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

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

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

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

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

72 ) 

73 

74 

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

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

77 

78 For PY39+ add `__class_getitem__`. 

79 """ 

80 class_def = nodes.ClassDef( 

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

82 lineno=node.lineno, 

83 col_offset=node.col_offset, 

84 parent=node.parent, 

85 end_lineno=node.end_lineno, 

86 end_col_offset=node.end_col_offset, 

87 ) 

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 )