Skip to content

Commit f324a0f

Browse files
committed
Fix re brain on 3.11
1 parent af38d3c commit f324a0f

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

ChangeLog

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ What's New in astroid 2.12.0?
66
=============================
77
Release date: TBA
88

9-
9+
* Fix ``re`` brain on Python ``3.11``. The flags now come from ``re._compile``.
1010

1111
What's New in astroid 2.11.3?
1212
=============================

astroid/brain/brain_re.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77
from astroid import context, inference_tip, nodes
88
from astroid.brain.helpers import register_module_extender
99
from astroid.builder import _extract_single_node, parse
10-
from astroid.const import PY37_PLUS, PY39_PLUS
10+
from astroid.const import PY37_PLUS, PY39_PLUS, PY311_PLUS
1111
from astroid.manager import AstroidManager
1212

1313

14-
def _re_transform():
15-
# Since Python 3.6 there is the RegexFlag enum
16-
# where every entry will be exposed via updating globals()
17-
return parse(
18-
"""
14+
def _re_transform() -> nodes.Module:
15+
# The RegexFlag enum exposes all its entries by updating globals()
16+
# In 3.6-3.10 all flags come from sre_compile
17+
if not PY311_PLUS:
18+
return parse(
19+
"""
1920
import sre_compile
2021
ASCII = sre_compile.SRE_FLAG_ASCII
2122
IGNORECASE = sre_compile.SRE_FLAG_IGNORECASE
@@ -34,6 +35,30 @@ def _re_transform():
3435
TEMPLATE = sre_compile.SRE_FLAG_TEMPLATE
3536
T = TEMPLATE
3637
DEBUG = sre_compile.SRE_FLAG_DEBUG
38+
"""
39+
)
40+
# On 3.11+ all flags come from re._compiler
41+
return parse(
42+
"""
43+
import re._compiler as _compiler
44+
NOFLAG = 0
45+
ASCII = _compiler.SRE_FLAG_ASCII
46+
IGNORECASE = _compiler.SRE_FLAG_IGNORECASE
47+
LOCALE = _compiler.SRE_FLAG_LOCALE
48+
UNICODE = _compiler.SRE_FLAG_UNICODE
49+
MULTILINE = _compiler.SRE_FLAG_MULTILINE
50+
DOTALL = _compiler.SRE_FLAG_DOTALL
51+
VERBOSE = _compiler.SRE_FLAG_VERBOSE
52+
TEMPLATE = _compiler.SRE_FLAG_TEMPLATE
53+
DEBUG = _compiler.SRE_FLAG_DEBUG
54+
A = ASCII
55+
I = IGNORECASE
56+
L = LOCALE
57+
U = UNICODE
58+
M = MULTILINE
59+
S = DOTALL
60+
X = VERBOSE
61+
T = TEMPLATE
3762
"""
3863
)
3964

astroid/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
PY38_PLUS = sys.version_info >= (3, 8)
1212
PY39_PLUS = sys.version_info >= (3, 9)
1313
PY310_PLUS = sys.version_info >= (3, 10)
14+
PY311_PLUS = sys.version_info >= (3, 11)
1415
BUILTINS = "builtins" # TODO Remove in 2.8
1516

1617
WIN32 = sys.platform == "win32"

0 commit comments

Comments
 (0)