File: test_typing_compiler.py

package info (click to toggle)
python-aristaproto 1.2%2Breally0.1.4-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 1,600 kB
  • sloc: python: 6,521; java: 106; xml: 84; makefile: 6
file content (76 lines) | stat: -rw-r--r-- 3,161 bytes parent folder | download | duplicates (3)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from aristaproto.plugin.typing_compiler import (
    DirectImportTypingCompiler,
    NoTyping310TypingCompiler,
    TypingImportTypingCompiler,
)


def test_direct_import_typing_compiler():
    compiler = DirectImportTypingCompiler()
    assert compiler.imports() == {}
    assert compiler.optional("str") == "Optional[str]"
    assert compiler.imports() == {"typing": {"Optional"}}
    assert compiler.list("str") == "List[str]"
    assert compiler.imports() == {"typing": {"Optional", "List"}}
    assert compiler.dict("str", "int") == "Dict[str, int]"
    assert compiler.imports() == {"typing": {"Optional", "List", "Dict"}}
    assert compiler.union("str", "int") == "Union[str, int]"
    assert compiler.imports() == {"typing": {"Optional", "List", "Dict", "Union"}}
    assert compiler.iterable("str") == "Iterable[str]"
    assert compiler.imports() == {
        "typing": {"Optional", "List", "Dict", "Union", "Iterable"}
    }
    assert compiler.async_iterable("str") == "AsyncIterable[str]"
    assert compiler.imports() == {
        "typing": {"Optional", "List", "Dict", "Union", "Iterable", "AsyncIterable"}
    }
    assert compiler.async_iterator("str") == "AsyncIterator[str]"
    assert compiler.imports() == {
        "typing": {
            "Optional",
            "List",
            "Dict",
            "Union",
            "Iterable",
            "AsyncIterable",
            "AsyncIterator",
        }
    }


def test_typing_import_typing_compiler():
    compiler = TypingImportTypingCompiler()
    assert compiler.imports() == {}
    assert compiler.optional("str") == "typing.Optional[str]"
    assert compiler.imports() == {"typing": None}
    assert compiler.list("str") == "typing.List[str]"
    assert compiler.imports() == {"typing": None}
    assert compiler.dict("str", "int") == "typing.Dict[str, int]"
    assert compiler.imports() == {"typing": None}
    assert compiler.union("str", "int") == "typing.Union[str, int]"
    assert compiler.imports() == {"typing": None}
    assert compiler.iterable("str") == "typing.Iterable[str]"
    assert compiler.imports() == {"typing": None}
    assert compiler.async_iterable("str") == "typing.AsyncIterable[str]"
    assert compiler.imports() == {"typing": None}
    assert compiler.async_iterator("str") == "typing.AsyncIterator[str]"
    assert compiler.imports() == {"typing": None}


def test_no_typing_311_typing_compiler():
    compiler = NoTyping310TypingCompiler()
    assert compiler.imports() == {}
    assert compiler.optional("str") == '"str | None"'
    assert compiler.imports() == {}
    assert compiler.list("str") == '"list[str]"'
    assert compiler.imports() == {}
    assert compiler.dict("str", "int") == '"dict[str, int]"'
    assert compiler.imports() == {}
    assert compiler.union("str", "int") == '"str | int"'
    assert compiler.imports() == {}
    assert compiler.iterable("str") == '"Iterable[str]"'
    assert compiler.async_iterable("str") == '"AsyncIterable[str]"'
    assert compiler.async_iterator("str") == '"AsyncIterator[str]"'
    assert compiler.imports() == {
        "collections.abc": {"Iterable", "AsyncIterable", "AsyncIterator"}
    }