Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.10/site-packages/astroid/brain/brain_regex.py: 68%

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

19 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.inference_tip import inference_tip 

11from astroid.manager import AstroidManager 

12 

13 

14def _regex_transform() -> nodes.Module: 

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

16 

17 We hard-code the flags for now. 

18 # pylint: disable-next=line-too-long 

19 See https://github.com/mrabarnett/mrab-regex/blob/2022.10.31/regex_3/regex.py#L200 

20 """ 

21 return parse( 

22 """ 

23 A = ASCII = 0x80 # Assume ASCII locale. 

24 B = BESTMATCH = 0x1000 # Best fuzzy match. 

25 D = DEBUG = 0x200 # Print parsed pattern. 

26 E = ENHANCEMATCH = 0x8000 # Attempt to improve the fit after finding the first 

27 # fuzzy match. 

28 F = FULLCASE = 0x4000 # Unicode full case-folding. 

29 I = IGNORECASE = 0x2 # Ignore case. 

30 L = LOCALE = 0x4 # Assume current 8-bit locale. 

31 M = MULTILINE = 0x8 # Make anchors look for newline. 

32 P = POSIX = 0x10000 # POSIX-style matching (leftmost longest). 

33 R = REVERSE = 0x400 # Search backwards. 

34 S = DOTALL = 0x10 # Make dot match newline. 

35 U = UNICODE = 0x20 # Assume Unicode locale. 

36 V0 = VERSION0 = 0x2000 # Old legacy behaviour. 

37 DEFAULT_VERSION = V0 

38 V1 = VERSION1 = 0x100 # New enhanced behaviour. 

39 W = WORD = 0x800 # Default Unicode word breaks. 

40 X = VERBOSE = 0x40 # Ignore whitespace and comments. 

41 T = TEMPLATE = 0x1 # Template (present because re module has it). 

42 """ 

43 ) 

44 

45 

46CLASS_GETITEM_TEMPLATE = """ 

47@classmethod 

48def __class_getitem__(cls, item): 

49 return cls 

50""" 

51 

52 

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

54 """Check for regex.Pattern or regex.Match call in stdlib. 

55 

56 Match these patterns from stdlib/re.py 

57 ```py 

58 Pattern = type(...) 

59 Match = type(...) 

60 ``` 

61 """ 

62 return ( 

63 node.root().name == "regex.regex" 

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

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

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

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

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

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

70 ) 

71 

72 

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

74 """Infer regex.Pattern and regex.Match as classes. 

75 

76 For PY39+ add `__class_getitem__`. 

77 """ 

78 class_def = nodes.ClassDef( 

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

80 lineno=node.lineno, 

81 col_offset=node.col_offset, 

82 parent=node.parent, 

83 end_lineno=node.end_lineno, 

84 end_col_offset=node.end_col_offset, 

85 ) 

86 func_to_add = _extract_single_node(CLASS_GETITEM_TEMPLATE) 

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

88 return iter([class_def]) 

89 

90 

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

92 register_module_extender(manager, "regex", _regex_transform) 

93 manager.register_transform( 

94 nodes.Call, inference_tip(infer_pattern_match), _looks_like_pattern_or_match 

95 )