Skip to content

Fix re brain on 3.11 #1515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ What's New in astroid 2.12.0?
=============================
Release date: TBA

* Fix ``re`` brain on Python ``3.11``. The flags now come from ``re._compile``.


What's New in astroid 2.11.4?
Expand Down
36 changes: 21 additions & 15 deletions astroid/brain/brain_re.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,39 @@
from astroid import context, inference_tip, nodes
from astroid.brain.helpers import register_module_extender
from astroid.builder import _extract_single_node, parse
from astroid.const import PY37_PLUS, PY39_PLUS
from astroid.const import PY37_PLUS, PY39_PLUS, PY311_PLUS
from astroid.manager import AstroidManager


def _re_transform():
# Since Python 3.6 there is the RegexFlag enum
# where every entry will be exposed via updating globals()
def _re_transform() -> nodes.Module:
# The RegexFlag enum exposes all its entries by updating globals()
# In 3.6-3.10 all flags come from sre_compile
# On 3.11+ all flags come from re._compiler
if PY311_PLUS:
import_compiler = "import re._compiler as _compiler"
else:
import_compiler = "import sre_compile as _compiler"
return parse(
"""
import sre_compile
ASCII = sre_compile.SRE_FLAG_ASCII
IGNORECASE = sre_compile.SRE_FLAG_IGNORECASE
LOCALE = sre_compile.SRE_FLAG_LOCALE
UNICODE = sre_compile.SRE_FLAG_UNICODE
MULTILINE = sre_compile.SRE_FLAG_MULTILINE
DOTALL = sre_compile.SRE_FLAG_DOTALL
VERBOSE = sre_compile.SRE_FLAG_VERBOSE
f"""
{import_compiler}
NOFLAG = 0
ASCII = _compiler.SRE_FLAG_ASCII
IGNORECASE = _compiler.SRE_FLAG_IGNORECASE
LOCALE = _compiler.SRE_FLAG_LOCALE
UNICODE = _compiler.SRE_FLAG_UNICODE
MULTILINE = _compiler.SRE_FLAG_MULTILINE
DOTALL = _compiler.SRE_FLAG_DOTALL
VERBOSE = _compiler.SRE_FLAG_VERBOSE
TEMPLATE = _compiler.SRE_FLAG_TEMPLATE
DEBUG = _compiler.SRE_FLAG_DEBUG
A = ASCII
I = IGNORECASE
L = LOCALE
U = UNICODE
M = MULTILINE
S = DOTALL
X = VERBOSE
TEMPLATE = sre_compile.SRE_FLAG_TEMPLATE
T = TEMPLATE
DEBUG = sre_compile.SRE_FLAG_DEBUG
"""
)

Expand Down
1 change: 1 addition & 0 deletions astroid/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
PY38_PLUS = sys.version_info >= (3, 8)
PY39_PLUS = sys.version_info >= (3, 9)
PY310_PLUS = sys.version_info >= (3, 10)
PY311_PLUS = sys.version_info >= (3, 11)
BUILTINS = "builtins" # TODO Remove in 2.8

WIN32 = sys.platform == "win32"
Expand Down