Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/astroid/brain/brain_regex.py: 68%
19 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:53 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:53 +0000
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
5from __future__ import annotations
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
11from astroid.manager import AstroidManager
14def _regex_transform() -> nodes.Module:
15 """The RegexFlag enum exposes all its entries by updating globals().
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 )
46register_module_extender(AstroidManager(), "regex", _regex_transform)
49CLASS_GETITEM_TEMPLATE = """
50@classmethod
51def __class_getitem__(cls, item):
52 return cls
53"""
56def _looks_like_pattern_or_match(node: nodes.Call) -> bool:
57 """Check for regex.Pattern or regex.Match call in stdlib.
59 Match these patterns from stdlib/re.py
60 ```py
61 Pattern = type(...)
62 Match = type(...)
63 ```
64 """
65 return (
66 node.root().name == "regex.regex"
67 and isinstance(node.func, nodes.Name)
68 and node.func.name == "type"
69 and isinstance(node.parent, nodes.Assign)
70 and len(node.parent.targets) == 1
71 and isinstance(node.parent.targets[0], nodes.AssignName)
72 and node.parent.targets[0].name in {"Pattern", "Match"}
73 )
76def infer_pattern_match(node: nodes.Call, ctx: context.InferenceContext | None = None):
77 """Infer regex.Pattern and regex.Match as classes.
79 For PY39+ add `__class_getitem__`.
80 """
81 class_def = nodes.ClassDef(
82 name=node.parent.targets[0].name,
83 lineno=node.lineno,
84 col_offset=node.col_offset,
85 parent=node.parent,
86 end_lineno=node.end_lineno,
87 end_col_offset=node.end_col_offset,
88 )
89 if PY39_PLUS:
90 func_to_add = _extract_single_node(CLASS_GETITEM_TEMPLATE)
91 class_def.locals["__class_getitem__"] = [func_to_add]
92 return iter([class_def])
95AstroidManager().register_transform(
96 nodes.Call, inference_tip(infer_pattern_match), _looks_like_pattern_or_match
97)