Skip to content

Commit 8a98393

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

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
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: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,39 @@
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()
14+
def _re_transform() -> nodes.Module:
15+
# The RegexFlag enum exposes all its entries by updating globals()
16+
# On 3.11+ all flags come from re._compiler
17+
if PY311_PLUS:
18+
import_compiler = "import re._compiler as _compiler"
19+
# In 3.6-3.10 all flags come from sre_compile
20+
else:
21+
import_compiler = "import sre_compiler as _compiler"
1722
return parse(
18-
"""
19-
import sre_compile
20-
ASCII = sre_compile.SRE_FLAG_ASCII
21-
IGNORECASE = sre_compile.SRE_FLAG_IGNORECASE
22-
LOCALE = sre_compile.SRE_FLAG_LOCALE
23-
UNICODE = sre_compile.SRE_FLAG_UNICODE
24-
MULTILINE = sre_compile.SRE_FLAG_MULTILINE
25-
DOTALL = sre_compile.SRE_FLAG_DOTALL
26-
VERBOSE = sre_compile.SRE_FLAG_VERBOSE
23+
f"""
24+
{import_compiler}
25+
NOFLAG = 0
26+
ASCII = _compiler.SRE_FLAG_ASCII
27+
IGNORECASE = _compiler.SRE_FLAG_IGNORECASE
28+
LOCALE = _compiler.SRE_FLAG_LOCALE
29+
UNICODE = _compiler.SRE_FLAG_UNICODE
30+
MULTILINE = _compiler.SRE_FLAG_MULTILINE
31+
DOTALL = _compiler.SRE_FLAG_DOTALL
32+
VERBOSE = _compiler.SRE_FLAG_VERBOSE
33+
TEMPLATE = _compiler.SRE_FLAG_TEMPLATE
34+
DEBUG = _compiler.SRE_FLAG_DEBUG
2735
A = ASCII
2836
I = IGNORECASE
2937
L = LOCALE
3038
U = UNICODE
3139
M = MULTILINE
3240
S = DOTALL
3341
X = VERBOSE
34-
TEMPLATE = sre_compile.SRE_FLAG_TEMPLATE
3542
T = TEMPLATE
36-
DEBUG = sre_compile.SRE_FLAG_DEBUG
3743
"""
3844
)
3945

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)