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

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

20 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 _re_transform() -> nodes.Module: 

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

16 # All flags come from re._compiler 

17 return parse(""" 

18 import re._compiler as _compiler 

19 NOFLAG = 0 

20 ASCII = _compiler.SRE_FLAG_ASCII 

21 IGNORECASE = _compiler.SRE_FLAG_IGNORECASE 

22 LOCALE = _compiler.SRE_FLAG_LOCALE 

23 UNICODE = _compiler.SRE_FLAG_UNICODE 

24 MULTILINE = _compiler.SRE_FLAG_MULTILINE 

25 DOTALL = _compiler.SRE_FLAG_DOTALL 

26 VERBOSE = _compiler.SRE_FLAG_VERBOSE 

27 TEMPLATE = _compiler.SRE_FLAG_TEMPLATE 

28 DEBUG = _compiler.SRE_FLAG_DEBUG 

29 A = ASCII 

30 I = IGNORECASE 

31 L = LOCALE 

32 U = UNICODE 

33 M = MULTILINE 

34 S = DOTALL 

35 X = VERBOSE 

36 T = TEMPLATE 

37 """) 

38 

39 

40CLASS_GETITEM_TEMPLATE = """ 

41@classmethod 

42def __class_getitem__(cls, item): 

43 return cls 

44""" 

45 

46 

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

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

49 

50 Match these patterns from stdlib/re.py 

51 ```py 

52 Pattern = type(...) 

53 Match = type(...) 

54 ``` 

55 """ 

56 return ( 

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

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

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

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

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

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

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

64 ) 

65 

66 

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

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

69 

70 For PY39+ add `__class_getitem__`. 

71 """ 

72 class_def = nodes.ClassDef( 

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

74 lineno=node.lineno, 

75 col_offset=node.col_offset, 

76 parent=node.parent, 

77 end_lineno=node.end_lineno, 

78 end_col_offset=node.end_col_offset, 

79 ) 

80 func_to_add = _extract_single_node(CLASS_GETITEM_TEMPLATE) 

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

82 return iter([class_def]) 

83 

84 

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

86 register_module_extender(manager, "re", _re_transform) 

87 manager.register_transform( 

88 nodes.Call, inference_tip(infer_pattern_match), _looks_like_pattern_or_match 

89 )