| 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 |
|
| 5 |
try: |
| 6 |
import regex # type: ignore[import] |
| 7 |
|
| 8 |
HAS_REGEX = True |
| 9 |
except ImportError: |
| 10 |
HAS_REGEX = False |
| 11 |
|
| 12 |
import pytest |
| 13 |
|
| 14 |
from astroid import MANAGER, builder, nodes |
| 15 |
|
| 16 |
|
| 17 |
@pytest.mark.skipif(not HAS_REGEX, reason="This test requires the regex library.") |
| 18 |
class TestRegexBrain: |
| 19 |
def test_regex_flags(self) -> None: |
| 20 |
"""Test that we have all regex enum flags in the brain.""" |
| 21 |
names = [name for name in dir(regex) if name.isupper()] |
| 22 |
re_ast = MANAGER.ast_from_module_name("regex") |
| 23 |
for name in names: |
| 24 |
assert name in re_ast |
| 25 |
assert next(re_ast[name].infer()).value == getattr(regex, name) |
| 26 |
|
| 27 |
def test_regex_pattern_and_match_subscriptable(self): |
| 28 |
"""Test regex.Pattern and regex.Match are subscriptable in PY39+.""" |
| 29 |
node1 = builder.extract_node(""" |
| 30 |
import regex |
| 31 |
regex.Pattern[str] |
| 32 |
""") |
| 33 |
inferred1 = next(node1.infer()) |
| 34 |
assert isinstance(inferred1, nodes.ClassDef) |
| 35 |
assert isinstance(inferred1.getattr("__class_getitem__")[0], nodes.FunctionDef) |
| 36 |
|
| 37 |
node2 = builder.extract_node(""" |
| 38 |
import regex |
| 39 |
regex.Match[str] |
| 40 |
""") |
| 41 |
inferred2 = next(node2.infer()) |
| 42 |
assert isinstance(inferred2, nodes.ClassDef) |
| 43 |
assert isinstance(inferred2.getattr("__class_getitem__")[0], nodes.FunctionDef) |