summaryrefslogtreecommitdiff
path: root/tests/brain/test_regex.py
blob: 0d44074df6ff059ad5498735caa05c6cb4347e4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
# For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
# Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt

try:
    import regex

    HAS_REGEX = True
except ImportError:
    HAS_REGEX = False

import pytest

from astroid import MANAGER, builder, nodes, test_utils


@pytest.mark.skipif(not HAS_REGEX, reason="This test requires the regex library.")
class TestRegexBrain:
    def test_regex_flags(self) -> None:
        """Test that we have all regex enum flags in the brain."""
        names = [name for name in dir(regex) if name.isupper()]
        re_ast = MANAGER.ast_from_module_name("regex")
        for name in names:
            assert name in re_ast
            assert next(re_ast[name].infer()).value == getattr(regex, name)

    @pytest.mark.xfail(
        reason="Started failing on main, but no one reproduced locally yet"
    )
    @test_utils.require_version(minver="3.9")
    def test_regex_pattern_and_match_subscriptable(self):
        """Test regex.Pattern and regex.Match are subscriptable in PY39+."""
        node1 = builder.extract_node(
            """
        import regex
        regex.Pattern[str]
        """
        )
        inferred1 = next(node1.infer())
        assert isinstance(inferred1, nodes.ClassDef)
        assert isinstance(inferred1.getattr("__class_getitem__")[0], nodes.FunctionDef)

        node2 = builder.extract_node(
            """
        import regex
        regex.Match[str]
        """
        )
        inferred2 = next(node2.infer())
        assert isinstance(inferred2, nodes.ClassDef)
        assert isinstance(inferred2.getattr("__class_getitem__")[0], nodes.FunctionDef)