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 A = ASCII = 0x80 # Assume ASCII locale.
23 B = BESTMATCH = 0x1000 # Best fuzzy match.
24 D = DEBUG = 0x200 # Print parsed pattern.
25 E = ENHANCEMATCH = 0x8000 # Attempt to improve the fit after finding the first
26 # fuzzy match.
27 F = FULLCASE = 0x4000 # Unicode full case-folding.
28 I = IGNORECASE = 0x2 # Ignore case.
29 L = LOCALE = 0x4 # Assume current 8-bit locale.
30 M = MULTILINE = 0x8 # Make anchors look for newline.
31 P = POSIX = 0x10000 # POSIX-style matching (leftmost longest).
32 R = REVERSE = 0x400 # Search backwards.
33 S = DOTALL = 0x10 # Make dot match newline.
34 U = UNICODE = 0x20 # Assume Unicode locale.
35 V0 = VERSION0 = 0x2000 # Old legacy behaviour.
36 DEFAULT_VERSION = V0
37 V1 = VERSION1 = 0x100 # New enhanced behaviour.
38 W = WORD = 0x800 # Default Unicode word breaks.
39 X = VERBOSE = 0x40 # Ignore whitespace and comments.
40 T = TEMPLATE = 0x1 # Template (present because re module has it).
41 """)
42
43
44CLASS_GETITEM_TEMPLATE = """
45@classmethod
46def __class_getitem__(cls, item):
47 return cls
48"""
49
50
51def _looks_like_pattern_or_match(node: nodes.Call) -> bool:
52 """Check for regex.Pattern or regex.Match call in stdlib.
53
54 Match these patterns from stdlib/re.py
55 ```py
56 Pattern = type(...)
57 Match = type(...)
58 ```
59 """
60 return (
61 node.root().name == "regex.regex"
62 and isinstance(node.func, nodes.Name)
63 and node.func.name == "type"
64 and isinstance(node.parent, nodes.Assign)
65 and len(node.parent.targets) == 1
66 and isinstance(node.parent.targets[0], nodes.AssignName)
67 and node.parent.targets[0].name in {"Pattern", "Match"}
68 )
69
70
71def infer_pattern_match(node: nodes.Call, ctx: context.InferenceContext | None = None):
72 """Infer regex.Pattern and regex.Match as classes.
73
74 For PY39+ add `__class_getitem__`.
75 """
76 class_def = nodes.ClassDef(
77 name=node.parent.targets[0].name,
78 lineno=node.lineno,
79 col_offset=node.col_offset,
80 parent=node.parent,
81 end_lineno=node.end_lineno,
82 end_col_offset=node.end_col_offset,
83 )
84 func_to_add = _extract_single_node(CLASS_GETITEM_TEMPLATE)
85 class_def.locals["__class_getitem__"] = [func_to_add]
86 return iter([class_def])
87
88
89def register(manager: AstroidManager) -> None:
90 register_module_extender(manager, "regex", _regex_transform)
91 manager.register_transform(
92 nodes.Call, inference_tip(infer_pattern_match), _looks_like_pattern_or_match
93 )